KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > reflect > reference > CtFieldReferenceImpl


1 package spoon.support.reflect.reference;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.lang.reflect.Member JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.TreeSet JavaDoc;
9
10 import spoon.reflect.declaration.CtField;
11 import spoon.reflect.declaration.CtSimpleType;
12 import spoon.reflect.declaration.CtVariable;
13 import spoon.reflect.declaration.ModifierKind;
14 import spoon.reflect.reference.CtFieldReference;
15 import spoon.reflect.reference.CtTypeReference;
16 import spoon.reflect.visitor.CtVisitor;
17 import spoon.support.util.RtHelper;
18
19 public class CtFieldReferenceImpl<T> extends CtVariableReferenceImpl<T>
20         implements CtFieldReference<T> {
21     private static final long serialVersionUID = 1L;
22
23     CtTypeReference<?> declaringType;
24
25     boolean fina = false;
26
27     boolean stat = false;
28     
29
30     public CtFieldReferenceImpl() {
31         super();
32     }
33
34     public void accept(CtVisitor visitor) {
35         visitor.visitCtFieldReference(this);
36     }
37
38     public Member JavaDoc getActualField() {
39         try {
40         if(getDeclaringType().getActualClass().isAnnotation()){
41             return getDeclaringType().getActualClass().getDeclaredMethod(getSimpleName());
42         }else{
43             return getDeclaringType().getActualClass().getDeclaredField(
44                     getSimpleName());
45         }
46         } catch (Exception JavaDoc e) {
47             e.printStackTrace();
48         }
49         return null;
50     }
51
52     @Override JavaDoc
53     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationType) {
54         A annotation = super.getAnnotation(annotationType);
55         if (annotation != null)
56             return annotation;
57         // use reflection
58
Class JavaDoc<?> c = getDeclaringType().getActualClass();
59         if(c.isAnnotation()){
60             for(Method JavaDoc m : RtHelper.getAllMethods(c)){
61                 if (!this.getSimpleName().equals(m.getName()))
62                     continue;
63                 m.setAccessible(true);
64                 return m.getAnnotation(annotationType);
65             }
66         }else{
67         for (Field JavaDoc f : RtHelper.getAllFields(c)) {
68             if (!this.getSimpleName().equals(f.getName()))
69                 continue;
70             f.setAccessible(true);
71             return f.getAnnotation(annotationType);
72         }
73         }
74         return null;
75     }
76
77     @Override JavaDoc
78     public Annotation JavaDoc[] getAnnotations() {
79         Annotation JavaDoc[] annotations = super.getAnnotations();
80         if (annotations != null) {
81             return annotations;
82         } else {
83             // use reflection
84
Class JavaDoc<?> c = getDeclaringType().getActualClass();
85             for (Field JavaDoc f : RtHelper.getAllFields(c)) {
86                 if (!this.getSimpleName().equals(f.getName()))
87                     continue;
88                 f.setAccessible(true);
89                 return f.getAnnotations();
90             }
91             //If the fields belong to an annotation type, they are actually methods
92
for(Method JavaDoc m: RtHelper.getAllMethods(c)){
93                 if(!this.getSimpleName().equals(m.getName()))
94                     continue;
95                 m.setAccessible(true);
96                 return m.getAnnotations();
97             }
98             return null;
99         }
100     }
101
102     @SuppressWarnings JavaDoc("unchecked")
103     public CtField<T> getDeclaration() {
104         CtSimpleType<?> type = declaringType.getDeclaration();
105         if (declaringType != null && type != null) {
106             return (CtField<T>) type.getField(getSimpleName());
107         } else {
108             return null;
109         }
110     }
111
112     public CtTypeReference<?> getDeclaringType() {
113         return declaringType;
114     }
115
116     public String JavaDoc getQualifiedName() {
117         return getDeclaringType().getQualifiedName() + "#" + getSimpleName();
118     }
119
120     public boolean isFinal() {
121         return fina;
122     }
123
124     /**
125      * Tells if the referenced field is static.
126      */

127     public boolean isStatic() {
128         return stat;
129     }
130
131     public void setDeclaringType(CtTypeReference<?> declaringType) {
132         this.declaringType = declaringType;
133     }
134
135     public void setFinal(boolean b) {
136         fina = b;
137     }
138
139     public void setStatic(boolean stat) {
140         this.stat = stat;
141     }
142
143     public Set JavaDoc<ModifierKind> getModifiers() {
144         CtVariable v=getDeclaration();
145         if(v!=null) {
146             return v.getModifiers();
147         } else {
148             Member JavaDoc m=getActualField();
149             if(m!=null) {
150                 return RtHelper.getModifiers(m.getModifiers());
151             } else {
152                 return new TreeSet JavaDoc<ModifierKind>();
153             }
154         }
155     }
156
157 }
158
Popular Tags