1 package spoon.reflect.factory; 2 3 import java.lang.annotation.Annotation ; 4 import java.lang.reflect.Method ; 5 import java.util.Arrays ; 6 import java.util.Collection ; 7 import java.util.List ; 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 20 public class AnnotationFactory extends TypeFactory { 21 22 private static final long serialVersionUID = 1L; 23 24 30 public AnnotationFactory(Factory factory) { 31 super(factory); 32 } 33 34 42 public <T extends Annotation > CtAnnotationType create(CtPackage owner, 43 String 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 57 public CtAnnotationType create(String qualifiedName) { 58 return create(factory.Package().getOrCreate(getPackageName(qualifiedName)), 59 getSimpleName(qualifiedName)); 60 } 61 62 65 @SuppressWarnings ("unchecked") 66 public <T extends Annotation > CtAnnotationType<T> get(String qualifiedName) { 67 try { 68 return (CtAnnotationType) super.get(qualifiedName); 69 } catch (Exception e) { 70 return null; 71 } 72 } 73 74 87 @SuppressWarnings ("unchecked") 88 public CtAnnotation annotate(CtElement element, 89 Class <? extends Annotation > annotationType, 90 String annotationElementName, Object 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 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 m = null; 110 try { 111 m = annotation.getAnnotationType().getActualClass().getMethod( 112 annotationElementName, new Class [0]); 113 } catch (Exception ex) { 114 throw new RuntimeException ("undefined element '" 115 + annotationElementName + "' for annotation '" 116 + annotationType.getName() + "'"); 117 } 118 isArray = m.getReturnType().isArray(); 119 } 120 if (isArray == ((value instanceof Collection ) || 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 ) annotation.getElementValue(annotationElementName)) 129 .add(value); 130 } else { 131 throw new RuntimeException ( 132 "cannot assing an array to a non-array annotation element"); 133 } 134 135 } 136 return annotation; 137 } 138 139 148 public <A extends Annotation > CtAnnotation<A> annotate(CtElement element, 149 Class <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 |