add annotation descriptor. type descriptor is responsible for keeping corresponding...
authoryeom <yeom>
Tue, 1 Mar 2011 22:49:11 +0000 (22:49 +0000)
committeryeom <yeom>
Tue, 1 Mar 2011 22:49:11 +0000 (22:49 +0000)
Robust/src/IR/AnnotationDescriptor.java [new file with mode: 0644]
Robust/src/IR/Tree/AnnotationNode.java [deleted file]
Robust/src/IR/Tree/BuildIR.java
Robust/src/IR/Tree/Modifiers.java
Robust/src/IR/TypeDescriptor.java

diff --git a/Robust/src/IR/AnnotationDescriptor.java b/Robust/src/IR/AnnotationDescriptor.java
new file mode 100644 (file)
index 0000000..f831378
--- /dev/null
@@ -0,0 +1,63 @@
+package IR;
+
+import java.util.Set;
+
+public class AnnotationDescriptor extends Descriptor {
+
+  public static final int MARKER_ANNOTATION = 1;
+  public static final int SINGLE_ANNOTATION = 2;
+  public static final int FULL_ANNOTATION = 3;
+
+  private String marker;
+  private Set arrayedValues; // for single annotation
+  private int type;
+
+  public AnnotationDescriptor(String annotationName) {
+    // constructor for marker annotation
+    super(annotationName);
+    this.marker = annotationName;
+    this.type = MARKER_ANNOTATION;
+  }
+
+  public int getType() {
+    return type;
+  }
+
+  public boolean isMarkerAnnotation() {
+    return type == MARKER_ANNOTATION;
+  }
+
+  public boolean isSingleAnnotation() {
+    return type == SINGLE_ANNOTATION;
+  }
+
+  public boolean isFullAnnotation() {
+    return type == FULL_ANNOTATION;
+  }
+
+  public String getMarker() {
+    return marker;
+  }
+
+  public boolean equals(Object o) {
+    if (o instanceof AnnotationDescriptor) {
+      AnnotationDescriptor a = (AnnotationDescriptor) o;
+      if (a.getType() != type)
+        return false;
+      if (!a.getMarker().equals(getMarker()))
+        return false;
+
+      return true;
+    }
+    return false;
+  }
+
+  public String toString() {
+    if (type == MARKER_ANNOTATION) {
+      return "@" + name;
+    } else {
+      return "@" + name + "()";
+    }
+  }
+
+}
diff --git a/Robust/src/IR/Tree/AnnotationNode.java b/Robust/src/IR/Tree/AnnotationNode.java
deleted file mode 100644 (file)
index b0622ca..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-package IR.Tree;
-
-public class AnnotationNode extends TreeNode {
-  //currently it only supports marker annotation that have no variables.
-  String name;
-  
-  public AnnotationNode(String name){
-    //constructor for marker annotation
-    this.name=name;
-  }
-  
-}
index 9a20ec9a550527b0942857cbcbe27f314b503192..5634955bb07586082302e90436a7cb9997cfab26 100644 (file)
@@ -618,6 +618,7 @@ public class BuildIR {
 
     ParseNode tn=pn.getChild("type");
     TypeDescriptor t=parseTypeDescriptor(tn);
+    assignAnnotationsToType(m,t);
     ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
     ParseNodeVector pnv=vn.getChildren();
     boolean isglobal=pn.getChild("global")!=null;
@@ -675,6 +676,15 @@ public class BuildIR {
       cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
     }
   }
