src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/InlineAnnotationReaderImpl.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.jxc.ap;
aoqi@0 27
aoqi@0 28 import java.lang.annotation.Annotation;
aoqi@0 29 import java.lang.reflect.InvocationTargetException;
aoqi@0 30 import java.util.ArrayList;
aoqi@0 31 import java.util.Collection;
aoqi@0 32 import java.util.List;
aoqi@0 33
aoqi@0 34 import javax.lang.model.element.AnnotationMirror;
aoqi@0 35 import javax.lang.model.element.Element;
aoqi@0 36 import javax.lang.model.element.ExecutableElement;
aoqi@0 37 import javax.lang.model.element.TypeElement;
aoqi@0 38 import javax.lang.model.element.VariableElement;
aoqi@0 39 import javax.lang.model.type.MirroredTypeException;
aoqi@0 40 import javax.lang.model.type.MirroredTypesException;
aoqi@0 41 import javax.lang.model.type.TypeMirror;
aoqi@0 42 import com.sun.xml.internal.bind.v2.model.annotation.AbstractInlineAnnotationReaderImpl;
aoqi@0 43 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
aoqi@0 44 import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
aoqi@0 45 import com.sun.xml.internal.bind.v2.model.annotation.LocatableAnnotation;
aoqi@0 46
aoqi@0 47 /**
aoqi@0 48 * {@link AnnotationReader} implementation that reads annotation inline from Annoation Processing.
aoqi@0 49 *
aoqi@0 50 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
aoqi@0 51 */
aoqi@0 52 public final class InlineAnnotationReaderImpl extends AbstractInlineAnnotationReaderImpl<TypeMirror, TypeElement, VariableElement, ExecutableElement> {
aoqi@0 53
aoqi@0 54 /** The singleton instance. */
aoqi@0 55 public static final InlineAnnotationReaderImpl theInstance = new InlineAnnotationReaderImpl();
aoqi@0 56
aoqi@0 57 private InlineAnnotationReaderImpl() {}
aoqi@0 58
aoqi@0 59 public <A extends Annotation> A getClassAnnotation(Class<A> a, TypeElement clazz, Locatable srcPos) {
aoqi@0 60 return LocatableAnnotation.create(clazz.getAnnotation(a),srcPos);
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 public <A extends Annotation> A getFieldAnnotation(Class<A> a, VariableElement f, Locatable srcPos) {
aoqi@0 64 return LocatableAnnotation.create(f.getAnnotation(a),srcPos);
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 public boolean hasFieldAnnotation(Class<? extends Annotation> annotationType, VariableElement f) {
aoqi@0 68 return f.getAnnotation(annotationType)!=null;
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 public boolean hasClassAnnotation(TypeElement clazz, Class<? extends Annotation> annotationType) {
aoqi@0 72 return clazz.getAnnotation(annotationType)!=null;
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 public Annotation[] getAllFieldAnnotations(VariableElement field, Locatable srcPos) {
aoqi@0 76 return getAllAnnotations(field,srcPos);
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 public <A extends Annotation> A getMethodAnnotation(Class<A> a, ExecutableElement method, Locatable srcPos) {
aoqi@0 80 return LocatableAnnotation.create(method.getAnnotation(a),srcPos);
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 public boolean hasMethodAnnotation(Class<? extends Annotation> a, ExecutableElement method) {
aoqi@0 84 return method.getAnnotation(a)!=null;
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 public Annotation[] getAllMethodAnnotations(ExecutableElement method, Locatable srcPos) {
aoqi@0 88 return getAllAnnotations(method,srcPos);
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 /**
aoqi@0 92 * Gets all the annotations on the given declaration.
aoqi@0 93 */
aoqi@0 94 private Annotation[] getAllAnnotations(Element decl, Locatable srcPos) {
aoqi@0 95 List<Annotation> r = new ArrayList<Annotation>();
aoqi@0 96
aoqi@0 97 for( AnnotationMirror m : decl.getAnnotationMirrors() ) {
aoqi@0 98 try {
aoqi@0 99 String fullName = ((TypeElement) m.getAnnotationType().asElement()).getQualifiedName().toString();
aoqi@0 100 Class<? extends Annotation> type =
aoqi@0 101 SecureLoader.getClassClassLoader(getClass()).loadClass(fullName).asSubclass(Annotation.class);
aoqi@0 102 Annotation annotation = decl.getAnnotation(type);
aoqi@0 103 if(annotation!=null)
aoqi@0 104 r.add( LocatableAnnotation.create(annotation,srcPos) );
aoqi@0 105 } catch (ClassNotFoundException e) {
aoqi@0 106 // just continue
aoqi@0 107 }
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 return r.toArray(new Annotation[r.size()]);
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 public <A extends Annotation> A getMethodParameterAnnotation(Class<A> a, ExecutableElement m, int paramIndex, Locatable srcPos) {
aoqi@0 114 VariableElement[] params = m.getParameters().toArray(new VariableElement[m.getParameters().size()]);
aoqi@0 115 return LocatableAnnotation.create(
aoqi@0 116 params[paramIndex].getAnnotation(a), srcPos );
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 public <A extends Annotation> A getPackageAnnotation(Class<A> a, TypeElement clazz, Locatable srcPos) {
aoqi@0 120 return LocatableAnnotation.create(clazz.getEnclosingElement().getAnnotation(a), srcPos);
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 public TypeMirror getClassValue(Annotation a, String name) {
aoqi@0 124 try {
aoqi@0 125 a.annotationType().getMethod(name).invoke(a);
aoqi@0 126 assert false;
aoqi@0 127 throw new IllegalStateException("should throw a MirroredTypeException");
aoqi@0 128 } catch (IllegalAccessException e) {
aoqi@0 129 throw new IllegalAccessError(e.getMessage());
aoqi@0 130 } catch (InvocationTargetException e) {
aoqi@0 131 if( e.getCause() instanceof MirroredTypeException ) {
aoqi@0 132 MirroredTypeException me = (MirroredTypeException)e.getCause();
aoqi@0 133 return me.getTypeMirror();
aoqi@0 134 }
aoqi@0 135 // impossible
aoqi@0 136 throw new RuntimeException(e);
aoqi@0 137 } catch (NoSuchMethodException e) {
aoqi@0 138 throw new NoSuchMethodError(e.getMessage());
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 public TypeMirror[] getClassArrayValue(Annotation a, String name) {
aoqi@0 143 try {
aoqi@0 144 a.annotationType().getMethod(name).invoke(a);
aoqi@0 145 assert false;
aoqi@0 146 throw new IllegalStateException("should throw a MirroredTypesException");
aoqi@0 147 } catch (IllegalAccessException e) {
aoqi@0 148 throw new IllegalAccessError(e.getMessage());
aoqi@0 149 } catch (InvocationTargetException e) {
aoqi@0 150 if( e.getCause() instanceof MirroredTypesException ) {
aoqi@0 151 MirroredTypesException me = (MirroredTypesException)e.getCause();
aoqi@0 152 Collection<? extends TypeMirror> r = me.getTypeMirrors();
aoqi@0 153 return r.toArray(new TypeMirror[r.size()]);
aoqi@0 154 }
aoqi@0 155 // *********************** TODO: jdk6 bug. Fixed in java7
aoqi@0 156 // According to the javadocs it should throw the MirroredTypesException
aoqi@0 157 if( e.getCause() instanceof MirroredTypeException ) {
aoqi@0 158 MirroredTypeException me = (MirroredTypeException)e.getCause();
aoqi@0 159 TypeMirror tr = me.getTypeMirror();
aoqi@0 160 TypeMirror[] trArr = new TypeMirror[1];
aoqi@0 161 trArr[0] = tr;
aoqi@0 162 return trArr;
aoqi@0 163 }
aoqi@0 164 // *******************************************
aoqi@0 165 // impossible
aoqi@0 166 throw new RuntimeException(e);
aoqi@0 167 } catch (NoSuchMethodException e) {
aoqi@0 168 throw new NoSuchMethodError(e.getMessage());
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 protected String fullName(ExecutableElement m) {
aoqi@0 173 return ((TypeElement) m.getEnclosingElement()).getQualifiedName().toString()+'#'+m.getSimpleName();
aoqi@0 174 }
aoqi@0 175 }

mercurial