summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/TypeName.java
blob: e8849e0554a1914476a9683bbeefe2695154a046 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * TypeName.java 1.0
 *
 * This interface is made to type ptree-node into the type
 * specifier in the body of class.
 *
 * Jun 20, 1997 by mich
 * Sep 29, 1997 by bv
 * Oct 11, 1997 by mich
 * Dec 27, 1998 by mich
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @version 1.0 last updated:  Oct 11, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import java.util.Hashtable;

import io.devnulllabs.openjava.mop.OJClass;
import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;

/**
 * The <code>TypeName</code> class represents a type specifier
 * node of parse tree.
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @see io.devnulllabs.openjava.ptree.NonLeaf
 */
public class TypeName extends NonLeaf {
    Hashtable suffixes;

    int dim;

    public TypeName(String typename, int n, Hashtable suffixes) {
        super();
        set(typename);
        setDimension(n);
        this.suffixes = suffixes;
    }

    /**
     * Allocates a new object.
     *
     * @param typename type name
     * @param n array dimension
     */
    public TypeName(String typename, int n) {
        this(typename, n, null);
    }

    /**
     * Allocates a new object.
     *
     * @param typename type name
     * @param n array dimension
     */
    public TypeName(String typename, Hashtable suffixes) {
        this(typename, 0, suffixes);
    }

    /**
     * Allocates a new object.
     * No array dimension will set.
     * <br><blockquote><pre>
     *     new TypeName( typename )
     * </pre></blockquote><br>
     * equals:
     * <br><blockquote><pre>
     *     new TypeName( typename, 0 )
     * </pre></blockquote><br>
     *
     * @param typename type name
     */
    public TypeName(String typename) {
        this(typename, 0, null);
    }

    TypeName() {
        super();
    }

    public static TypeName forOJClass(OJClass clazz) {
        int demension = 0;
        while (clazz.isArray()) {
            ++demension;
            clazz = clazz.getComponentType();
        }
        String name = clazz.getName();
        return new TypeName(name, demension);
    }

    public ParseTree makeRecursiveCopy() {
        TypeName result = (TypeName) super.makeRecursiveCopy();
        result.dim = this.dim;
        result.suffixes = this.suffixes;
        return result;
    }

    public ParseTree makeCopy() {
        TypeName result = (TypeName) super.makeCopy();
        result.dim = this.dim;
        result.suffixes = this.suffixes;
        return result;
    }

    /**
     * Gets array dimension of declarated type
     *
     * @return array dimension
     */
    public int getDimension() {
        return dim;
    }

    /**
     * Sets array dimension of declarated type
     *
     * @param dim array dimension
     */
    public void setDimension(int n) {
        dim = n;
    }

    public void addDimension(int n) {
        this.dim += n;
    }

    public void addDimension(String dimstr) {
        this.addDimension(dimstr.length() / 2);
    }

    /**
     * Gets the type name of this type specifier.
     *
     * @return  the type name.
     */
    public String getName() {
        return (String) elementAt(0);
    }

    /**
     * Sets the type name of this type specifier.
     *
     * @param typename the type name to set.
     * @deprecated
     * @see io.devnulllabs.openjava.ptree.TypeName#setName(String)
     */
    public void setTypeName(String typename) {
        setElementAt(typename, 0);
    }

    /**
     * Sets the type name except array dimension of this type specifier.
     *
     * @param name the type name to set.
     */
    public void setName(String name) {
        setElementAt(name, 0);
    }

    public static String stringFromDimension(int dimension) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < dimension; ++i) {
            buf.append("[]");
        }
        return buf.toString();
    }

    public static int toDimension(String typename) {
        int result = 0;
        for (int i = typename.length() - 1; 0 < i; i -= 2) {
            if (typename.lastIndexOf(']', i) != i
                || typename.lastIndexOf('[', i) != i - 1) {
                return result;
            }
            result++;
        }
        return result;
    }

    public void accept(ParseTreeVisitor v) throws ParseTreeException {
        v.visit(this);
    }

}