+  
+  private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type){
+    Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
+    for(int i=0; i<annotations.size(); i++) {
+      // it only supports a marker annotation
+      AnnotationDescriptor an=annotations.elementAt(i);
+      type.addAnnotationMarker(an);           
+    }    
+  }
 
   private ExpressionNode parseExpression(ParseNode pn) {
     if (isNode(pn,"assignment"))
@@ -1068,12 +1078,12 @@ public class BuildIR {
       blockstatements.add(new TagDeclarationNode(name, type));
     } else if (isNode(pn,"local_variable_declaration")) {
       
-      ParseNode mn=pn.getChild("modifiers");
+      ParseNode mn=pn.getChild("modifiers");         
+      TypeDescriptor t=parseTypeDescriptor(pn);
       if(mn!=null){
         Modifiers m=parseModifiersList(mn);
-        // TODO: add annotations to corresponding descriptor
-      }      
-      TypeDescriptor t=parseTypeDescriptor(pn);
+        assignAnnotationsToType(m, t);        
+      }   
       ParseNode vn=pn.getChild("variable_declarators_list");
       ParseNodeVector pnv=vn.getChildren();
       for(int i=0; i<pnv.size(); i++) {
@@ -1082,6 +1092,7 @@ public class BuildIR {
 
        ParseNode tmp=vardecl;
        TypeDescriptor arrayt=t;
+
        while (tmp.getChild("single")==null) {
          arrayt=arrayt.makeArray(state);
          tmp=tmp.getChild("array");
@@ -1332,7 +1343,7 @@ public class BuildIR {
         if(isNode(body_list,"annotation_body")){
           ParseNode body_node=body_list.getFirstChild();
           if (isNode(body_node,"marker_annotation")){          
-            m.addAnnotation(new AnnotationNode(body_node.getChild("name").getTerminal()));
+            m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
           }else if(isNode(body_node,"single_annotation")){
             throw new Error("Annotation with single piece of data is not supported yet.");
           } else if(isNode(body_node,"normal_annotation")){
index fa35937472a8f6b0b26f8f878f3b3a4eedf84a63..ae7cb303ab8072075c0889647d71bfbd38e399bc 100644 (file)
@@ -2,6 +2,8 @@ package IR.Tree;
 
 import java.util.Vector;
 
+import IR.AnnotationDescriptor;
+
 public class Modifiers {
   public static final int PUBLIC=1;
   public static final int PROTECTED=2;
@@ -18,21 +20,25 @@ public class Modifiers {
 
   // java annotation can be intermixed freely with modifiers
   // so Modifiers  maintains the list of annotations for later usage
-  Vector<AnnotationNode> annotations;  
+  Vector<AnnotationDescriptor> annotations;  
   private int value;
 
   public Modifiers() {
     value=0;
-    annotations=new Vector<AnnotationNode>();
+    annotations=new Vector<AnnotationDescriptor>();
   }
 
   public Modifiers(int v) {
     value=v;
   }
   
-  public void addAnnotation(AnnotationNode an){
+  public void addAnnotation(AnnotationDescriptor an){
     annotations.add(an);
   }
+  
+  public Vector<AnnotationDescriptor> getAnnotations(){
+    return annotations;
+  }
 
   public void addModifier(int mod) {
     value|=mod;
index cb67bb9308be3f332b37d19b7536910cc97f4d35..a678640d74f492f413f5549e5e0cfd2ce0adfee4 100644 (file)
@@ -1,5 +1,8 @@
 package IR;
 
+import java.util.HashSet;
+import java.util.Set;
+
 /**
  * Descriptor
  *
@@ -26,6 +29,8 @@ public class TypeDescriptor extends Descriptor {
   private int type;
   ClassDescriptor class_desc;
   boolean isClassNameRef = false;
+  
+  private HashSet<AnnotationDescriptor> annotationSet;
 
   public boolean equals(Object o) {
     if (o instanceof TypeDescriptor) {
@@ -280,6 +285,7 @@ public class TypeDescriptor extends Descriptor {
     this.class_desc=null;
     this.arraycount=0;
     this.isClassNameRef =false;
+    this.annotationSet=new HashSet<AnnotationDescriptor>();
   }
 
   public TypeDescriptor(String st) {
@@ -288,6 +294,7 @@ public class TypeDescriptor extends Descriptor {
     this.class_desc=null;
     this.arraycount=0;
     this.isClassNameRef =false;
+    this.annotationSet=new HashSet<AnnotationDescriptor>();
   }
 
   public ClassDescriptor getClassDesc() {
@@ -300,6 +307,7 @@ public class TypeDescriptor extends Descriptor {
     this.class_desc=cd;
     this.arraycount=0;
     this.isClassNameRef =false;
+    this.annotationSet=new HashSet<AnnotationDescriptor>();
   }
 
   public TypeDescriptor(int t) {
@@ -307,6 +315,7 @@ public class TypeDescriptor extends Descriptor {
     this.type=t;
     this.arraycount=0;
     this.isClassNameRef =false;
+    this.annotationSet=new HashSet<AnnotationDescriptor>();
   }
 
   public String toString() {
@@ -353,4 +362,13 @@ public class TypeDescriptor extends Descriptor {
       return "offset";
     else throw new Error();
   }
+  
+  public void addAnnotationMarker(AnnotationDescriptor an){
+    annotationSet.add(an);
+  }
+  
+  public Set getAnnotationMarkerSet(){
+    return annotationSet;
+  }
+  
 }