KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.support.reflect.declaration;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.lang.annotation.Inherited JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.TreeSet JavaDoc;
9
10 import spoon.reflect.declaration.CtAnnotation;
11 import spoon.reflect.declaration.CtAnonymousExecutable;
12 import spoon.reflect.declaration.CtClass;
13 import spoon.reflect.declaration.CtConstructor;
14 import spoon.reflect.declaration.CtField;
15 import spoon.reflect.declaration.CtMethod;
16 import spoon.reflect.declaration.CtSimpleType;
17 import spoon.reflect.declaration.CtType;
18 import spoon.reflect.reference.CtTypeReference;
19 import spoon.reflect.visitor.CtVisitor;
20
21 /**
22  * The implementation for {@link spoon.reflect.declaration.CtClass}.
23  *
24  * @author Renaud Pawlak
25  */

26 public class CtClassImpl<T extends Object JavaDoc> extends CtTypeImpl<T> implements
27         CtClass<T> {
28     private static final long serialVersionUID = 1L;
29
30     List JavaDoc<CtAnonymousExecutable> anonymousExecutable = new ArrayList JavaDoc<CtAnonymousExecutable>();
31
32     Set JavaDoc<CtConstructor> constructors = new TreeSet JavaDoc<CtConstructor>();
33
34     CtTypeReference superClass;
35
36     public void accept(CtVisitor v) {
37         v.visitCtClass(this);
38     }
39
40 // @Override
41
// public List<CtField<?>> getAllFields() {
42
// if (getSuperclass() != null && getSuperclass().getDeclaration() != null) {
43
// List<CtField<?>> fields = new ArrayList<CtField<?>>();
44
// fields.addAll(getSuperclass().getDeclaration().getAllFields());
45
// fields.addAll(getFields());
46
// return fields;
47
// }
48
// return super.getAllFields();
49
// }
50

51     public Set JavaDoc<CtMethod<?>> getAllMethods() {
52         Set JavaDoc<CtMethod<?>> ret = new TreeSet JavaDoc<CtMethod<?>>();
53         ret.addAll(getMethods());
54
55         if (getSuperclass() != null && getSuperclass().getDeclaration() != null) {
56             CtType<?> t = (CtType<?>) getSuperclass().getDeclaration();
57             ret.addAll(t.getMethods());
58         }
59         return ret;
60     }
61
62     public List JavaDoc<CtAnonymousExecutable> getAnonymousExecutables() {
63         return anonymousExecutable;
64     }
65
66     public CtConstructor<T> getConstructor(CtTypeReference<?>... parameterTypes) {
67         for (CtConstructor<T> c : constructors) {
68             boolean cont = c.getParameters().size() == parameterTypes.length;
69             for (int i = 0; cont && i < c.getParameters().size()
70                     && i < parameterTypes.length; i++) {
71                 if (!c.getParameters().get(i).getType().getQualifiedName()
72                         .equals(parameterTypes[i].getQualifiedName())) {
73                     cont = false;
74                 }
75             }
76             if (cont) {
77                 return c;
78             }
79         }
80         return null;
81     }
82
83     public Set JavaDoc<CtConstructor> getConstructors() {
84         return constructors;
85     }
86
87     @Override JavaDoc
88     @SuppressWarnings JavaDoc("unchecked")
89     public List JavaDoc<CtField<?>> getFields() {
90         return (List JavaDoc<CtField<?>>) super.getFields();
91     }
92
93     public CtTypeReference<?> getSuperclass() {
94         return superClass;
95     }
96
97     public void setAnonymousExecutables(List JavaDoc<CtAnonymousExecutable> e) {
98         anonymousExecutable.clear();
99         anonymousExecutable.addAll(e);
100     }
101
102     public void setConstructors(Set JavaDoc<CtConstructor> constructors) {
103         this.constructors = constructors;
104     }
105
106     public void setSuperclass(CtTypeReference<?> superClass) {
107         this.superClass = superClass;
108     }
109
110     @Override JavaDoc
111     public Set JavaDoc<CtAnnotation<? extends Annotation JavaDoc>> getAnnotations() {
112         Set JavaDoc<CtAnnotation<? extends Annotation JavaDoc>> annot = super.getAnnotations();
113
114         if (getSuperclass() != null) {
115             CtSimpleType sup = getSuperclass().getDeclaration();
116             if (sup != null) {
117                 for (CtAnnotation<? extends Annotation JavaDoc> a : sup
118                         .getAnnotations()) {
119                     if (a.getAnnotationType().getAnnotation(Inherited JavaDoc.class) != null) {
120                         annot.add(a);
121                     }
122                 }
123             }
124         }
125         return annot;
126     }
127
128     public boolean isSubtypeOf(CtTypeReference<?> type) {
129         if (getSuperclass() != null && getSuperclass().isSubtypeOf(type))
130             return true;
131         for (CtTypeReference<?> ref : getSuperInterfaces()) {
132             if (ref.isSubtypeOf(type))
133                 return true;
134         }
135         return false;
136     }
137 }
138
Popular Tags