1 22 package org.jboss.reflect.plugins; 23 24 import java.io.Serializable ; 25 import java.lang.annotation.Inherited ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.jboss.reflect.spi.AnnotatedInfo; 31 import org.jboss.reflect.spi.AnnotationValue; 32 import org.jboss.util.JBossObject; 33 34 40 public abstract class InheritableAnnotationHolder extends JBossObject implements AnnotatedInfo, Serializable 41 { 42 43 private static final long serialVersionUID = 3257290210164289843L; 44 45 46 private static final String INHERITED_NAME = Inherited .class.getName(); 48 49 static final Map <String , AnnotationValue> UNKNOWN_ANNOTATIONS_MAP = Collections.unmodifiableMap(new HashMap <String , AnnotationValue>()); 50 51 52 static final AnnotationValue[] UNKNOWN_ANNOTATIONS = new AnnotationValue[0]; 53 54 55 protected Map <String , AnnotationValue> declaredAnnotations = UNKNOWN_ANNOTATIONS_MAP; 56 57 58 protected Map <String , AnnotationValue> allAnnotations = UNKNOWN_ANNOTATIONS_MAP; 59 60 61 protected AnnotationValue[] allAnnotationsArray = UNKNOWN_ANNOTATIONS; 62 63 64 protected AnnotationValue[] declaredAnnotationsArray = UNKNOWN_ANNOTATIONS; 65 66 67 protected Object annotatedElement; 68 69 70 protected AnnotationHelper annotationHelper; 71 72 75 public InheritableAnnotationHolder() 76 { 77 } 78 79 84 public void setAnnotationHelper(AnnotationHelper helper) 85 { 86 this.annotationHelper = helper; 87 } 88 89 public void setAnnotatedElement(Object annotatedElement) 90 { 91 this.annotatedElement = annotatedElement; 92 } 93 94 99 public AnnotationValue[] getDeclaredAnnotations() 100 { 101 if (declaredAnnotationsArray == UNKNOWN_ANNOTATIONS) 102 setupAnnotations(annotationHelper.getAnnotations(annotatedElement)); 103 return declaredAnnotationsArray; 104 } 105 106 public AnnotationValue[] getAnnotations() 107 { 108 if (allAnnotationsArray == UNKNOWN_ANNOTATIONS) 109 setupAnnotations(annotationHelper.getAnnotations(annotatedElement)); 110 return allAnnotationsArray; 111 } 112 113 public AnnotationValue getAnnotation(String name) 114 { 115 if (allAnnotations == UNKNOWN_ANNOTATIONS_MAP) 116 setupAnnotations(annotationHelper.getAnnotations(annotatedElement)); 117 return allAnnotations.get(name); 118 } 119 120 public boolean isAnnotationPresent(String name) 121 { 122 if (allAnnotations == UNKNOWN_ANNOTATIONS_MAP) 123 setupAnnotations(annotationHelper.getAnnotations(annotatedElement)); 124 return allAnnotations.containsKey(name); 125 } 126 127 132 public void setupAnnotations(AnnotationValue[] annotations) 133 { 134 InheritableAnnotationHolder superHolder = getSuperHolder(); 135 AnnotationValue[] superAllAnnotations = (superHolder != null) ? superHolder.getAnnotations() : null; 136 137 if (annotations != null && annotations.length > 0) 138 { 139 declaredAnnotations = new HashMap <String , AnnotationValue>(); 140 declaredAnnotationsArray = annotations; 141 for (int i = 0; i < annotations.length; i++) 142 declaredAnnotations.put(annotations[i].getAnnotationType().getName(), annotations[i]); 143 allAnnotations = new HashMap <String , AnnotationValue>(); 144 145 if (superHolder != null && superAllAnnotations != null && superAllAnnotations.length != 0) 146 { 147 for (int i = 0; i < superAllAnnotations.length; i++) 148 { 149 AnnotationValue av = superAllAnnotations[i]; 150 if (av.getAnnotationType().isAnnotationPresent(INHERITED_NAME)) 151 { 152 allAnnotations.put(av.getAnnotationType().getName(), av); 153 } 154 } 155 } 156 else 157 allAnnotationsArray = declaredAnnotationsArray; 158 159 for (int i = 0; i < annotations.length; i++) 160 allAnnotations.put(annotations[i].getAnnotationType().getName(), annotations[i]); 161 162 allAnnotationsArray = allAnnotations.values().toArray(new AnnotationValue[allAnnotations.size()]); 163 } 164 else 165 { 166 if (superHolder != null) 167 { 168 allAnnotations = superHolder.getAllAnnotations(); 169 allAnnotationsArray = superAllAnnotations; 170 } 171 } 172 } 173 174 179 protected Map <String , AnnotationValue> getAllAnnotations() 180 { 181 if (allAnnotations == UNKNOWN_ANNOTATIONS_MAP) 182 setupAnnotations(annotationHelper.getAnnotations(annotatedElement)); 183 return allAnnotations; 184 } 185 186 191 protected abstract InheritableAnnotationHolder getSuperHolder(); 192 } 193 | Popular Tags |