001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.model.annotation;
027:
028: import java.lang.annotation.Annotation;
029: import java.lang.reflect.Field;
030: import java.lang.reflect.InvocationTargetException;
031: import java.lang.reflect.Method;
032: import java.lang.reflect.Type;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: * {@link AnnotationReader} that uses {@code java.lang.reflect} to
038: * read annotations from class files.
039: *
040: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
041: */
042: public final class RuntimeInlineAnnotationReader extends
043: AbstractInlineAnnotationReaderImpl<Type, Class, Field, Method>
044: implements RuntimeAnnotationReader {
045:
046: public <A extends Annotation> A getFieldAnnotation(
047: Class<A> annotation, Field field, Locatable srcPos) {
048: return LocatableAnnotation.create(field
049: .getAnnotation(annotation), srcPos);
050: }
051:
052: public boolean hasFieldAnnotation(
053: Class<? extends Annotation> annotationType, Field field) {
054: return field.isAnnotationPresent(annotationType);
055: }
056:
057: public Annotation[] getAllFieldAnnotations(Field field,
058: Locatable srcPos) {
059: Annotation[] r = field.getAnnotations();
060: for (int i = 0; i < r.length; i++) {
061: r[i] = LocatableAnnotation.create(r[i], srcPos);
062: }
063: return r;
064: }
065:
066: public <A extends Annotation> A getMethodAnnotation(
067: Class<A> annotation, Method method, Locatable srcPos) {
068: return LocatableAnnotation.create(method
069: .getAnnotation(annotation), srcPos);
070: }
071:
072: public boolean hasMethodAnnotation(
073: Class<? extends Annotation> annotation, Method method) {
074: return method.isAnnotationPresent(annotation);
075: }
076:
077: public Annotation[] getAllMethodAnnotations(Method method,
078: Locatable srcPos) {
079: Annotation[] r = method.getAnnotations();
080: for (int i = 0; i < r.length; i++) {
081: r[i] = LocatableAnnotation.create(r[i], srcPos);
082: }
083: return r;
084: }
085:
086: public <A extends Annotation> A getMethodParameterAnnotation(
087: Class<A> annotation, Method method, int paramIndex,
088: Locatable srcPos) {
089: Annotation[] pa = method.getParameterAnnotations()[paramIndex];
090: for (Annotation a : pa) {
091: if (a.annotationType() == annotation)
092: return LocatableAnnotation.create((A) a, srcPos);
093: }
094: return null;
095: }
096:
097: public <A extends Annotation> A getClassAnnotation(Class<A> a,
098: Class clazz, Locatable srcPos) {
099: return LocatableAnnotation.create(((Class<?>) clazz)
100: .getAnnotation(a), srcPos);
101: }
102:
103: /**
104: * Cache for package-level annotations.
105: */
106: private final Map<Class<? extends Annotation>, Map<Package, Annotation>> packageCache = new HashMap<Class<? extends Annotation>, Map<Package, Annotation>>();
107:
108: public <A extends Annotation> A getPackageAnnotation(Class<A> a,
109: Class clazz, Locatable srcPos) {
110: Package p = clazz.getPackage();
111: if (p == null)
112: return null;
113:
114: Map<Package, Annotation> cache = packageCache.get(a);
115: if (cache == null) {
116: cache = new HashMap<Package, Annotation>();
117: packageCache.put(a, cache);
118: }
119:
120: if (cache.containsKey(p))
121: return (A) cache.get(p);
122: else {
123: A ann = LocatableAnnotation.create(p.getAnnotation(a),
124: srcPos);
125: cache.put(p, ann);
126: return ann;
127: }
128: }
129:
130: public Class getClassValue(Annotation a, String name) {
131: try {
132: return (Class) a.annotationType().getMethod(name).invoke(a);
133: } catch (IllegalAccessException e) {
134: // impossible
135: throw new IllegalAccessError(e.getMessage());
136: } catch (InvocationTargetException e) {
137: // impossible
138: throw new InternalError(e.getMessage());
139: } catch (NoSuchMethodException e) {
140: throw new NoSuchMethodError(e.getMessage());
141: }
142: }
143:
144: protected String fullName(Method m) {
145: return m.getDeclaringClass().getName() + '#' + m.getName();
146: }
147: }
|