001    /* This file was generated with JastAdd2 (http://jastadd.org) version R20130213 */
002    package AST;
003    
004    import java.util.HashSet;
005    import java.io.File;
006    import java.util.*;
007    import beaver.*;
008    import java.util.ArrayList;
009    import java.util.zip.*;
010    import java.io.*;
011    import java.io.FileNotFoundException;
012    import java.util.Collection;
013    
014    /**
015     * @production Annotation : {@link Modifier} ::= <span class="component">&lt;ID:String&gt;</span> <span class="component">{@link Access}</span> <span class="component">{@link ElementValuePair}*</span>;
016     * @ast node
017     * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.ast:6
018     */
019    public class Annotation extends Modifier implements Cloneable {
020      /**
021       * @apilevel low-level
022       */
023      public void flushCache() {
024      }
025      /**
026       * @apilevel internal
027       */
028      public void flushCollectionCache() {
029      }
030      /**
031       * @apilevel internal
032       */
033      @SuppressWarnings({"unchecked", "cast"})
034      public Annotation clone() throws CloneNotSupportedException {
035        Annotation node = (Annotation)super.clone();
036        node.decl_computed = false;
037        node.decl_value = null;
038        node.in$Circle(false);
039        node.is$Final(false);
040        return node;
041      }
042    /**
043     * @apilevel internal
044     */
045      @SuppressWarnings({"unchecked", "cast"})
046    public Annotation copy() {
047      
048      try {
049        Annotation node = (Annotation) clone();
050        node.parent = null;
051        if(children != null)
052          node.children = (ASTNode[]) children.clone();
053        
054        return node;
055      } catch (CloneNotSupportedException e) {
056        throw new Error("Error: clone not supported for " + getClass().getName());
057      }
058      
059    }/**
060     * Create a deep copy of the AST subtree at this node.
061     * The copy is dangling, i.e. has no parent.
062     * @return dangling copy of the subtree at this node
063     * @apilevel low-level
064     */
065      @SuppressWarnings({"unchecked", "cast"})
066    public Annotation fullCopy() {
067      
068      Annotation tree = (Annotation) copy();
069      if (children != null) {
070        for (int i = 0; i < children.length; ++i) {
071          
072          ASTNode child = (ASTNode) children[i];
073          if(child != null) {
074            child = child.fullCopy();
075            tree.setChild(child, i);
076          }
077        }
078      }
079      return tree;
080      
081    }  /**
082       * @ast method 
083       * @aspect Annotations
084       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:41
085       */
086      public void checkModifiers() {
087        super.checkModifiers();
088        if(decl() instanceof AnnotationDecl) {
089          AnnotationDecl T = (AnnotationDecl)decl();
090          Annotation m = T.annotation(lookupType("java.lang.annotation", "Target"));
091          if(m != null && m.getNumElementValuePair() == 1 && m.getElementValuePair(0).getName().equals("value")) {
092            ElementValue v = m.getElementValuePair(0).getElementValue();
093            //System.out.println("ElementValue: \n" + v.dumpTree());
094            //System.out.println("Annotation: \n" + dumpTree());
095            if(!v.validTarget(this))
096              error("annotation type " + T.typeName() + " is not applicable to this kind of declaration");
097          }
098        }
099      }
100      /**
101       * @ast method 
102       * @aspect Annotations
103       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:383
104       */
105      public void typeCheck() {
106        if(!decl().isAnnotationDecl()) {
107          /* TypeName names the annotation type corresponding to the annotation. It is a
108          compile-time error if TypeName does not name an annotation type.*/
109          if(!decl().isUnknown())
110            error(decl().typeName() + " is not an annotation type");
111        } else {
112          TypeDecl typeDecl = decl();
113          /* It is a compile-time error if a declaration is annotated with more than one
114          annotation for a given annotation type.*/
115          if(lookupAnnotation(typeDecl) != this)
116            error("duplicate annotation " + typeDecl.typeName());
117          /* Annotations must contain an element-value pair for every element of the
118          corresponding annotation type, except for those elements with default
119          values, or a compile-time error occurs. Annotations may, but are not
120          required to, contain element-value pairs for elements with default values.*/
121          for(int i = 0; i < typeDecl.getNumBodyDecl(); i++) {
122            if(typeDecl.getBodyDecl(i) instanceof MethodDecl) {
123              MethodDecl decl = (MethodDecl)typeDecl.getBodyDecl(i);
124              if(elementValueFor(decl.name()) == null && (!(decl instanceof AnnotationMethodDecl) || !((AnnotationMethodDecl)decl).hasDefaultValue()))
125                error("missing value for " + decl.name());
126            }
127          }
128          /* The Identifier in an ElementValuePair must be the simple name of one of the
129          elements of the annotation type identified by TypeName in the containing
130          annotation. Otherwise, a compile-time error occurs. (In other words, the
131          identifier in an element-value pair must also be a method name in the interface
132          identified by TypeName.) */
133          for(int i = 0; i < getNumElementValuePair(); i++) {
134            ElementValuePair pair = getElementValuePair(i);
135            if(typeDecl.memberMethods(pair.getName()).isEmpty())
136              error("can not find element named " + pair.getName() + " in " + typeDecl.typeName());
137          }
138        }
139        checkOverride();
140      }
141      /**
142       * @ast method 
143       * @aspect Annotations
144       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:582
145       */
146      public void toString(StringBuffer s) {
147        s.append("@");
148        getAccess().toString(s);
149        s.append("(");
150        for(int i = 0; i < getNumElementValuePair(); i++) {
151          if(i != 0)
152            s.append(", ");
153          getElementValuePair(i).toString(s);
154        }
155        s.append(")");
156      }
157      /**
158       * @ast method 
159       * @aspect AnnotationsCodegen
160       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Backend/AnnotationsCodegen.jrag:155
161       */
162      public void appendAsAttributeTo(Attribute buf) {
163        ConstantPool cp = hostType().constantPool();
164        String typeDescriptor = decl().typeDescriptor();
165        int type_index = cp.addUtf8(typeDescriptor);
166        buf.u2(type_index);
167        buf.u2(getNumElementValuePair());
168        for(int i = 0; i < getNumElementValuePair(); i++) {
169          int name_index = cp.addUtf8(getElementValuePair(i).getName());
170          buf.u2(name_index);
171          getElementValuePair(i).getElementValue().appendAsAttributeTo(buf);
172        }
173      }
174      /**
175       * @ast method 
176       * 
177       */
178      public Annotation() {
179        super();
180    
181    
182      }
183      /**
184       * Initializes the child array to the correct size.
185       * Initializes List and Opt nta children.
186       * @apilevel internal
187       * @ast method
188       * @ast method 
189       * 
190       */
191      public void init$Children() {
192        children = new ASTNode[2];
193        setChild(new List(), 1);
194      }
195      /**
196       * @ast method 
197       * 
198       */
199      public Annotation(String p0, Access p1, List<ElementValuePair> p2) {
200        setID(p0);
201        setChild(p1, 0);
202        setChild(p2, 1);
203      }
204      /**
205       * @ast method 
206       * 
207       */
208      public Annotation(beaver.Symbol p0, Access p1, List<ElementValuePair> p2) {
209        setID(p0);
210        setChild(p1, 0);
211        setChild(p2, 1);
212      }
213      /**
214       * @apilevel low-level
215       * @ast method 
216       * 
217       */
218      protected int numChildren() {
219        return 2;
220      }
221      /**
222       * @apilevel internal
223       * @ast method 
224       * 
225       */
226      public boolean mayHaveRewrite() {
227        return false;
228      }
229      /**
230       * Replaces the lexeme ID.
231       * @param value The new value for the lexeme ID.
232       * @apilevel high-level
233       * @ast method 
234       * 
235       */
236      public void setID(String value) {
237        tokenString_ID = value;
238      }
239      /**
240       * JastAdd-internal setter for lexeme ID using the Beaver parser.
241       * @apilevel internal
242       * @ast method 
243       * 
244       */
245      public void setID(beaver.Symbol symbol) {
246        if(symbol.value != null && !(symbol.value instanceof String))
247          throw new UnsupportedOperationException("setID is only valid for String lexemes");
248        tokenString_ID = (String)symbol.value;
249        IDstart = symbol.getStart();
250        IDend = symbol.getEnd();
251      }
252      /**
253       * Retrieves the value for the lexeme ID.
254       * @return The value for the lexeme ID.
255       * @apilevel high-level
256       * @ast method 
257       * 
258       */
259      public String getID() {
260        return tokenString_ID != null ? tokenString_ID : "";
261      }
262      /**
263       * Replaces the Access child.
264       * @param node The new node to replace the Access child.
265       * @apilevel high-level
266       * @ast method 
267       * 
268       */
269      public void setAccess(Access node) {
270        setChild(node, 0);
271      }
272      /**
273       * Retrieves the Access child.
274       * @return The current node used as the Access child.
275       * @apilevel high-level
276       * @ast method 
277       * 
278       */
279      public Access getAccess() {
280        return (Access)getChild(0);
281      }
282      /**
283       * Retrieves the Access child.
284       * <p><em>This method does not invoke AST transformations.</em></p>
285       * @return The current node used as the Access child.
286       * @apilevel low-level
287       * @ast method 
288       * 
289       */
290      public Access getAccessNoTransform() {
291        return (Access)getChildNoTransform(0);
292      }
293      /**
294       * Replaces the ElementValuePair list.
295       * @param list The new list node to be used as the ElementValuePair list.
296       * @apilevel high-level
297       * @ast method 
298       * 
299       */
300      public void setElementValuePairList(List<ElementValuePair> list) {
301        setChild(list, 1);
302      }
303      /**
304       * Retrieves the number of children in the ElementValuePair list.
305       * @return Number of children in the ElementValuePair list.
306       * @apilevel high-level
307       * @ast method 
308       * 
309       */
310      public int getNumElementValuePair() {
311        return getElementValuePairList().getNumChild();
312      }
313      /**
314       * Retrieves the number of children in the ElementValuePair list.
315       * Calling this method will not trigger rewrites..
316       * @return Number of children in the ElementValuePair list.
317       * @apilevel low-level
318       * @ast method 
319       * 
320       */
321      public int getNumElementValuePairNoTransform() {
322        return getElementValuePairListNoTransform().getNumChildNoTransform();
323      }
324      /**
325       * Retrieves the element at index {@code i} in the ElementValuePair list..
326       * @param i Index of the element to return.
327       * @return The element at position {@code i} in the ElementValuePair list.
328       * @apilevel high-level
329       * @ast method 
330       * 
331       */
332      @SuppressWarnings({"unchecked", "cast"})
333      public ElementValuePair getElementValuePair(int i) {
334        return (ElementValuePair)getElementValuePairList().getChild(i);
335      }
336      /**
337       * Append an element to the ElementValuePair list.
338       * @param node The element to append to the ElementValuePair list.
339       * @apilevel high-level
340       * @ast method 
341       * 
342       */
343      public void addElementValuePair(ElementValuePair node) {
344        List<ElementValuePair> list = (parent == null || state == null) ? getElementValuePairListNoTransform() : getElementValuePairList();
345        list.addChild(node);
346      }
347      /**
348       * @apilevel low-level
349       * @ast method 
350       * 
351       */
352      public void addElementValuePairNoTransform(ElementValuePair node) {
353        List<ElementValuePair> list = getElementValuePairListNoTransform();
354        list.addChild(node);
355      }
356      /**
357       * Replaces the ElementValuePair list element at index {@code i} with the new node {@code node}.
358       * @param node The new node to replace the old list element.
359       * @param i The list index of the node to be replaced.
360       * @apilevel high-level
361       * @ast method 
362       * 
363       */
364      public void setElementValuePair(ElementValuePair node, int i) {
365        List<ElementValuePair> list = getElementValuePairList();
366        list.setChild(node, i);
367      }
368      /**
369       * Retrieves the ElementValuePair list.
370       * @return The node representing the ElementValuePair list.
371       * @apilevel high-level
372       * @ast method 
373       * 
374       */
375      public List<ElementValuePair> getElementValuePairs() {
376        return getElementValuePairList();
377      }
378      /**
379       * Retrieves the ElementValuePair list.
380       * <p><em>This method does not invoke AST transformations.</em></p>
381       * @return The node representing the ElementValuePair list.
382       * @apilevel low-level
383       * @ast method 
384       * 
385       */
386      public List<ElementValuePair> getElementValuePairsNoTransform() {
387        return getElementValuePairListNoTransform();
388      }
389      /**
390       * Retrieves the ElementValuePair list.
391       * @return The node representing the ElementValuePair list.
392       * @apilevel high-level
393       * @ast method 
394       * 
395       */
396      @SuppressWarnings({"unchecked", "cast"})
397      public List<ElementValuePair> getElementValuePairList() {
398        List<ElementValuePair> list = (List<ElementValuePair>)getChild(1);
399        list.getNumChild();
400        return list;
401      }
402      /**
403       * Retrieves the ElementValuePair list.
404       * <p><em>This method does not invoke AST transformations.</em></p>
405       * @return The node representing the ElementValuePair list.
406       * @apilevel low-level
407       * @ast method 
408       * 
409       */
410      @SuppressWarnings({"unchecked", "cast"})
411      public List<ElementValuePair> getElementValuePairListNoTransform() {
412        return (List<ElementValuePair>)getChildNoTransform(1);
413      }
414      /**
415       * @ast method 
416       * @aspect Annotations
417       * @declaredat /home/jesper/svn/JastAddJ/Java7Frontend/Override.jrag:20
418       */
419       
420       public void checkOverride() {
421               if (decl().fullName().equals("java.lang.Override") &&
422                               enclosingBodyDecl() instanceof MethodDecl) {
423    
424                       MethodDecl method = (MethodDecl)enclosingBodyDecl();
425                       TypeDecl host = method.hostType();
426                       SimpleSet ancestors = host.ancestorMethods(method.signature());
427                       boolean found = false;
428                       for (Iterator iter = ancestors.iterator(); iter.hasNext(); ) {
429                               MethodDecl decl = (MethodDecl)iter.next();
430                               if (method.overrides(decl)) {
431                                       found = true;
432                                       break;
433                               }
434                       }
435                       if (!found) {
436                               TypeDecl typeObject = lookupType("java.lang", "Object");
437                               SimpleSet overrides =
438                                       typeObject.localMethodsSignature(method.signature());
439                               if (overrides.isEmpty() ||
440                                               !((MethodDecl) overrides.iterator().next()).isPublic())
441                                       error("method does not override a method from a supertype");
442                       }
443               }
444       }
445      /**
446       * @apilevel internal
447       */
448      protected boolean decl_computed = false;
449      /**
450       * @apilevel internal
451       */
452      protected TypeDecl decl_value;
453      /**
454       * @attribute syn
455       * @aspect Annotations
456       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:420
457       */
458      @SuppressWarnings({"unchecked", "cast"})
459      public TypeDecl decl() {
460        if(decl_computed) {
461          return decl_value;
462        }
463          ASTNode$State state = state();
464      int num = state.boundariesCrossed;
465      boolean isFinal = this.is$Final();
466        decl_value = decl_compute();
467      if(isFinal && num == state().boundariesCrossed){ decl_computed = true; }
468            return decl_value;
469      }
470      /**
471       * @apilevel internal
472       */
473      private TypeDecl decl_compute() {  return getAccess().type();  }
474      /**
475       * @attribute syn
476       * @aspect Annotations
477       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:432
478       */
479      public ElementValue elementValueFor(String name) {
480        ASTNode$State state = state();
481        try {
482        for(int i = 0; i < getNumElementValuePair(); i++) {
483          ElementValuePair pair = getElementValuePair(i);
484          if(pair.getName().equals(name))
485            return pair.getElementValue();
486        }
487        return null;
488      }
489        finally {
490        }
491      }
492      /**
493       * @attribute syn
494       * @aspect Annotations
495       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:514
496       */
497      public TypeDecl type() {
498        ASTNode$State state = state();
499        try {  return getAccess().type();  }
500        finally {
501        }
502      }
503      /* An annotation on an annotation type declaration is known as a meta-annotation.
504      An annotation type may be used to annotate its own declaration. More generally,
505      circularities in the transitive closure of the "annotates" relation are
506      permitted. For example, it is legal to annotate an annotation type declaration
507      with another annotation type, and to annotate the latter type's declaration
508      with the former type. (The pre-defined meta-annotation types contain several
509      such circularities.)
510      Comment: no problems with reference attributes.
511      * @attribute syn
512       * @aspect Annotations
513       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:543
514       */
515      public boolean isMetaAnnotation() {
516        ASTNode$State state = state();
517        try {  return hostType().isAnnotationDecl();  }
518        finally {
519        }
520      }
521      /**
522       * @attribute syn
523       * @aspect AnnotationsCodegen
524       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Backend/AnnotationsCodegen.jrag:64
525       */
526      public boolean isRuntimeVisible() {
527        ASTNode$State state = state();
528        try {
529        Annotation a = decl().annotation(lookupType("java.lang.annotation", "Retention"));
530        if(a == null) return false;
531        ElementConstantValue value = (ElementConstantValue)a.getElementValuePair(0).getElementValue();
532        Variable v = value.getExpr().varDecl();
533        return v != null && v.name().equals("RUNTIME");
534      }
535        finally {
536        }
537      }
538      /**
539       * @attribute syn
540       * @aspect AnnotationsCodegen
541       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Backend/AnnotationsCodegen.jrag:74
542       */
543      public boolean isRuntimeInvisible() {
544        ASTNode$State state = state();
545        try {
546        Annotation a = decl().annotation(lookupType("java.lang.annotation", "Retention"));
547        if(a == null) return true; // default bahavior if not annotated
548        ElementConstantValue value = (ElementConstantValue)a.getElementValuePair(0).getElementValue();
549        Variable v = value.getExpr().varDecl();
550        return v != null &&  v.name().equals("CLASS");
551      }
552        finally {
553        }
554      }
555      /**
556       * @attribute inh
557       * @aspect Annotations
558       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:55
559       */
560      @SuppressWarnings({"unchecked", "cast"})
561      public TypeDecl lookupType(String packageName, String typeName) {
562          ASTNode$State state = state();
563        TypeDecl lookupType_String_String_value = getParent().Define_TypeDecl_lookupType(this, null, packageName, typeName);
564            return lookupType_String_String_value;
565      }
566      /**
567       * @attribute inh
568       * @aspect Annotations
569       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:69
570       */
571      @SuppressWarnings({"unchecked", "cast"})
572      public boolean mayUseAnnotationTarget(String name) {
573          ASTNode$State state = state();
574        boolean mayUseAnnotationTarget_String_value = getParent().Define_boolean_mayUseAnnotationTarget(this, null, name);
575            return mayUseAnnotationTarget_String_value;
576      }
577      /**
578       * @attribute inh
579       * @aspect Annotations
580       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:258
581       */
582      @SuppressWarnings({"unchecked", "cast"})
583      public BodyDecl enclosingBodyDecl() {
584          ASTNode$State state = state();
585        BodyDecl enclosingBodyDecl_value = getParent().Define_BodyDecl_enclosingBodyDecl(this, null);
586            return enclosingBodyDecl_value;
587      }
588      /**
589       * @attribute inh
590       * @aspect Annotations
591       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:422
592       */
593      @SuppressWarnings({"unchecked", "cast"})
594      public Annotation lookupAnnotation(TypeDecl typeDecl) {
595          ASTNode$State state = state();
596        Annotation lookupAnnotation_TypeDecl_value = getParent().Define_Annotation_lookupAnnotation(this, null, typeDecl);
597            return lookupAnnotation_TypeDecl_value;
598      }
599      /**
600       * @attribute inh
601       * @aspect Annotations
602       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:544
603       */
604      @SuppressWarnings({"unchecked", "cast"})
605      public TypeDecl hostType() {
606          ASTNode$State state = state();
607        TypeDecl hostType_value = getParent().Define_TypeDecl_hostType(this, null);
608            return hostType_value;
609      }
610      /**
611       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:462
612       * @apilevel internal
613       */
614      public TypeDecl Define_TypeDecl_enclosingAnnotationDecl(ASTNode caller, ASTNode child) {
615        if(caller == getElementValuePairListNoTransform())  {
616        int childIndex = caller.getIndexOfChild(child);
617        return decl();
618      }
619        else {      return getParent().Define_TypeDecl_enclosingAnnotationDecl(this, caller);
620        }
621      }
622      /**
623       * @declaredat /home/jesper/svn/JastAddJ/Java1.5Frontend/Annotations.jrag:549
624       * @apilevel internal
625       */
626      public NameType Define_NameType_nameType(ASTNode caller, ASTNode child) {
627        if(caller == getAccessNoTransform()) {
628          return NameType.TYPE_NAME;
629        }
630        else {      return getParent().Define_NameType_nameType(this, caller);
631        }
632      }
633      /**
634       * @apilevel internal
635       */
636      public ASTNode rewriteTo() {
637        return super.rewriteTo();
638      }
639    }