KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > Modifiers


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 //import java.lang.reflect.Modifier;
28

29 import org.aspectj.compiler.base.JavaCompiler;
30 import org.aspectj.compiler.base.CodeWriter;
31 import java.io.IOException JavaDoc;
32
33
34 /**
35   * @grammar (public|private|...)*
36   * @property int value
37   *
38   */

39 public class Modifiers extends ASTObject {
40     /*
41      * Access modifier flag constants from <em>The Java Virtual
42      * Machine Specification</em>, Table 4.1.
43      */

44
45     /**
46      * The <code>int</code> value representing the <code>public</code>
47      * modifier.
48      */

49     public static final int PUBLIC = 0x00000001;
50
51     /**
52      * The <code>int</code> value representing the <code>private</code>
53      * modifier.
54      */

55     public static final int PRIVATE = 0x00000002;
56
57     /**
58      * The <code>int</code> value representing the <code>protected</code>
59      * modifier.
60      */

61     public static final int PROTECTED = 0x00000004;
62
63     /**
64      * The <code>int</code> value representing the <code>static</code>
65      * modifier.
66      */

67     public static final int STATIC = 0x00000008;
68
69     /**
70      * The <code>int</code> value representing the <code>final</code>
71      * modifier.
72      */

73     public static final int FINAL = 0x00000010;
74
75     /**
76      * The <code>int</code> value representing the <code>synchronized</code>
77      * modifier.
78      */

79     public static final int SYNCHRONIZED = 0x00000020;
80
81     /**
82      * The <code>int</code> value representing the <code>volatile</code>
83      * modifier.
84      */

85     public static final int VOLATILE = 0x00000040;
86
87     /**
88      * The <code>int</code> value representing the <code>transient</code>
89      * modifier.
90      */

91     public static final int TRANSIENT = 0x00000080;
92
93     /**
94      * The <code>int</code> value representing the <code>native</code>
95      * modifier.
96      */

97     public static final int NATIVE = 0x00000100;
98
99     /**
100      * The <code>int</code> value representing the <code>interface</code>
101      * modifier.
102      */

103     public static final int INTERFACE = 0x00000200;
104
105     /**
106      * The <code>int</code> value representing the <code>abstract</code>
107      * modifier.
108      */

109     public static final int ABSTRACT = 0x00000400;
110
111     /**
112      * The <code>int</code> value representing the <code>strictfp</code>
113      * modifier.
114      */

115     public static final int STRICT = 0x00000800;
116
117     //XXX I'm claiming the upper 16-bits of modifier space here for any
118
//XXX aspect-specific modifiers. This certainly isn't in line with
119
//XXX notions of an extensible compiler
120
public static final int PRIVILEGED = 0x00010000;
121
122     /**
123      * toString masks for different kinds of output
124      */

125
126     /** Default print mask */
127     public static final int DEFAULT_PRINT_MASK =
128         PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | SYNCHRONIZED |
129         VOLATILE | TRANSIENT | NATIVE | ABSTRACT | STRICT;
130
131     /** Print mask for interface modifiers - avoid public+abstract+strictfp */
132     public static final int INTERFACE_METHOD_PRINT_MASK =
133         STATIC | FINAL | SYNCHRONIZED | VOLATILE | TRANSIENT | NATIVE;
134
135     /** Print mask for constructor modifiers - avoid strictfp */
136     public static final int CONSTRUCTOR_PRINT_MASK =
137         PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | SYNCHRONIZED |
138         VOLATILE | TRANSIENT | NATIVE | ABSTRACT;
139
140
141         /*
142     protected static int modifierValue(String[] labels) {
143         int modifier = 0;
144         for (int i=0; i<labels.length; i++) {
145             String label = labels[i];
146             if (label.equals("abstract")) modifier |= Modifiers.ABSTRACT;
147             else if (label.equals("final")) modifier |= Modifiers.FINAL;
148             else if (label.equals("interface")) modifier |= Modifiers.INTERFACE;
149             else if (label.equals("native")) modifier |= Modifiers.NATIVE;
150             else if (label.equals("private")) modifier |= Modifiers.PRIVATE;
151             else if (label.equals("protected")) modifier |= Modifiers.PROTECTED;
152             else if (label.equals("public")) modifier |= Modifiers.PUBLIC;
153             else if (label.equals("static")) modifier |= Modifiers.STATIC;
154             else if (label.equals("synchronized")) modifier |= Modifiers.SYNCHRONIZED;
155             else if (label.equals("transient")) modifier |= Modifiers.TRANSIENT;
156             else if (label.equals("volatile")) modifier |= Modifiers.VOLATILE;
157             else if (label.equals("strictfp")) modifier |= Modifiers.STRICT;
158             else throw new RuntimeException("unknown modifier name: "+label);
159         }
160         return modifier;
161     }
162
163     public Modifiers(SourceLocation location, String[] labels) {
164         this(location,modifierValue(labels));
165     }
166     */

167
168     public static boolean isPublic(int value) {
169         return (value & PUBLIC) != 0;
170     }
171     public boolean isPublic() {
172         return (value & PUBLIC) != 0;
173     }
174     public static boolean isFinal(int value) {
175         return (value & FINAL) != 0;
176     }
177     public boolean isFinal() {
178         return (value & FINAL) != 0;
179     }
180     public static boolean isPrivate(int value) {
181         return (value & PRIVATE) != 0;
182     }
183     public boolean isPrivate() {
184         return (value & PRIVATE) != 0;
185     }
186     public static boolean isProtected(int value) {
187         return (value & PROTECTED) != 0;
188     }
189     public boolean isProtected() {
190         return (value & PROTECTED) != 0;
191     }
192
193     public static boolean isPackagePrivate(int value) {
194         return (value & (PUBLIC | PROTECTED | PRIVATE)) == 0;
195     }
196     public boolean isPackagePrivate() {
197         return (value & (PUBLIC | PROTECTED | PRIVATE)) == 0;
198     }
199
200     public static boolean isInterface(int value) {
201         return (value & INTERFACE) != 0;
202     }
203     public boolean isInterface() {
204         return (value & INTERFACE) != 0;
205     }
206
207     public static boolean isStatic(int value) {
208         return (value & STATIC) != 0;
209     }
210     public boolean isStatic() {
211         return (value & STATIC) != 0;
212     }
213     public static boolean isAbstract(int value) {
214         return (value & ABSTRACT) != 0;
215     }
216     public boolean isAbstract() {
217         return (value & ABSTRACT) != 0;
218     }
219     public static boolean isStrict(int value) {
220         return (value & STRICT) != 0;
221     }
222     public boolean isStrict() {
223         return (value & STRICT) != 0;
224     }
225
226     public static boolean isPrivileged(int value) {
227         return (value & PRIVILEGED) != 0;
228     }
229     public boolean isPrivileged() {
230         return (value & PRIVILEGED) != 0;
231     }
232
233     public boolean isSynchronized() {
234         return (value & SYNCHRONIZED) != 0;
235     }
236
237     public boolean isNative() {
238         return (value & NATIVE) != 0;
239     }
240
241     public boolean isTransient() {
242         return (value & TRANSIENT) != 0;
243     }
244
245     public boolean isVolatile() {
246         return (value & VOLATILE) != 0;
247     }
248
249     protected void setAttribute(int mask, boolean set) {
250         if (set) value |= mask;
251         else value &= ~mask;
252     }
253
254     public void setPublic(boolean v) {
255         setAttribute(PRIVATE, false);
256         setAttribute(PROTECTED, false);
257         setAttribute(PUBLIC, v);
258     }
259     public void setProtected() {
260         setAttribute(PRIVATE, false);
261         setAttribute(PUBLIC, false);
262         setAttribute(PROTECTED, true);
263     }
264
265     public void setPrivate(boolean v) {
266         setAttribute(PROTECTED, false);
267         setAttribute(PUBLIC, false);
268         setAttribute(PRIVATE, v);
269     }
270     public void setInterface(boolean v) {
271         setAttribute(INTERFACE, v);
272     }
273     public void setFinal(boolean v) {
274         setAttribute(FINAL, v);
275     }
276     public void setAbstract(boolean v) {
277         setAttribute(ABSTRACT, v);
278     }
279     public void setStatic(boolean v) {
280         setAttribute(STATIC, v);
281     }
282     public void setStrict(boolean v) {
283         setAttribute(STRICT, v);
284     }
285     public void setSynchronized(boolean v) {
286         setAttribute(SYNCHRONIZED, v);
287     }
288
289     public boolean isWeakerThan(Modifiers other) {
290         if (isPublic()) return false;
291         if (isProtected()) return other.isPublic();
292         if (isPackagePrivate()) return other.isPublic() || other.isProtected();
293         if (isPrivate()) return !other.isPrivate();
294
295         //XXX this is probably an error
296
return false;
297     }
298
299
300     public boolean isMoreAccessibleThan(Modifiers other) {
301         if (isPublic()) return !other.isPublic();
302         if (isProtected()) return !(other.isPublic() || other.isProtected());
303         if (isPackagePrivate()) return other.isPrivate();
304         if (isPrivate()) return false;
305
306         //XXX this is probably an error
307
return false;
308     }
309
310     public int getFalseValue() { return 0; }
311
312     /** Returns an integer that is acceptable to store as a
313         classfile's access flags. Only modifiers of TypeDecs will go
314         through this. This strips out any value disallowed by the JVM
315         spec, and turns protected modifiers (which may occur on inner
316         classes) public. */

317     public int getAcceptableClassValue() {
318         int val = getValue() & (PUBLIC | FINAL | INTERFACE | ABSTRACT);
319         if (isProtected()) val |= PUBLIC;
320         if (isInterface()) {
321             val &= PUBLIC;
322             val |= ABSTRACT | INTERFACE;
323         } else {
324             val |= 0x0020; // ACC_SUPER
325
}
326         return val;
327     }
328
329     /** Returns an integer that is acceptable to store in the
330         InnerClass attribute of a classfile. Only modifiers of
331         TypeDecs will go through this. This strips out any value
332         disallowed by the JVM. */

333     public int getAcceptableInnerClassValue() {
334         int val = getValue() &
335             (PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | INTERFACE | ABSTRACT);
336         if (isInterface()) {
337             val |= STATIC | ABSTRACT;
338         }
339 // else {
340
// val |= 0x0020; // ACC_SUPER
341
// }
342
return val;
343     }
344
345     /** Returns an integer that only encapsulates the access
346         modifiers. That is, only PUBLIC, PRIVATE, or PROTECTED. */

347     public int getAccessValue() {
348         return getValue() & (PUBLIC | PRIVATE | PROTECTED);
349     }
350
351     public String JavaDoc getAccessValueString() {
352         if (isPublic()) return "public";
353         else if (isPrivate()) return "private";
354         else if (isProtected()) return "protected";
355         else return "package";
356     }
357
358     public void checkIllegalCombination(int c1, int c2) {
359         if ((getValue() & c1) != 0) {
360             if ((getValue() & c2) != 0) {
361                 showError("illegal combination of modifiers: " +
362                           toString(c1, false) + toString(c2, true));
363             }
364         }
365     }
366
367     public void checkSpec() {
368         checkIllegalCombination(PUBLIC, PRIVATE);
369         checkIllegalCombination(PUBLIC, PROTECTED);
370         checkIllegalCombination(PROTECTED, PRIVATE);
371         //checkIllegalCombination(ABSTRACT, PRIVATE); handle in MethodDec
372
//checkIllegalCombination(ABSTRACT, STATIC); handle in MethodDec
373
checkIllegalCombination(ABSTRACT, FINAL);
374         checkIllegalCombination(ABSTRACT, NATIVE);
375         //checkIllegalCombination(ABSTRACT, STRICT); handle in MethodDec
376
checkIllegalCombination(ABSTRACT, SYNCHRONIZED);
377
378         checkIllegalCombination(NATIVE, STRICT);
379         
380         checkIllegalCombination(VOLATILE, FINAL);
381     }
382
383
384     //!!! strange definition of matching here...
385
public boolean matches(Modifiers otherModifiers) {
386         // a match is when other modifiers define everything that this does
387
// they're also allowed to define more
388
return (value & otherModifiers.getValue()) == value;
389     }
390
391     public String JavaDoc toShortString() {
392         return toString();
393     }
394
395     public void unparse(CodeWriter writer) throws IOException JavaDoc {
396         //XXX JJH - this reliance on getParent() for unparsing makes me uncomfortable
397
//XXX JJH - we should revisit this some time
398
String JavaDoc modText;
399         if (getParent() instanceof ConstructorDec) {
400             modText = toString(CONSTRUCTOR_PRINT_MASK,false);
401         }
402         else if (getParent() instanceof MethodDec &&
403                  getParent().getParent().getParent() instanceof TypeDec &&
404                  !((TypeDec)(getParent().getParent().getParent())).isConcrete()
405                  ) {
406             modText = toString(INTERFACE_METHOD_PRINT_MASK,false);
407         }
408         else {
409             modText = toString(DEFAULT_PRINT_MASK,false);
410         }
411         if (modText.length() > 0) writer.write(modText);
412     }
413
414     public String JavaDoc toString() {
415         return toString(DEFAULT_PRINT_MASK,true);
416     }
417
418     private String JavaDoc toString(int mask, boolean trim) {
419         int val = value & mask;
420         if (val == 0) return "";
421         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
422
423         if ((val & PUBLIC) != 0) sb.append("public ");
424         else if ((val & PRIVATE) != 0) sb.append("private ");
425         else if ((val & PROTECTED) != 0) sb.append("protected ");
426
427         /* Canonical order */
428         if ((val & STATIC) != 0) sb.append("static ");
429         if ((val & ABSTRACT) != 0) sb.append("abstract ");
430         if ((val & FINAL) != 0) sb.append("final ");
431         if ((val & TRANSIENT) != 0) sb.append("transient ");
432         if ((val & VOLATILE) != 0) sb.append("volatile ");
433         if ((val & NATIVE) != 0) sb.append("native ");
434         if ((val & SYNCHRONIZED) != 0) sb.append("synchronized ");
435
436         if ((val & STRICT) != 0 && (val & (ABSTRACT|NATIVE)) == 0) {
437             sb.append("strictfp ");
438         }
439
440         String JavaDoc s = sb.toString();
441         if (trim) return s.substring(0, s.length()-1);
442         return s;
443     }
444
445     //BEGIN: Generated from @child and @property
446
protected int value;
447     public int getValue() { return value; }
448     public void setValue(int _value) { value = _value; }
449
450     public Modifiers(SourceLocation location, int _value) {
451         super(location);
452         setValue(_value);
453     }
454     protected Modifiers(SourceLocation source) {
455         super(source);
456     }
457
458     public ASTObject copyWalk(CopyWalker walker) {
459         Modifiers ret = new Modifiers(getSourceLocation());
460         ret.preCopy(walker, this);
461         ret.value = value;
462         return ret;
463     }
464
465
466     public String JavaDoc getDefaultDisplayName() {
467         return "Modifiers(value: "+value+")";
468     }
469
470     //END: Generated from @child and @property
471
}
472
Popular Tags