KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > reflect > declaration > CtElementImpl


1 package spoon.support.reflect.declaration;
2
3 import java.io.Serializable JavaDoc;
4 import java.lang.annotation.Annotation JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Set JavaDoc;
7 import java.util.TreeSet JavaDoc;
8
9 import spoon.reflect.Factory;
10 import spoon.reflect.declaration.CtAnnotation;
11 import spoon.reflect.declaration.CtElement;
12 import spoon.reflect.declaration.SourcePosition;
13 import spoon.reflect.reference.CtTypeReference;
14 import spoon.reflect.visitor.Filter;
15 import spoon.reflect.visitor.JavaPrettyPrinter;
16 import spoon.reflect.visitor.Query;
17 import spoon.support.query.AnnotationFilter;
18 import spoon.support.visitor.ElementReplacer;
19 import spoon.support.visitor.SignaturePrinter;
20
21 /**
22  * The implementation for {@link spoon.test.spoon.reflect.Declaration}.
23  *
24  * @author Renaud Pawlak
25  * @see spoon.test.spoon.reflect.Declaration
26  */

27 public abstract class CtElementImpl implements CtElement, Serializable JavaDoc {
28
29     transient Factory factory;
30
31     public Factory getFactory() {
32         return factory;
33     }
34
35     public void setFactory(Factory factory) {
36         this.factory = factory;
37     }
38
39     Set JavaDoc<CtAnnotation<? extends Annotation JavaDoc>> annotations = new TreeSet JavaDoc<CtAnnotation<? extends Annotation JavaDoc>>();
40
41     String JavaDoc docComment;
42
43     CtElement parent;
44
45     SourcePosition position;
46
47     public CtElementImpl() {
48         super();
49     }
50
51     public int compareTo(CtElement o) {
52         SignaturePrinter pr = new SignaturePrinter();
53         pr.scan(this);
54         String JavaDoc current = pr.getSignature();
55         pr.reset();
56         pr.scan(o);
57         String JavaDoc other = pr.getSignature();
58         if (current.length() <= 0 || other.length() <= 0)
59             throw new ClassCastException JavaDoc("Unable to compare elements");
60         return current.compareTo(other);
61     }
62
63     @SuppressWarnings JavaDoc("unchecked")
64     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationType) {
65         for (CtAnnotation<? extends Annotation JavaDoc> a : getAnnotations()) {
66             if (a.getAnnotationType().toString().equals(
67                     annotationType.getName())) {
68                 return ((CtAnnotation<A>) a).getActualAnnotation();
69             }
70         }
71         return null;
72     }
73
74     // TODO remove the warning
75
@SuppressWarnings JavaDoc("unchecked")
76     public <A extends Annotation JavaDoc> CtAnnotation<A> getAnnotation(
77             CtTypeReference<A> annotationType) {
78         for (CtAnnotation<? extends Annotation JavaDoc> a : getAnnotations()) {
79             if (a.getAnnotationType().equals(annotationType)) {
80                 return (CtAnnotation<A>) a;
81             }
82         }
83         return null;
84     }
85
86     public Set JavaDoc<CtAnnotation<? extends Annotation JavaDoc>> getAnnotations() {
87         return annotations;
88     }
89
90     public String JavaDoc getDocComment() {
91         return docComment;
92     }
93
94     public CtElement getParent() {
95         return parent;
96     }
97
98     @SuppressWarnings JavaDoc("unchecked")
99     public <P extends CtElement> P getParent(Class JavaDoc<P> parentType) {
100         if (getParent() == null)
101             return null;
102         if (parentType.isAssignableFrom(getParent().getClass()))
103             return (P) getParent();
104         return getParent().getParent(parentType);
105     }
106
107     public boolean hasParent(CtElement candidate) {
108         if (getParent() == null)
109             return false;
110         if (getParent() == candidate)
111             return true;
112         return getParent().hasParent(candidate);
113     }
114
115     public SourcePosition getPosition() {
116         return position;
117     }
118
119     @Override JavaDoc
120     public int hashCode() {
121         SignaturePrinter pr = new SignaturePrinter();
122         pr.scan(this);
123         return pr.getSignature().hashCode();
124     }
125
126     public void replace(CtElement element) {
127         ElementReplacer<CtElement> translator = new ElementReplacer<CtElement>(
128                 this, element);
129         getParent().accept(translator);
130     }
131
132     public void replace(Filter<? extends CtElement> replacementPoints,
133             CtElement element) {
134         List JavaDoc<? extends CtElement> l = Query
135                 .getElements(this, replacementPoints);
136         for (CtElement e : l) {
137             e.replace(element);
138         }
139     }
140
141     public void setAnnotations(
142             Set JavaDoc<CtAnnotation<? extends Annotation JavaDoc>> annotations) {
143         this.annotations = annotations;
144     }
145
146     public void setDocComment(String JavaDoc docComment) {
147         this.docComment = docComment;
148     }
149
150     public void setParent(CtElement parentElement) {
151         this.parent = parentElement;
152     }
153
154     public void setPosition(SourcePosition position) {
155         this.position = position;
156     }
157
158     @Override JavaDoc
159     public String JavaDoc toString() {
160         JavaPrettyPrinter printer = new JavaPrettyPrinter();
161         printer.scan(this);
162         return printer.toString();
163     }
164
165     @SuppressWarnings JavaDoc("unchecked")
166     public <E extends CtElement> List JavaDoc<E> getAnnotatedChildren(
167             Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
168         return Query.getElements(this, new AnnotationFilter(CtElement.class,
169                 annotationType));
170     }
171 }
Popular Tags