001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.model.annotation;
038:
039: import java.lang.annotation.Annotation;
040: import java.lang.reflect.Field;
041: import java.lang.reflect.InvocationTargetException;
042: import java.lang.reflect.Method;
043: import java.lang.reflect.Type;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: /**
048: * {@link AnnotationReader} that uses {@code java.lang.reflect} to
049: * read annotations from class files.
050: *
051: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
052: */
053: public final class RuntimeInlineAnnotationReader extends
054: AbstractInlineAnnotationReaderImpl<Type, Class, Field, Method>
055: implements RuntimeAnnotationReader {
056:
057: public <A extends Annotation> A getFieldAnnotation(
058: Class<A> annotation, Field field, Locatable srcPos) {
059: return LocatableAnnotation.create(field
060: .getAnnotation(annotation), srcPos);
061: }
062:
063: public boolean hasFieldAnnotation(
064: Class<? extends Annotation> annotationType, Field field) {
065: return field.isAnnotationPresent(annotationType);
066: }
067:
068: public boolean hasClassAnnotation(Class clazz,
069: Class<? extends Annotation> annotationType) {
070: return clazz.isAnnotationPresent(annotationType);
071: }
072:
073: public Annotation[] getAllFieldAnnotations(Field field,
074: Locatable srcPos) {
075: Annotation[] r = field.getAnnotations();
076: for (int i = 0; i < r.length; i++) {
077: r[i] = LocatableAnnotation.create(r[i], srcPos);
078: }
079: return r;
080: }
081:
082: public <A extends Annotation> A getMethodAnnotation(
083: Class<A> annotation, Method method, Locatable srcPos) {
084: return LocatableAnnotation.create(method
085: .getAnnotation(annotation), srcPos);
086: }
087:
088: public boolean hasMethodAnnotation(
089: Class<? extends Annotation> annotation, Method method) {
090: return method.isAnnotationPresent(annotation);
091: }
092:
093: public Annotation[] getAllMethodAnnotations(Method method,
094: Locatable srcPos) {
095: Annotation[] r = method.getAnnotations();
096: for (int i = 0; i < r.length; i++) {
097: r[i] = LocatableAnnotation.create(r[i], srcPos);
098: }
099: return r;
100: }
101:
102: public <A extends Annotation> A getMethodParameterAnnotation(
103: Class<A> annotation, Method method, int paramIndex,
104: Locatable srcPos) {
105: Annotation[] pa = method.getParameterAnnotations()[paramIndex];
106: for (Annotation a : pa) {
107: if (a.annotationType() == annotation)
108: return LocatableAnnotation.create((A) a, srcPos);
109: }
110: return null;
111: }
112:
113: public <A extends Annotation> A getClassAnnotation(Class<A> a,
114: Class clazz, Locatable srcPos) {
115: return LocatableAnnotation.create(((Class<?>) clazz)
116: .getAnnotation(a), srcPos);
117: }
118:
119: /**
120: * Cache for package-level annotations.
121: */
122: private final Map<Class<? extends Annotation>, Map<Package, Annotation>> packageCache = new HashMap<Class<? extends Annotation>, Map<Package, Annotation>>();
123:
124: public <A extends Annotation> A getPackageAnnotation(Class<A> a,
125: Class clazz, Locatable srcPos) {
126: Package p = clazz.getPackage();
127: if (p == null)
128: return null;
129:
130: Map<Package, Annotation> cache = packageCache.get(a);
131: if (cache == null) {
132: cache = new HashMap<Package, Annotation>();
133: packageCache.put(a, cache);
134: }
135:
136: if (cache.containsKey(p))
137: return (A) cache.get(p);
138: else {
139: A ann = LocatableAnnotation.create(p.getAnnotation(a),
140: srcPos);
141: cache.put(p, ann);
142: return ann;
143: }
144: }
145:
146: public Class getClassValue(Annotation a, String name) {
147: try {
148: return (Class) a.annotationType().getMethod(name).invoke(a);
149: } catch (IllegalAccessException e) {
150: // impossible
151: throw new IllegalAccessError(e.getMessage());
152: } catch (InvocationTargetException e) {
153: // impossible
154: throw new InternalError(e.getMessage());
155: } catch (NoSuchMethodException e) {
156: throw new NoSuchMethodError(e.getMessage());
157: }
158: }
159:
160: public Class[] getClassArrayValue(Annotation a, String name) {
161: try {
162: return (Class[]) a.annotationType().getMethod(name).invoke(
163: a);
164: } catch (IllegalAccessException e) {
165: // impossible
166: throw new IllegalAccessError(e.getMessage());
167: } catch (InvocationTargetException e) {
168: // impossible
169: throw new InternalError(e.getMessage());
170: } catch (NoSuchMethodException e) {
171: throw new NoSuchMethodError(e.getMessage());
172: }
173: }
174:
175: protected String fullName(Method m) {
176: return m.getDeclaringClass().getName() + '#' + m.getName();
177: }
178: }
|