1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/InlineAnnotationReaderImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,175 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.jxc.ap; 1.30 + 1.31 +import java.lang.annotation.Annotation; 1.32 +import java.lang.reflect.InvocationTargetException; 1.33 +import java.util.ArrayList; 1.34 +import java.util.Collection; 1.35 +import java.util.List; 1.36 + 1.37 +import javax.lang.model.element.AnnotationMirror; 1.38 +import javax.lang.model.element.Element; 1.39 +import javax.lang.model.element.ExecutableElement; 1.40 +import javax.lang.model.element.TypeElement; 1.41 +import javax.lang.model.element.VariableElement; 1.42 +import javax.lang.model.type.MirroredTypeException; 1.43 +import javax.lang.model.type.MirroredTypesException; 1.44 +import javax.lang.model.type.TypeMirror; 1.45 +import com.sun.xml.internal.bind.v2.model.annotation.AbstractInlineAnnotationReaderImpl; 1.46 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; 1.47 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable; 1.48 +import com.sun.xml.internal.bind.v2.model.annotation.LocatableAnnotation; 1.49 + 1.50 +/** 1.51 + * {@link AnnotationReader} implementation that reads annotation inline from Annoation Processing. 1.52 + * 1.53 + * @author Kohsuke Kawaguchi (kk@kohsuke.org) 1.54 + */ 1.55 +public final class InlineAnnotationReaderImpl extends AbstractInlineAnnotationReaderImpl<TypeMirror, TypeElement, VariableElement, ExecutableElement> { 1.56 + 1.57 + /** The singleton instance. */ 1.58 + public static final InlineAnnotationReaderImpl theInstance = new InlineAnnotationReaderImpl(); 1.59 + 1.60 + private InlineAnnotationReaderImpl() {} 1.61 + 1.62 + public <A extends Annotation> A getClassAnnotation(Class<A> a, TypeElement clazz, Locatable srcPos) { 1.63 + return LocatableAnnotation.create(clazz.getAnnotation(a),srcPos); 1.64 + } 1.65 + 1.66 + public <A extends Annotation> A getFieldAnnotation(Class<A> a, VariableElement f, Locatable srcPos) { 1.67 + return LocatableAnnotation.create(f.getAnnotation(a),srcPos); 1.68 + } 1.69 + 1.70 + public boolean hasFieldAnnotation(Class<? extends Annotation> annotationType, VariableElement f) { 1.71 + return f.getAnnotation(annotationType)!=null; 1.72 + } 1.73 + 1.74 + public boolean hasClassAnnotation(TypeElement clazz, Class<? extends Annotation> annotationType) { 1.75 + return clazz.getAnnotation(annotationType)!=null; 1.76 + } 1.77 + 1.78 + public Annotation[] getAllFieldAnnotations(VariableElement field, Locatable srcPos) { 1.79 + return getAllAnnotations(field,srcPos); 1.80 + } 1.81 + 1.82 + public <A extends Annotation> A getMethodAnnotation(Class<A> a, ExecutableElement method, Locatable srcPos) { 1.83 + return LocatableAnnotation.create(method.getAnnotation(a),srcPos); 1.84 + } 1.85 + 1.86 + public boolean hasMethodAnnotation(Class<? extends Annotation> a, ExecutableElement method) { 1.87 + return method.getAnnotation(a)!=null; 1.88 + } 1.89 + 1.90 + public Annotation[] getAllMethodAnnotations(ExecutableElement method, Locatable srcPos) { 1.91 + return getAllAnnotations(method,srcPos); 1.92 + } 1.93 + 1.94 + /** 1.95 + * Gets all the annotations on the given declaration. 1.96 + */ 1.97 + private Annotation[] getAllAnnotations(Element decl, Locatable srcPos) { 1.98 + List<Annotation> r = new ArrayList<Annotation>(); 1.99 + 1.100 + for( AnnotationMirror m : decl.getAnnotationMirrors() ) { 1.101 + try { 1.102 + String fullName = ((TypeElement) m.getAnnotationType().asElement()).getQualifiedName().toString(); 1.103 + Class<? extends Annotation> type = 1.104 + SecureLoader.getClassClassLoader(getClass()).loadClass(fullName).asSubclass(Annotation.class); 1.105 + Annotation annotation = decl.getAnnotation(type); 1.106 + if(annotation!=null) 1.107 + r.add( LocatableAnnotation.create(annotation,srcPos) ); 1.108 + } catch (ClassNotFoundException e) { 1.109 + // just continue 1.110 + } 1.111 + } 1.112 + 1.113 + return r.toArray(new Annotation[r.size()]); 1.114 + } 1.115 + 1.116 + public <A extends Annotation> A getMethodParameterAnnotation(Class<A> a, ExecutableElement m, int paramIndex, Locatable srcPos) { 1.117 + VariableElement[] params = m.getParameters().toArray(new VariableElement[m.getParameters().size()]); 1.118 + return LocatableAnnotation.create( 1.119 + params[paramIndex].getAnnotation(a), srcPos ); 1.120 + } 1.121 + 1.122 + public <A extends Annotation> A getPackageAnnotation(Class<A> a, TypeElement clazz, Locatable srcPos) { 1.123 + return LocatableAnnotation.create(clazz.getEnclosingElement().getAnnotation(a), srcPos); 1.124 + } 1.125 + 1.126 + public TypeMirror getClassValue(Annotation a, String name) { 1.127 + try { 1.128 + a.annotationType().getMethod(name).invoke(a); 1.129 + assert false; 1.130 + throw new IllegalStateException("should throw a MirroredTypeException"); 1.131 + } catch (IllegalAccessException e) { 1.132 + throw new IllegalAccessError(e.getMessage()); 1.133 + } catch (InvocationTargetException e) { 1.134 + if( e.getCause() instanceof MirroredTypeException ) { 1.135 + MirroredTypeException me = (MirroredTypeException)e.getCause(); 1.136 + return me.getTypeMirror(); 1.137 + } 1.138 + // impossible 1.139 + throw new RuntimeException(e); 1.140 + } catch (NoSuchMethodException e) { 1.141 + throw new NoSuchMethodError(e.getMessage()); 1.142 + } 1.143 + } 1.144 + 1.145 + public TypeMirror[] getClassArrayValue(Annotation a, String name) { 1.146 + try { 1.147 + a.annotationType().getMethod(name).invoke(a); 1.148 + assert false; 1.149 + throw new IllegalStateException("should throw a MirroredTypesException"); 1.150 + } catch (IllegalAccessException e) { 1.151 + throw new IllegalAccessError(e.getMessage()); 1.152 + } catch (InvocationTargetException e) { 1.153 + if( e.getCause() instanceof MirroredTypesException ) { 1.154 + MirroredTypesException me = (MirroredTypesException)e.getCause(); 1.155 + Collection<? extends TypeMirror> r = me.getTypeMirrors(); 1.156 + return r.toArray(new TypeMirror[r.size()]); 1.157 + } 1.158 + // *********************** TODO: jdk6 bug. Fixed in java7 1.159 + // According to the javadocs it should throw the MirroredTypesException 1.160 + if( e.getCause() instanceof MirroredTypeException ) { 1.161 + MirroredTypeException me = (MirroredTypeException)e.getCause(); 1.162 + TypeMirror tr = me.getTypeMirror(); 1.163 + TypeMirror[] trArr = new TypeMirror[1]; 1.164 + trArr[0] = tr; 1.165 + return trArr; 1.166 + } 1.167 + // ******************************************* 1.168 + // impossible 1.169 + throw new RuntimeException(e); 1.170 + } catch (NoSuchMethodException e) { 1.171 + throw new NoSuchMethodError(e.getMessage()); 1.172 + } 1.173 + } 1.174 + 1.175 + protected String fullName(ExecutableElement m) { 1.176 + return ((TypeElement) m.getEnclosingElement()).getQualifiedName().toString()+'#'+m.getSimpleName(); 1.177 + } 1.178 +}