summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/ModifierList.java
blob: 48c713979a1672d92b0aeca29a9d6c64b991e2c5 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * ModifierList.java 1.0
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @version last updated: 06/11/97
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import java.lang.reflect.Modifier;

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

/**
 * The ModifierList class presents for the node of parse tree
 * of qualified name.
 * This is a List of String for extended modifiers.
 */
public class ModifierList extends List {

    /*
     * Access modifier flag constants from <em>The Java Virtual
     * Machine Specification</em>, Table 4.1.
     */
    public static final int PUBLIC = Modifier.PUBLIC;
    public static final int PROTECTED = Modifier.PROTECTED;
    public static final int PRIVATE = Modifier.PRIVATE;
    public static final int STATIC = Modifier.STATIC;
    public static final int FINAL = Modifier.FINAL;
    public static final int SYNCHRONIZED = Modifier.SYNCHRONIZED;
    public static final int VOLATILE = Modifier.VOLATILE;
    public static final int TRANSIENT = Modifier.TRANSIENT;
    public static final int NATIVE = Modifier.NATIVE;
    //    public static final int INTERFACE     =    Modifier.INTERFACE;
    public static final int ABSTRACT = Modifier.ABSTRACT;
    //    public static final int STRICT     =    Modifier.STRICT;
    //    public static final int EXPLICIT     =    Modifier.EXPLICIT;

    public static final int EMPTY = 0;

    private int mod = 0;

    public ModifierList() {
        super(" ");
    }

    public ModifierList(String e0) {
        super(" ", e0);
    }

    public ModifierList(int mod) {
        this();
        this.mod = mod;
    }

    public String toString() {
        return toString(this.getRegular());
    }

    /**
     * Return a string describing the access modifier flags in
     * the specified modifier. For example:
     * <blockquote><pre>
     *    public final synchronized
     *    private transient volatile
     * </pre></blockquote>
     * The modifier names are return in canonical order, as
     * specified by <em>The Java Language Specification</em>.
     */
    public static String toString(int mod) {
        StringBuffer sb = new StringBuffer();
        int len;

        if ((mod & PUBLIC) != 0)
            sb.append("public ");
        if ((mod & PRIVATE) != 0)
            sb.append("private ");
        if ((mod & PROTECTED) != 0)
            sb.append("protected ");

        /* Canonical order */
        if ((mod & ABSTRACT) != 0)
            sb.append("abstract ");
        if ((mod & STATIC) != 0)
            sb.append("static ");
        if ((mod & FINAL) != 0)
            sb.append("final ");
        if ((mod & TRANSIENT) != 0)
            sb.append("transient ");
        if ((mod & VOLATILE) != 0)
            sb.append("volatile ");
        if ((mod & NATIVE) != 0)
            sb.append("native ");
        if ((mod & SYNCHRONIZED) != 0)
            sb.append("synchronized ");

        //        if ((mod & INTERFACE) != 0)     sb.append( "interface " );

        //        if ((mod & STRICT) != 0)        sb.append( "strict " );
        //        if ((mod & EXPLICIT) != 0)      sb.append( "explicit " );

        if ((len = sb.length()) > 0) /* trim trailing space */
            return sb.toString().substring(0, len - 1);
        return "";
    }

    public boolean isEmpty() {
        return (super.isEmpty() && getRegular() == EMPTY);
    }

    public boolean isEmptyAsRegular() {
        return (getRegular() == EMPTY);
    }

    public boolean contains(String str) {
        if (str == null)
            return false;
        if (str.equals("public") && contains(PUBLIC))
            return true;
        if (str.equals("private") && contains(PRIVATE))
            return true;
        if (str.equals("protected") && contains(PROTECTED))
            return true;
        if (str.equals("abstract") && contains(ABSTRACT))
            return true;
        if (str.equals("static") && contains(STATIC))
            return true;
        if (str.equals("final") && contains(FINAL))
            return true;
        if (str.equals("transient") && contains(TRANSIENT))
            return true;
        if (str.equals("volatile") && contains(VOLATILE))
            return true;
        if (str.equals("native") && contains(NATIVE))
            return true;
        if (str.equals("synchronized") && contains(SYNCHRONIZED)) {
            return true;
        }
        return super.contains(str);
    }

    public boolean contains(int mod) {
        return ((this.mod & mod) != 0);
    }

    /**
     * Gets the specified element at
     *
     * @param  n
     */
    public String get(int n) {
        return (String) contents_elementAt(n);
    }

    /**
     * Sets the specified element at the specified place of the list
     *
     * @param  p  element
     * @param  n  the number where to set element
     */
    public void set(int n, String p) {
        contents_setElementAt(p, n);
    }

    /**
     * Adds the specified element after the list
     * This causes side-effect.
     *
     * @param  p  a modifier  to be inserted into the list
     */
    public void add(int mod) {
        this.mod |= mod;
    }

    /**
     * Adds the specified element after the list
     * This causes side-effect.
     *
     * @param  p  a modifier to be inserted into the list
     */
    public void add(String p) {
        contents_addElement(p);
    }

    /**
     * Removes an element at the specified point from this list
     *
     * @param  n  where to remove element.
     */
    public String remove(int n) {
        String removed = (String) contents_elementAt(n);
        contents_removeElementAt(n);
        return removed;
    }

    /**
     * Inserts the specified element into the list
     * before the specified element of the list.
     * This causes side-effect.
     *
     * @param  p  the element to be inserted into the list
     * @param  n  number of the element before which insertion ocuurs
     */
    public void insertElementAt(String p, int n) {
        contents_insertElementAt(p, n);
    }

    /**
     * Appends a list after this list.
     *
     * @param  lst  a list to be appended
     */
    public void append(ModifierList lst) {
        this.mod |= lst.getRegular();
        for (int i = 0; i < lst.size(); i++) {
            contents_addElement(lst.get(i));
        }
    }

    public int getRegular() {
        return this.mod;
    }

    public void setRegular(int mod) {
        this.mod = mod;
    }

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

}