KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > plugins > loader > reflection > AnnotatedElementMetaDataLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata.plugins.loader.reflection;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.lang.reflect.AnnotatedElement JavaDoc;
26 import java.lang.reflect.Member JavaDoc;
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 /**
40  * AnnotatedElementMetaDataLoader.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @version $Revision: 57307 $
44  */

45 public class AnnotatedElementMetaDataLoader extends BasicMetaDataLoader
46 {
47    /** The annotated element */
48    private AnnotatedElement JavaDoc annotated;
49    
50    private static final ScopeKey getScopeKey(AnnotatedElement JavaDoc annotated)
51    {
52       Scope scope;
53       if (annotated instanceof Class JavaDoc)
54       {
55          Class JavaDoc clazz = (Class JavaDoc) annotated;
56          scope = new Scope(CommonLevels.CLASS, clazz.getName());
57       }
58       else if (annotated instanceof Member JavaDoc)
59       {
60          Member JavaDoc member = (Member JavaDoc) 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    /**
71     * Create a new AnnotatedElementMetaDataContext.
72     *
73     * @param annotated the annotated element
74     */

75    public AnnotatedElementMetaDataLoader(AnnotatedElement JavaDoc annotated)
76    {
77       super(getScopeKey(annotated));
78       if (annotated == null)
79          throw new IllegalArgumentException JavaDoc("Null annotated element");
80       this.annotated = annotated;
81    }
82
83    @SuppressWarnings JavaDoc("unchecked")
84    public AnnotationsItem retrieveAnnotations()
85    {
86       Annotation JavaDoc[] 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 JavaDoc> AnnotationItem<T> retrieveAnnotation(Class JavaDoc<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 JavaDoc 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