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.tools.internal.jxc.apt;
027:
028: import java.lang.annotation.Annotation;
029: import java.lang.reflect.InvocationTargetException;
030: import java.util.List;
031: import java.util.ArrayList;
032:
033: import com.sun.mirror.declaration.FieldDeclaration;
034: import com.sun.mirror.declaration.MethodDeclaration;
035: import com.sun.mirror.declaration.ParameterDeclaration;
036: import com.sun.mirror.declaration.TypeDeclaration;
037: import com.sun.mirror.declaration.AnnotationMirror;
038: import com.sun.mirror.declaration.Declaration;
039: import com.sun.mirror.type.MirroredTypeException;
040: import com.sun.mirror.type.TypeMirror;
041: import com.sun.xml.internal.bind.v2.model.annotation.AbstractInlineAnnotationReaderImpl;
042: import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
043: import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
044: import com.sun.xml.internal.bind.v2.model.annotation.LocatableAnnotation;
045:
046: /**
047: * {@link AnnotationReader} implementation that reads annotation inline from APT.
048: *
049: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
050: */
051: public final class InlineAnnotationReaderImpl
052: extends
053: AbstractInlineAnnotationReaderImpl<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> {
054:
055: /** The singleton instance. */
056: public static final InlineAnnotationReaderImpl theInstance = new InlineAnnotationReaderImpl();
057:
058: private InlineAnnotationReaderImpl() {
059: }
060:
061: public <A extends Annotation> A getClassAnnotation(Class<A> a,
062: TypeDeclaration clazz, Locatable srcPos) {
063: return LocatableAnnotation.create(clazz.getAnnotation(a),
064: srcPos);
065: }
066:
067: public <A extends Annotation> A getFieldAnnotation(Class<A> a,
068: FieldDeclaration f, Locatable srcPos) {
069: return LocatableAnnotation.create(f.getAnnotation(a), srcPos);
070: }
071:
072: public boolean hasFieldAnnotation(
073: Class<? extends Annotation> annotationType,
074: FieldDeclaration f) {
075: return f.getAnnotation(annotationType) != null;
076: }
077:
078: public Annotation[] getAllFieldAnnotations(FieldDeclaration field,
079: Locatable srcPos) {
080: return getAllAnnotations(field, srcPos);
081: }
082:
083: public <A extends Annotation> A getMethodAnnotation(Class<A> a,
084: MethodDeclaration method, Locatable srcPos) {
085: return LocatableAnnotation.create(method.getAnnotation(a),
086: srcPos);
087: }
088:
089: public boolean hasMethodAnnotation(Class<? extends Annotation> a,
090: MethodDeclaration method) {
091: return method.getAnnotation(a) != null;
092: }
093:
094: private static final Annotation[] EMPTY_ANNOTATION = new Annotation[0];
095:
096: public Annotation[] getAllMethodAnnotations(
097: MethodDeclaration method, Locatable srcPos) {
098: return getAllAnnotations(method, srcPos);
099: }
100:
101: /**
102: * Gets all the annotations on the given declaration.
103: */
104: private Annotation[] getAllAnnotations(Declaration decl,
105: Locatable srcPos) {
106: List<Annotation> r = new ArrayList<Annotation>();
107:
108: for (AnnotationMirror m : decl.getAnnotationMirrors()) {
109: try {
110: String fullName = m.getAnnotationType()
111: .getDeclaration().getQualifiedName();
112: Class type = getClass().getClassLoader().loadClass(
113: fullName);
114: Annotation annotation = decl.getAnnotation(type);
115: if (annotation != null)
116: r.add(LocatableAnnotation
117: .create(annotation, srcPos));
118: } catch (ClassNotFoundException e) {
119: // just continue
120: }
121: }
122:
123: return r.toArray(EMPTY_ANNOTATION);
124: }
125:
126: public <A extends Annotation> A getMethodParameterAnnotation(
127: Class<A> a, MethodDeclaration m, int paramIndex,
128: Locatable srcPos) {
129: ParameterDeclaration[] params = m.getParameters().toArray(
130: new ParameterDeclaration[0]);
131: return LocatableAnnotation.create(params[paramIndex]
132: .getAnnotation(a), srcPos);
133: }
134:
135: public <A extends Annotation> A getPackageAnnotation(Class<A> a,
136: TypeDeclaration clazz, Locatable srcPos) {
137: return LocatableAnnotation.create(clazz.getPackage()
138: .getAnnotation(a), srcPos);
139: }
140:
141: public TypeMirror getClassValue(Annotation a, String name) {
142: try {
143: a.annotationType().getMethod(name).invoke(a);
144: assert false;
145: throw new IllegalStateException(
146: "should throw a MirroredTypeException");
147: } catch (IllegalAccessException e) {
148: throw new IllegalAccessError(e.getMessage());
149: } catch (InvocationTargetException e) {
150: if (e.getCause() instanceof MirroredTypeException) {
151: MirroredTypeException me = (MirroredTypeException) e
152: .getCause();
153: return me.getTypeMirror();
154: }
155: // impossible
156: throw new RuntimeException(e);
157: } catch (NoSuchMethodException e) {
158: throw new NoSuchMethodError(e.getMessage());
159: }
160: }
161:
162: protected String fullName(MethodDeclaration m) {
163: return m.getDeclaringType().getQualifiedName() + '#'
164: + m.getSimpleName();
165: }
166: }
|