1 22 package org.jboss.reflect.plugins.javassist; 23 24 import java.lang.annotation.Inherited ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import javassist.CtClass; 29 30 import org.jboss.reflect.plugins.AnnotationHelper; 31 import org.jboss.reflect.spi.AnnotationValue; 32 33 38 public abstract class JavassistInheritableAnnotationHolder extends JavassistAnnotatedInfo 39 { 40 41 private static final String INHERITED_NAME = Inherited .class.getName(); 43 44 protected Map <String , AnnotationValue> allAnnotations; 45 46 47 protected AnnotationValue[] allAnnotationsArray = NOT_CONFIGURED; 48 49 protected CtClass ctClass; 50 51 public JavassistInheritableAnnotationHolder(CtClass ctClass, AnnotationHelper annotationHelper) 52 { 53 super(annotationHelper); 54 this.ctClass = ctClass; 55 } 56 57 public AnnotationValue[] getAnnotations() 58 { 59 if (allAnnotationsArray == NOT_CONFIGURED) 60 setupAnnotations(annotationHelper.getAnnotations(ctClass)); 61 return allAnnotationsArray; 62 } 63 64 protected AnnotationValue[] getAnnotations(Object obj) 65 { 66 synchronized (this) 67 { 68 if (allAnnotationsArray == NOT_CONFIGURED) 69 { 70 allAnnotationsArray = annotationHelper.getAnnotations(obj); 71 setupAnnotations(allAnnotationsArray); 72 73 } 74 } 75 return allAnnotationsArray; 76 } 77 78 public AnnotationValue getAnnotation(String name) 79 { 80 getAnnotations(); 81 return allAnnotations.get(name); 82 } 83 84 public boolean isAnnotationPresent(String name) 85 { 86 getAnnotations(); 87 return allAnnotations.containsKey(name); 88 } 89 90 91 96 public void setupAnnotations(AnnotationValue[] annotations) 97 { 98 JavassistInheritableAnnotationHolder superHolder = getSuperHolder(); 99 AnnotationValue[] superAllAnnotations = (superHolder != null) ? superHolder.getAnnotations() : null; 100 101 if (annotations != null && annotations.length > 0) 102 { 103 annotationMap = new HashMap <String , AnnotationValue>(); 104 annotationsArray = annotations; 105 for (int i = 0; i < annotations.length; i++) 106 { 107 annotationMap.put(annotations[i].getAnnotationType().getName(), annotations[i]); 108 } 109 allAnnotations = new HashMap <String , AnnotationValue>(); 110 111 if (superHolder != null && superAllAnnotations != null && superAllAnnotations.length != 0) 112 { 113 for (int i = 0; i < superAllAnnotations.length; i++) 114 { 115 AnnotationValue av = superAllAnnotations[i]; 116 if (av.getAnnotationType().isAnnotationPresent(INHERITED_NAME)) 117 { 118 allAnnotations.put(av.getAnnotationType().getName(), av); 119 } 120 } 121 } 122 else 123 allAnnotationsArray = annotationsArray; 124 125 for (int i = 0; i < annotations.length; i++) 126 allAnnotations.put(annotations[i].getAnnotationType().getName(), annotations[i]); 127 128 allAnnotationsArray = allAnnotations.values().toArray(new AnnotationValue[allAnnotations.size()]); 129 } 130 else 131 { 132 if (superHolder != null) 133 { 134 allAnnotations = superHolder.getAllAnnotations(); 135 allAnnotationsArray = superAllAnnotations; 136 } 137 else 138 { 139 allAnnotations = new HashMap <String , AnnotationValue>(); 140 } 141 } 142 } 143 144 149 protected Map <String , AnnotationValue> getAllAnnotations() 150 { 151 if (allAnnotations == null) 152 setupAnnotations(annotationHelper.getAnnotations(ctClass)); 153 return allAnnotations; 154 } 155 156 157 public abstract JavassistInheritableAnnotationHolder getSuperHolder(); 158 } 159 | Popular Tags |