001: package net.sourceforge.jrefactory.ast;
002:
003: import net.sourceforge.jrefactory.parser.JavaParser;
004:
005: /**
006: * This class is the base for all Nodes that contain modifiers ("public", "native", etc.).
007: *
008: * @author Mike Atkinson
009: * @since jRefactory 2.9.0, created October 16, 2003
010: */
011: public class AccessNode extends SimpleNode implements ModifierHolder {
012:
013: /** Description of the Field */
014: protected int modifiers = 0;
015: /** Description of the Field */
016: public final static int ALPHABETICAL_ORDER = 1;
017: /** Description of the Field */
018: public final static int STANDARD_ORDER = 2;
019:
020: /**
021: * Constructor for the AccessNode object.
022: *
023: * Access nodes are not designed to be constructed directly.
024: *
025: * @param parser Description of Parameter
026: * @param identifier Description of Parameter
027: */
028: public AccessNode(JavaParser parser, int identifier) {
029: super (parser, identifier);
030: }
031:
032: /**
033: * Constructor for the AccessNode object
034: *
035: * Access nodes are not designed to be constructed directly.
036: *
037: * @param identifier Description of Parameter
038: */
039: public AccessNode(int identifier) {
040: super (identifier);
041: }
042:
043: /**
044: * Sets the private bit in the modifiers
045: *
046: * @param value <code>true</code> if we are setting the private modifier
047: */
048: public void setPrivate(boolean value) {
049: setCode(value, PRIVATE);
050: }
051:
052: /** Sets the private bit (to <code>true</code>) in the modifiers */
053: public void setPrivate() {
054: modifiers = modifiers | PRIVATE;
055: }
056:
057: /**
058: * Sets the protected bit in the modifiers
059: *
060: * @param value <code>true</code> if we are setting the protected modifier
061: */
062: public void setProtected(boolean value) {
063: setCode(value, PROTECTED);
064: }
065:
066: /** Sets the protected bit (to <code>true</code>) in the modifiers */
067: public void setProtected() {
068: modifiers = modifiers | PROTECTED;
069: }
070:
071: /**
072: * Sets the public bit in the modifiers
073: *
074: * @param value <code>true</code> if we are setting the public modifier
075: */
076: public void setPublic(boolean value) {
077: setCode(value, PUBLIC);
078: }
079:
080: /** Sets the public bit (to <code>true</code>) in the modifiers */
081: public void setPublic() {
082: modifiers = modifiers | PUBLIC;
083: }
084:
085: /**
086: * Sets the abstract bit in the modifiers
087: *
088: * @param value <code>true</code> if we are setting the modifier
089: */
090: public void setAbstract(boolean value) {
091: setCode(value, ABSTRACT);
092: }
093:
094: /** Sets the abstract bit (to <code>true</code>) in the modifiers */
095: public void setAbstract() {
096: modifiers = modifiers | ABSTRACT;
097: }
098:
099: /**
100: * Sets the Synchronized bit in the modifiers
101: *
102: * @param value The new Synchronized value
103: */
104: public void setSynchronized(boolean value) {
105: setCode(value, SYNCHRONIZED);
106: }
107:
108: /** Sets the Synchronized bit (to <code>true</code>) in the modifiers */
109: public void setSynchronized() {
110: modifiers = modifiers | SYNCHRONIZED;
111: }
112:
113: /**
114: * Sets the Static bit in the modifiers
115: *
116: * @param value The new Static value
117: */
118: public void setStatic(boolean value) {
119: setCode(value, STATIC);
120: }
121:
122: /** Sets the Static bit (to <code>true</code>) in the modifiers */
123: public void setStatic() {
124: modifiers = modifiers | STATIC;
125: }
126:
127: /** Sets the Final bit (to <code>true</code>) of the in the modifiers */
128: public void setFinal() {
129: modifiers = modifiers | FINAL;
130: }
131:
132: /** Sets the Final bit (to <code>true</code>) of the in the modifiers */
133: public void setVolatile() {
134: modifiers = modifiers | VOLATILE;
135: }
136:
137: /** Sets the Final bit (to <code>true</code>) of the in the modifiers */
138: public void setTransient() {
139: modifiers = modifiers | TRANSIENT;
140: }
141:
142: /** Sets the Final bit (to <code>true</code>) of the in the modifiers */
143: public void setNative() {
144: modifiers = modifiers | NATIVE;
145: }
146:
147: /** Sets the Final bit (to <code>true</code>) of the in the modifiers */
148: public void setInterface() {
149: modifiers = modifiers | INTERFACE;
150: }
151:
152: /** Sets the StrictFP bit (to <code>true</code>) of the in the modifiers */
153: public void setStrict() {
154: modifiers = modifiers | STRICTFP;
155: }
156:
157: /**
158: * Sets the modifier bits
159: *
160: * @param modifiers the modifier bits
161: */
162: public void setModifiers(int modifiers) {
163: this .modifiers = modifiers;
164: }
165:
166: /**
167: * Determine if the node is abstract
168: *
169: * @return <code>true</code> if this stores an ABSTRACT flag
170: */
171: public boolean isAbstract() {
172: return ((modifiers & ABSTRACT) != 0);
173: }
174:
175: /**
176: * Determine if the node is explicit
177: *
178: * @return <code>true</code> if this stores an EXPLICIT flag
179: */
180: public boolean isExplicit() {
181: return ((modifiers & EXPLICIT) != 0);
182: }
183:
184: /**
185: * Determine if the node is final
186: *
187: * @return <code>true</code> if this stores an FINAL flag
188: */
189: public boolean isFinal() {
190: return ((modifiers & FINAL) != 0);
191: }
192:
193: /**
194: * Determine if the node is interface
195: *
196: * @return <code>true</code> if this stores an INTERFACE flag
197: */
198: public boolean isInterface() {
199: return ((modifiers & INTERFACE) != 0);
200: }
201:
202: /**
203: * Determine if the node is native
204: *
205: * @return <code>true</code> if this stores an NATIVE flag
206: */
207: public boolean isNative() {
208: return ((modifiers & NATIVE) != 0);
209: }
210:
211: /**
212: * Determine if the node is private
213: *
214: * @return <code>true</code> if this stores an PRIVATE flag
215: */
216: public boolean isPrivate() {
217: return ((modifiers & PRIVATE) != 0);
218: }
219:
220: /**
221: * Determine if the node is protected
222: *
223: * @return <code>true</code> if this stores an PROTECTED flag
224: */
225: public boolean isProtected() {
226: return ((modifiers & PROTECTED) != 0);
227: }
228:
229: /**
230: * Determine if the node is public
231: *
232: * @return <code>true</code> if this stores an PUBLIC flag
233: */
234: public boolean isPublic() {
235: return ((modifiers & PUBLIC) != 0);
236: }
237:
238: /**
239: * Determine if the node is static
240: *
241: * @return <code>true</code> if this stores an static flag
242: */
243: public boolean isStatic() {
244: return ((modifiers & STATIC) != 0);
245: }
246:
247: ///**
248: // * Determine if the node is strict
249: // *
250: // *@return <code>true</code> if this stores an STRICT flag
251: // */
252: //public boolean isStrict() {
253: // return ((modifiers & STRICT) != 0);
254: //}
255:
256: /**
257: * Determine if the node is strictFP
258: *
259: * @return <code>true</code> if this stores an STRICTFP flag
260: */
261: public boolean isStrictFP() {
262: return ((modifiers & STRICTFP) != 0);
263: }
264:
265: /**
266: * Determine if the node is synchronized
267: *
268: * @return <code>true</code> if this stores an SYNCHRONIZED flag
269: */
270: public boolean isSynchronized() {
271: return ((modifiers & SYNCHRONIZED) != 0);
272: }
273:
274: /**
275: * Determine if the node is transient
276: *
277: * @return <code>true</code> if this stores an TRANSIENT flag
278: */
279: public boolean isTransient() {
280: return ((modifiers & TRANSIENT) != 0);
281: }
282:
283: /**
284: * Determine if the node is volatile
285: *
286: * @return <code>true</code> if this stores an VOLATILE flag
287: */
288: public boolean isVolatile() {
289: return ((modifiers & VOLATILE) != 0);
290: }
291:
292: /**
293: * Determines if this has package scope
294: *
295: * @return <code>true</code> if this has package scope
296: */
297: public boolean isPackage() {
298: return !isPublic() && !isProtected() && !isPrivate();
299: }
300:
301: /**
302: * Gets the modifier bits
303: *
304: * @return the modifier bits
305: */
306: public int getModifiers() {
307: return modifiers;
308: }
309:
310: /**
311: * Returns a string containing all the modifiers
312: *
313: * @param code the code used to determine the order of the modifiers
314: * @return the string representationof the order
315: */
316: public String getModifiersString(int code) {
317: if (code == ALPHABETICAL_ORDER) {
318: return toStringAlphabetical();
319: } else {
320: return toStandardOrderString();
321: }
322: }
323:
324: /**
325: * Add a modifier
326: *
327: * @param mod the new modifier
328: */
329: public void addModifier(String mod) {
330: if ((mod == null) || (mod.length() == 0)) {
331: // Nothing to add
332: return;
333: } else if (mod.equalsIgnoreCase(names[0])) {
334: modifiers = modifiers | ABSTRACT;
335: } else if (mod.equalsIgnoreCase(names[1])) {
336: modifiers = modifiers | EXPLICIT;
337: } else if (mod.equalsIgnoreCase(names[2])) {
338: modifiers = modifiers | FINAL;
339: } else if (mod.equalsIgnoreCase(names[3])) {
340: modifiers = modifiers | INTERFACE;
341: } else if (mod.equalsIgnoreCase(names[4])) {
342: modifiers = modifiers | NATIVE;
343: } else if (mod.equalsIgnoreCase(names[5])) {
344: modifiers = modifiers | PRIVATE;
345: } else if (mod.equalsIgnoreCase(names[6])) {
346: modifiers = modifiers | PROTECTED;
347: } else if (mod.equalsIgnoreCase(names[7])) {
348: modifiers = modifiers | PUBLIC;
349: } else if (mod.equalsIgnoreCase(names[8])) {
350: modifiers = modifiers | STATIC;
351: //} else if (mod.equalsIgnoreCase(names[9])) {
352: // modifiers = modifiers | STRICT;
353: } else if (mod.equalsIgnoreCase(names[10])) {
354: modifiers = modifiers | STRICTFP;
355: } else if (mod.equalsIgnoreCase(names[11])) {
356: modifiers = modifiers | SYNCHRONIZED;
357: } else if (mod.equalsIgnoreCase(names[12])) {
358: modifiers = modifiers | TRANSIENT;
359: } else if (mod.equalsIgnoreCase(names[13])) {
360: modifiers = modifiers | VOLATILE;
361: }
362: }
363:
364: /**
365: * Convert the node to a string
366: *
367: * @return a string describing the modifiers
368: */
369: public String toStringAlphabetical() {
370: // Local Variables
371: StringBuffer buf = new StringBuffer();
372:
373: // Protection first
374: if (isPrivate()) {
375: buf.append(names[5]);
376: buf.append(" ");
377: }
378: if (isProtected()) {
379: buf.append(names[6]);
380: buf.append(" ");
381: }
382: if (isPublic()) {
383: buf.append(names[7]);
384: buf.append(" ");
385: }
386:
387: // Others next
388: if (isAbstract()) {
389: buf.append(names[0]);
390: buf.append(" ");
391: }
392: if (isExplicit()) {
393: buf.append(names[1]);
394: buf.append(" ");
395: }
396: if (isFinal()) {
397: buf.append(names[2]);
398: buf.append(" ");
399: }
400: if (isInterface()) {
401: buf.append(names[3]);
402: buf.append(" ");
403: }
404: if (isNative()) {
405: buf.append(names[4]);
406: buf.append(" ");
407: }
408: if (isStatic()) {
409: buf.append(names[8]);
410: buf.append(" ");
411: }
412: //if (isStrict()) {
413: // buf.append(names[9]);
414: // buf.append(" ");
415: //}
416: if (isStrictFP()) {
417: buf.append(names[10]);
418: buf.append(" ");
419: }
420: if (isSynchronized()) {
421: buf.append(names[11]);
422: buf.append(" ");
423: }
424: if (isTransient()) {
425: buf.append(names[12]);
426: buf.append(" ");
427: }
428: if (isVolatile()) {
429: buf.append(names[13]);
430: buf.append(" ");
431: }
432:
433: return buf.toString();
434: }
435:
436: /**
437: * Convert the node to a string
438: *
439: * @return a string describing the modifiers
440: */
441: public String toStandardOrderString() {
442: // Local Variables
443: StringBuffer buf = new StringBuffer();
444:
445: // Protection first
446: if (isPrivate()) {
447: buf.append(names[5]);
448: buf.append(" ");
449: }
450: if (isProtected()) {
451: buf.append(names[6]);
452: buf.append(" ");
453: }
454: if (isPublic()) {
455: buf.append(names[7]);
456: buf.append(" ");
457: }
458:
459: // Others next
460: if (isAbstract()) {
461: buf.append(names[0]);
462: buf.append(" ");
463: }
464: if (isExplicit()) {
465: buf.append(names[1]);
466: buf.append(" ");
467: }
468: if (isInterface()) {
469: buf.append(names[3]);
470: buf.append(" ");
471: }
472: if (isStatic()) {
473: buf.append(names[8]);
474: buf.append(" ");
475: }
476: //if (isStrict()) {
477: // buf.append(names[9]);
478: // buf.append(" ");
479: //}
480: if (isFinal()) {
481: buf.append(names[2]);
482: buf.append(" ");
483: }
484: if (isSynchronized()) {
485: buf.append(names[11]);
486: buf.append(" ");
487: }
488: if (isTransient()) {
489: buf.append(names[12]);
490: buf.append(" ");
491: }
492: if (isVolatile()) {
493: buf.append(names[13]);
494: buf.append(" ");
495: }
496: if (isNative()) {
497: buf.append(names[4]);
498: buf.append(" ");
499: }
500: if (isStrictFP()) {
501: buf.append(names[10]);
502: buf.append(" ");
503: }
504:
505: return buf.toString();
506: }
507:
508: /**
509: * Copies the modifiers from another source
510: *
511: * @param source the source
512: */
513: public void copyModifiers(ModifierHolder source) {
514: modifiers = source.getModifiers();
515: }
516:
517: /**
518: * Compare the modifiers of these two nodes.
519: *
520: * @param obj the other node.
521: * @return <code>true</code> if they are the same
522: */
523: public boolean equalModifiers(Object obj) {
524: if (obj instanceof ModifierHolder) {
525: ModifierHolder other = (ModifierHolder) obj;
526: return other.getModifiers() == modifiers;
527: }
528: return false;
529: }
530:
531: /**
532: * Sets or resets a single bit in the modifiers
533: *
534: * @param value <code>true</code> if we are setting the bit
535: * @param code The new Code value
536: */
537: protected void setCode(boolean value, int code) {
538: if (value) {
539: modifiers = modifiers | code;
540: } else {
541: modifiers = modifiers & (~code);
542: }
543: }
544:
545: /**
546: * Description of the Method
547: *
548: * @return Description of the Returned Value
549: */
550: protected String printModifiers() {
551: return (modifiers == 0) ? ""
552: : (" (" + toStandardOrderString() + ")");
553: }
554:
555: public int skipAnnotations() {
556: int childNo = 0;
557: Node child = jjtGetFirstChild();
558: while (child instanceof ASTAnnotation) {
559: child = jjtGetChild(++childNo);
560: }
561: return childNo;
562: }
563:
564: public int skipAnnotationsAndTypeParameters() {
565: int childNo = 0;
566: Node child = jjtGetFirstChild();
567: while (child instanceof ASTAnnotation) {
568: child = jjtGetChild(++childNo);
569: }
570: if (child instanceof ASTTypeParameters) {
571: childNo++;// skip possible type parameters
572: }
573: return childNo;
574: }
575:
576: }
|