1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/annotation/RuntimeInlineAnnotationReader.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,151 @@ 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.xml.internal.bind.v2.model.annotation; 1.30 + 1.31 +import java.lang.annotation.Annotation; 1.32 +import java.lang.reflect.Field; 1.33 +import java.lang.reflect.InvocationTargetException; 1.34 +import java.lang.reflect.Method; 1.35 +import java.lang.reflect.Type; 1.36 +import java.util.HashMap; 1.37 +import java.util.Map; 1.38 + 1.39 +/** 1.40 + * {@link AnnotationReader} that uses {@code java.lang.reflect} to 1.41 + * read annotations from class files. 1.42 + * 1.43 + * @author Kohsuke Kawaguchi (kk@kohsuke.org) 1.44 + */ 1.45 +public final class RuntimeInlineAnnotationReader extends AbstractInlineAnnotationReaderImpl<Type,Class,Field,Method> 1.46 + implements RuntimeAnnotationReader { 1.47 + 1.48 + public <A extends Annotation> A getFieldAnnotation(Class<A> annotation, Field field, Locatable srcPos) { 1.49 + return LocatableAnnotation.create(field.getAnnotation(annotation),srcPos); 1.50 + } 1.51 + 1.52 + public boolean hasFieldAnnotation(Class<? extends Annotation> annotationType, Field field) { 1.53 + return field.isAnnotationPresent(annotationType); 1.54 + } 1.55 + 1.56 + public boolean hasClassAnnotation(Class clazz, Class<? extends Annotation> annotationType) { 1.57 + return clazz.isAnnotationPresent(annotationType); 1.58 + } 1.59 + 1.60 + public Annotation[] getAllFieldAnnotations(Field field, Locatable srcPos) { 1.61 + Annotation[] r = field.getAnnotations(); 1.62 + for( int i=0; i<r.length; i++ ) { 1.63 + r[i] = LocatableAnnotation.create(r[i],srcPos); 1.64 + } 1.65 + return r; 1.66 + } 1.67 + 1.68 + public <A extends Annotation> A getMethodAnnotation(Class<A> annotation, Method method, Locatable srcPos) { 1.69 + return LocatableAnnotation.create(method.getAnnotation(annotation),srcPos); 1.70 + } 1.71 + 1.72 + public boolean hasMethodAnnotation(Class<? extends Annotation> annotation, Method method) { 1.73 + return method.isAnnotationPresent(annotation); 1.74 + } 1.75 + 1.76 + public Annotation[] getAllMethodAnnotations(Method method, Locatable srcPos) { 1.77 + Annotation[] r = method.getAnnotations(); 1.78 + for( int i=0; i<r.length; i++ ) { 1.79 + r[i] = LocatableAnnotation.create(r[i],srcPos); 1.80 + } 1.81 + return r; 1.82 + } 1.83 + 1.84 + public <A extends Annotation> A getMethodParameterAnnotation(Class<A> annotation, Method method, int paramIndex, Locatable srcPos) { 1.85 + Annotation[] pa = method.getParameterAnnotations()[paramIndex]; 1.86 + for( Annotation a : pa ) { 1.87 + if(a.annotationType()==annotation) 1.88 + return LocatableAnnotation.create((A)a,srcPos); 1.89 + } 1.90 + return null; 1.91 + } 1.92 + 1.93 + public <A extends Annotation> A getClassAnnotation(Class<A> a, Class clazz, Locatable srcPos) { 1.94 + return LocatableAnnotation.create(((Class<?>)clazz).getAnnotation(a),srcPos); 1.95 + } 1.96 + 1.97 + 1.98 + /** 1.99 + * Cache for package-level annotations. 1.100 + */ 1.101 + private final Map<Class<? extends Annotation>,Map<Package,Annotation>> packageCache = 1.102 + new HashMap<Class<? extends Annotation>,Map<Package,Annotation>>(); 1.103 + 1.104 + public <A extends Annotation> A getPackageAnnotation(Class<A> a, Class clazz, Locatable srcPos) { 1.105 + Package p = clazz.getPackage(); 1.106 + if(p==null) return null; 1.107 + 1.108 + Map<Package,Annotation> cache = packageCache.get(a); 1.109 + if(cache==null) { 1.110 + cache = new HashMap<Package,Annotation>(); 1.111 + packageCache.put(a,cache); 1.112 + } 1.113 + 1.114 + if(cache.containsKey(p)) 1.115 + return (A)cache.get(p); 1.116 + else { 1.117 + A ann = LocatableAnnotation.create(p.getAnnotation(a),srcPos); 1.118 + cache.put(p,ann); 1.119 + return ann; 1.120 + } 1.121 + } 1.122 + 1.123 + public Class getClassValue(Annotation a, String name) { 1.124 + try { 1.125 + return (Class)a.annotationType().getMethod(name).invoke(a); 1.126 + } catch (IllegalAccessException e) { 1.127 + // impossible 1.128 + throw new IllegalAccessError(e.getMessage()); 1.129 + } catch (InvocationTargetException e) { 1.130 + // impossible 1.131 + throw new InternalError(Messages.CLASS_NOT_FOUND.format(a.annotationType(), e.getMessage())); 1.132 + } catch (NoSuchMethodException e) { 1.133 + throw new NoSuchMethodError(e.getMessage()); 1.134 + } 1.135 + } 1.136 + 1.137 + public Class[] getClassArrayValue(Annotation a, String name) { 1.138 + try { 1.139 + return (Class[])a.annotationType().getMethod(name).invoke(a); 1.140 + } catch (IllegalAccessException e) { 1.141 + // impossible 1.142 + throw new IllegalAccessError(e.getMessage()); 1.143 + } catch (InvocationTargetException e) { 1.144 + // impossible 1.145 + throw new InternalError(e.getMessage()); 1.146 + } catch (NoSuchMethodException e) { 1.147 + throw new NoSuchMethodError(e.getMessage()); 1.148 + } 1.149 + } 1.150 + 1.151 + protected String fullName(Method m) { 1.152 + return m.getDeclaringClass().getName()+'#'+m.getName(); 1.153 + } 1.154 +}