Annotations.jrag 4.04 KB
Newer Older
1
aspect Annotations {
2
    syn boolean TypeInstance.hasAnnotations() = getAnnotations().getNumAnnotation()>0;
3
    syn boolean TypeInstance.hasIntentions() = ! intentionSet().isEmpty();
4

5
6
7
8
9
10
11
12
13
14
15
    syn String Annotations.getName() = new String(lookup(""));

    syn byte[] Annotations.lookup(String key) {
        for(Annotation a: getAnnotations()) {
            byte[] res = a.lookup(key);
            if(res != null) return res;
        }
        return null;
    }

    syn byte[] Annotation.lookup(String key) = (getKey().equals(key) ? getValue() : null);
16

17
18
19
    syn boolean Annotation.isIntention() = false;
    eq Intention.isIntention() = true;

20
21
    inh TypeInstance Annotation.parentInstance();
    eq TypeInstance.getAnnotations().parentInstance() = this;
22

23
    coll Set<Intention> TypeInstance.intentionSet() [new HashSet<Intention>()] with add;
24
    Intention contributes this
25
        to TypeInstance.intentionSet()
26
27
28
29
        for parentInstance();

}

30
31
32
33
34
35
36
37
38
aspect SigAnnotations {

   inh Decl TypeInstance.parentDecl();

   coll Set Decl.allAnnotations() [new HashSet()] with add;
   TypeInstance contributes getAnnotationString()
           to Decl.allAnnotations()
           for parentDecl();

39
40
41
42
43
44
45
46
47
48
49
50
   // Helper attribute to get the "outermost" intentions for Decls

   syn byte[] TypeInstance.intentionBytes() = getIntentionBytes(sortedIntentions());
   syn byte[] Decl.getIntentionBytes() = getTypeInstance().intentionBytes();

    static Comparator TypeInstance.intentionComp =
        new Comparator<Intention>() {
            public int compare(Intention i1, Intention i2) {
                    return i1.getKey().compareTo(i2.getKey());
            }
    };

51
52
    syn int Decl.getNumIntentions() = getTypeInstance().sortedIntentions().getNumChild();

53
54
55
56
57
58
59
60
61
62
63
64
65
66
   syn List<Intention> TypeInstance.sortedIntentions() {
       List<Intention> res = new List<Intention>();

       //TODO: refactor out creation of sorted list of intentions

       java.util.ArrayList<Intention> sorted = new ArrayList(intentionSet());
       java.util.Collections.sort(sorted, intentionComp);
       for(Intention i : sorted) {
           res.add(i);
       }
       return res;
   }


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
   public DocString.DocString(byte[] bs) {
           super("DOCSTRING", bs);
   }

   public DocString.DocString(String s) {
           super("DOCSTRING", s.getBytes());
   }

   public String Intention.toString() {
       return "("+getKey() + ":"+new String(getValue())+")";
   }
   public String  DocString.toString() {
           return "\""+new String(getValue())+"\"";
   }

    /// TESTING
    syn String Decl.getAnnotationString()  {
            StringBuilder sb = new StringBuilder();
            Iterator<String> iti = allAnnotations().iterator();
            while(iti.hasNext()) {
                    //Annotation i = iti.next();
                    //sb.append("("+i.getKey()+" : "+i.getValue()+") ");
                    String i = iti.next();
                    sb.append(i);
            }
            return sb.toString();
    }


    syn int TypeInstance.fooHash() {
97
            List<Annotation> ints = getAnnotations().getAnnotationList();
98
99
100
101
102
103
104
105
106
107
108
            int result=0;
            for(Annotation i : ints) {
                    if(i.isIntention()) {
                        result += i.toString().hashCode();
                    }
            }
            return result;
    }

    syn String TypeInstance.getAnnotationString() {
            StringBuilder sb = new StringBuilder();
109
            List<Annotation> ints = getAnnotations().getAnnotationList();
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            for(Annotation i : ints) {
                    sb.append(i.toString());
            }
            return sb.toString();
    }

    public void Decl.debugAnnotations() {
            getTypeInstance().debugAnnotations(getName());
    }

    public void TypeInstance.debugAnnotations(String context) {
            if(hasAnnotations()){
                System.out.println(context+".annotations: " + fooHash() + " : " + getAnnotationString());
            } else {
                //System.out.println(context + " : " + fooHash() + " : " + ": NO ANNOTATIONS ");
            }
    }
    //  TESTING END

}