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