1 22 package org.jboss.metadata.plugins.loader.reflection; 23 24 import java.lang.annotation.Annotation ; 25 import java.lang.reflect.AnnotatedElement ; 26 import java.lang.reflect.Member ; 27 28 import org.jboss.metadata.plugins.loader.BasicMetaDataLoader; 29 import org.jboss.metadata.spi.retrieval.AnnotationItem; 30 import org.jboss.metadata.spi.retrieval.AnnotationsItem; 31 import org.jboss.metadata.spi.retrieval.simple.SimpleAnnotationItem; 32 import org.jboss.metadata.spi.retrieval.simple.SimpleAnnotationsItem; 33 import org.jboss.metadata.spi.scope.CommonLevels; 34 import org.jboss.metadata.spi.scope.Scope; 35 import org.jboss.metadata.spi.scope.ScopeKey; 36 import org.jboss.util.JBossStringBuilder; 37 import org.jboss.util.Strings; 38 39 45 public class AnnotatedElementMetaDataLoader extends BasicMetaDataLoader 46 { 47 48 private AnnotatedElement annotated; 49 50 private static final ScopeKey getScopeKey(AnnotatedElement annotated) 51 { 52 Scope scope; 53 if (annotated instanceof Class ) 54 { 55 Class clazz = (Class ) annotated; 56 scope = new Scope(CommonLevels.CLASS, clazz.getName()); 57 } 58 else if (annotated instanceof Member ) 59 { 60 Member member = (Member ) annotated; 61 scope = new Scope(CommonLevels.JOINPOINT, member.getName()); 62 } 63 else 64 { 65 return ScopeKey.DEFAULT_SCOPE; 66 } 67 return new ScopeKey(scope); 68 } 69 70 75 public AnnotatedElementMetaDataLoader(AnnotatedElement annotated) 76 { 77 super(getScopeKey(annotated)); 78 if (annotated == null) 79 throw new IllegalArgumentException ("Null annotated element"); 80 this.annotated = annotated; 81 } 82 83 @SuppressWarnings ("unchecked") 84 public AnnotationsItem retrieveAnnotations() 85 { 86 Annotation [] annotations = annotated.getAnnotations(); 87 if (annotations.length == 0) 88 return SimpleAnnotationsItem.NO_ANNOTATIONS; 89 90 AnnotationItem[] items = new AnnotationItem[annotations.length]; 91 for (int i = 0; i < items.length; ++i) 92 items[i] = new SimpleAnnotationItem(annotations[i]); 93 return new SimpleAnnotationsItem(items); 94 } 95 96 public <T extends Annotation > AnnotationItem<T> retrieveAnnotation(Class <T> annotationType) 97 { 98 T annotation = annotated.getAnnotation(annotationType); 99 if (annotation == null) 100 return null; 101 return new SimpleAnnotationItem<T>(annotation); 102 } 103 104 public String toString() 105 { 106 JBossStringBuilder buffer = new JBossStringBuilder(); 107 Strings.defaultToString(buffer, this); 108 buffer.append("["); 109 buffer.append(annotated); 110 buffer.append("]"); 111 return buffer.toString(); 112 } 113 } 114 | Popular Tags |