• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package annotations.el;
2 
3 import java.util.LinkedHashMap;
4 
5 import type.Type;
6 import annotations.io.ASTPath;
7 import annotations.util.coll.VivifyingMap;
8 
9 /**
10  * An {@link ATypeElement} that also stores an un-annotated type. This is useful for cast
11  * insertion or receiver insertion.
12  */
13 public class ATypeElementWithType extends ATypeElement {
14 
15     /**
16      * A map with {@link ATypeElementWithType}s as values. When
17      * {@link VivifyingMap#vivify(Object)} method is called, a new
18      * {@code ATypeElementWithType} is constructed with the parameter to
19      * {@code vivify} passed to {@code ATypeElementWithType}'s constructor. This
20      * parameter is also used as the key into the map. This is used to map
21      * {@link ASTPath}s to their corresponding {@code ATypeElementWithType}.
22      * <p>
23      * {@code ATEWT} stands for {@code ATypeElementWithType}.
24      */
newVivifyingLHMap_ATEWT()25     /*package-private*/ static <K extends Object> VivifyingMap<K, ATypeElementWithType> newVivifyingLHMap_ATEWT() {
26         return new VivifyingMap<K, ATypeElementWithType>(
27                 new LinkedHashMap<K, ATypeElementWithType>()) {
28             @Override
29             public  ATypeElementWithType createValueFor(K k) {
30                 return new ATypeElementWithType(k);
31             }
32 
33             @Override
34             public boolean subPrune(ATypeElementWithType v) {
35                 return v.prune();
36             }
37         };
38     }
39 
40     /**
41      * The un-annotated type.
42      */
43     private Type type;
44 
45     ATypeElementWithType(Object description) {
46         super(description);
47     }
48 
49     ATypeElementWithType(ATypeElementWithType elem) {
50         super(elem);
51         type = elem.type;
52     }
53 
54     @Override
55     public ATypeElementWithType clone() {
56         return new ATypeElementWithType(this);
57     }
58 
59     /**
60      * Gets the un-annotated type.
61      *
62      * @return the un-annotated type
63      */
64     public Type getType() {
65         return type;
66     }
67 
68     /**
69      * Sets the un-annotated type.
70      *
71      * @param type the un-annotated type
72      */
73     public void setType(Type type) {
74         this.type = type;
75     }
76 
77     @Override
78     public boolean equals(Object o) {
79         return o instanceof ATypeElementWithType
80             && ((ATypeElementWithType) o).equals(this);
81     }
82 
83     public boolean equalsTypeElementWithType(ATypeElementWithType o) {
84         return super.equals(o) && o.type.equals(type);
85     }
86 
87     @Override
88     public int hashCode() {
89         return super.hashCode() + type.hashCode();
90     }
91 
92     @Override
93     public boolean prune() {
94         boolean result = super.prune();
95         if (result && tlAnnotationsHere.isEmpty()) {
96             // If we're about to be pruned because we have no annotations, then
97             // stop the prune to just insert a cast with no annotations.
98             result = false;
99         }
100         return result;
101     }
102 
103     @Override
104     public String toString() {
105         StringBuilder sb = new StringBuilder();
106         sb.append("AInsertTypecastTypeElement: ");
107         sb.append('\t');
108         sb.append(super.toString());
109         sb.append("type: " + type);
110         return sb.toString();
111     }
112 
113     @Override
114     public <R, T> R accept(ElementVisitor<R, T> v, T t) {
115         return v.visitTypeElementWithType(this, t);
116     }
117 }
118