KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > factory > AnnotationFactory


1 package spoon.reflect.factory;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.List JavaDoc;
8
9 import spoon.reflect.Factory;
10 import spoon.reflect.declaration.CtAnnotation;
11 import spoon.reflect.declaration.CtAnnotationType;
12 import spoon.reflect.declaration.CtElement;
13 import spoon.reflect.declaration.CtField;
14 import spoon.reflect.declaration.CtPackage;
15 import spoon.reflect.reference.CtArrayTypeReference;
16
17 /**
18  * The {@link CtAnnotationType} sub-factory.
19  */

20 public class AnnotationFactory extends TypeFactory {
21
22     private static final long serialVersionUID = 1L;
23
24     /**
25      * Creates an annotation sub-factory.
26      *
27      * @param factory
28      * the parent factory
29      */

30     public AnnotationFactory(Factory factory) {
31         super(factory);
32     }
33
34     /**
35      * Creates an annotation type.
36      *
37      * @param owner
38      * the package of the annotation type
39      * @param simpleName
40      * the name of annotation
41      */

42     public <T extends Annotation JavaDoc> CtAnnotationType create(CtPackage owner,
43             String JavaDoc simpleName) {
44         CtAnnotationType<T> t = factory.Core().createAnnotationType();
45         t.setSimpleName(simpleName);
46         owner.getTypes().add(t);
47         t.setParent(owner);
48         return t;
49     }
50
51     /**
52      * Creates an annotation type.
53      *
54      * @param qualifiedName
55      * the fully qualified name of the annotation type.
56      */

57     public CtAnnotationType create(String JavaDoc qualifiedName) {
58         return create(factory.Package().getOrCreate(getPackageName(qualifiedName)),
59                 getSimpleName(qualifiedName));
60     }
61
62     /**
63      * Gets a annotation type from its name.
64      */

65     @SuppressWarnings JavaDoc("unchecked")
66     public <T extends Annotation JavaDoc> CtAnnotationType<T> get(String JavaDoc qualifiedName) {
67         try {
68             return (CtAnnotationType) super.get(qualifiedName);
69         } catch (Exception JavaDoc e) {
70             return null;
71         }
72     }
73
74     /**
75      * Creates/updates an element's annotation value.
76      *
77      * @param element
78      * the program element to annotate
79      * @param annotationType
80      * the annotation type
81      * @param annotationElementName
82      * the annotation element name
83      * @param value
84      * the value of the annotation element
85      * @return the created/updated annotation
86      */

87     @SuppressWarnings JavaDoc("unchecked")
88     public CtAnnotation annotate(CtElement element,
89             Class JavaDoc<? extends Annotation JavaDoc> annotationType,
90             String JavaDoc annotationElementName, Object JavaDoc value) {
91         CtAnnotation annotation = element.getAnnotation(factory.Type()
92                 .createReference(annotationType));
93         if (annotation == null) {
94             annotation = factory.Core().createAnnotation();
95             annotation.setAnnotationType(factory.Type().createReference(
96                     annotationType));
97             element.getAnnotations().add(annotation);
98             annotation.setParent(element);
99         }
100         boolean isArray;
101
102         // try with CT reflexion
103
CtAnnotationType annotationtype = ((CtAnnotationType) annotation
104                 .getAnnotationType().getDeclaration());
105         if (annotationtype != null) {
106             CtField e = annotationtype.getField(annotationElementName);
107             isArray = (e.getType() instanceof CtArrayTypeReference);
108         } else {
109             Method JavaDoc m = null;
110             try {
111                 m = annotation.getAnnotationType().getActualClass().getMethod(
112                         annotationElementName, new Class JavaDoc[0]);
113             } catch (Exception JavaDoc ex) {
114                 throw new RuntimeException JavaDoc("undefined element '"
115                         + annotationElementName + "' for annotation '"
116                         + annotationType.getName() + "'");
117             }
118             isArray = m.getReturnType().isArray();
119         }
120         if (isArray == ((value instanceof Collection JavaDoc) || value.getClass()
121                 .isArray())) {
122             if (value.getClass().isArray()) {
123                 value = Arrays.asList(value);
124             }
125             annotation.getElementValues().put(annotationElementName, value);
126         } else {
127             if (isArray) {
128                 ((List JavaDoc) annotation.getElementValue(annotationElementName))
129                         .add(value);
130             } else {
131                 throw new RuntimeException JavaDoc(
132                         "cannot assing an array to a non-array annotation element");
133             }
134
135         }
136         return annotation;
137     }
138
139     /**
140      * Adds an annotation to an element.
141      *
142      * @param element
143      * the program element to annotate
144      * @param annotationType
145      * the annotation type
146      * @return the concerned annotation
147      */

148     public <A extends Annotation JavaDoc> CtAnnotation<A> annotate(CtElement element,
149             Class JavaDoc<A> annotationType) {
150         CtAnnotation<A> annotation = element.getAnnotation(factory.Type()
151                 .createReference(annotationType));
152         if (annotation == null) {
153             annotation = factory.Core().createAnnotation();
154             annotation.setAnnotationType(factory.Type().createReference(
155                     annotationType));
156             element.getAnnotations().add(annotation);
157             annotation.setParent(element);
158         }
159         return annotation;
160     }
161
162 }
163
Popular Tags