Thu, 31 Aug 2017 15:18:52 +0800
merge
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.xml.internal.bind.v2.model.annotation; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.annotation.Annotation; |
aoqi@0 | 29 | import java.lang.reflect.Field; |
aoqi@0 | 30 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 31 | import java.lang.reflect.Method; |
aoqi@0 | 32 | import java.lang.reflect.Type; |
aoqi@0 | 33 | import java.util.HashMap; |
aoqi@0 | 34 | import java.util.Map; |
aoqi@0 | 35 | |
aoqi@0 | 36 | /** |
aoqi@0 | 37 | * {@link AnnotationReader} that uses {@code java.lang.reflect} to |
aoqi@0 | 38 | * read annotations from class files. |
aoqi@0 | 39 | * |
aoqi@0 | 40 | * @author Kohsuke Kawaguchi (kk@kohsuke.org) |
aoqi@0 | 41 | */ |
aoqi@0 | 42 | public final class RuntimeInlineAnnotationReader extends AbstractInlineAnnotationReaderImpl<Type,Class,Field,Method> |
aoqi@0 | 43 | implements RuntimeAnnotationReader { |
aoqi@0 | 44 | |
aoqi@0 | 45 | public <A extends Annotation> A getFieldAnnotation(Class<A> annotation, Field field, Locatable srcPos) { |
aoqi@0 | 46 | return LocatableAnnotation.create(field.getAnnotation(annotation),srcPos); |
aoqi@0 | 47 | } |
aoqi@0 | 48 | |
aoqi@0 | 49 | public boolean hasFieldAnnotation(Class<? extends Annotation> annotationType, Field field) { |
aoqi@0 | 50 | return field.isAnnotationPresent(annotationType); |
aoqi@0 | 51 | } |
aoqi@0 | 52 | |
aoqi@0 | 53 | public boolean hasClassAnnotation(Class clazz, Class<? extends Annotation> annotationType) { |
aoqi@0 | 54 | return clazz.isAnnotationPresent(annotationType); |
aoqi@0 | 55 | } |
aoqi@0 | 56 | |
aoqi@0 | 57 | public Annotation[] getAllFieldAnnotations(Field field, Locatable srcPos) { |
aoqi@0 | 58 | Annotation[] r = field.getAnnotations(); |
aoqi@0 | 59 | for( int i=0; i<r.length; i++ ) { |
aoqi@0 | 60 | r[i] = LocatableAnnotation.create(r[i],srcPos); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | return r; |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | public <A extends Annotation> A getMethodAnnotation(Class<A> annotation, Method method, Locatable srcPos) { |
aoqi@0 | 66 | return LocatableAnnotation.create(method.getAnnotation(annotation),srcPos); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | public boolean hasMethodAnnotation(Class<? extends Annotation> annotation, Method method) { |
aoqi@0 | 70 | return method.isAnnotationPresent(annotation); |
aoqi@0 | 71 | } |
aoqi@0 | 72 | |
aoqi@0 | 73 | public Annotation[] getAllMethodAnnotations(Method method, Locatable srcPos) { |
aoqi@0 | 74 | Annotation[] r = method.getAnnotations(); |
aoqi@0 | 75 | for( int i=0; i<r.length; i++ ) { |
aoqi@0 | 76 | r[i] = LocatableAnnotation.create(r[i],srcPos); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | return r; |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | public <A extends Annotation> A getMethodParameterAnnotation(Class<A> annotation, Method method, int paramIndex, Locatable srcPos) { |
aoqi@0 | 82 | Annotation[] pa = method.getParameterAnnotations()[paramIndex]; |
aoqi@0 | 83 | for( Annotation a : pa ) { |
aoqi@0 | 84 | if(a.annotationType()==annotation) |
aoqi@0 | 85 | return LocatableAnnotation.create((A)a,srcPos); |
aoqi@0 | 86 | } |
aoqi@0 | 87 | return null; |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | public <A extends Annotation> A getClassAnnotation(Class<A> a, Class clazz, Locatable srcPos) { |
aoqi@0 | 91 | return LocatableAnnotation.create(((Class<?>)clazz).getAnnotation(a),srcPos); |
aoqi@0 | 92 | } |
aoqi@0 | 93 | |
aoqi@0 | 94 | |
aoqi@0 | 95 | /** |
aoqi@0 | 96 | * Cache for package-level annotations. |
aoqi@0 | 97 | */ |
aoqi@0 | 98 | private final Map<Class<? extends Annotation>,Map<Package,Annotation>> packageCache = |
aoqi@0 | 99 | new HashMap<Class<? extends Annotation>,Map<Package,Annotation>>(); |
aoqi@0 | 100 | |
aoqi@0 | 101 | public <A extends Annotation> A getPackageAnnotation(Class<A> a, Class clazz, Locatable srcPos) { |
aoqi@0 | 102 | Package p = clazz.getPackage(); |
aoqi@0 | 103 | if(p==null) return null; |
aoqi@0 | 104 | |
aoqi@0 | 105 | Map<Package,Annotation> cache = packageCache.get(a); |
aoqi@0 | 106 | if(cache==null) { |
aoqi@0 | 107 | cache = new HashMap<Package,Annotation>(); |
aoqi@0 | 108 | packageCache.put(a,cache); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | if(cache.containsKey(p)) |
aoqi@0 | 112 | return (A)cache.get(p); |
aoqi@0 | 113 | else { |
aoqi@0 | 114 | A ann = LocatableAnnotation.create(p.getAnnotation(a),srcPos); |
aoqi@0 | 115 | cache.put(p,ann); |
aoqi@0 | 116 | return ann; |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | public Class getClassValue(Annotation a, String name) { |
aoqi@0 | 121 | try { |
aoqi@0 | 122 | return (Class)a.annotationType().getMethod(name).invoke(a); |
aoqi@0 | 123 | } catch (IllegalAccessException e) { |
aoqi@0 | 124 | // impossible |
aoqi@0 | 125 | throw new IllegalAccessError(e.getMessage()); |
aoqi@0 | 126 | } catch (InvocationTargetException e) { |
aoqi@0 | 127 | // impossible |
aoqi@0 | 128 | throw new InternalError(Messages.CLASS_NOT_FOUND.format(a.annotationType(), e.getMessage())); |
aoqi@0 | 129 | } catch (NoSuchMethodException e) { |
aoqi@0 | 130 | throw new NoSuchMethodError(e.getMessage()); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | public Class[] getClassArrayValue(Annotation a, String name) { |
aoqi@0 | 135 | try { |
aoqi@0 | 136 | return (Class[])a.annotationType().getMethod(name).invoke(a); |
aoqi@0 | 137 | } catch (IllegalAccessException e) { |
aoqi@0 | 138 | // impossible |
aoqi@0 | 139 | throw new IllegalAccessError(e.getMessage()); |
aoqi@0 | 140 | } catch (InvocationTargetException e) { |
aoqi@0 | 141 | // impossible |
aoqi@0 | 142 | throw new InternalError(e.getMessage()); |
aoqi@0 | 143 | } catch (NoSuchMethodException e) { |
aoqi@0 | 144 | throw new NoSuchMethodError(e.getMessage()); |
aoqi@0 | 145 | } |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | protected String fullName(Method m) { |
aoqi@0 | 149 | return m.getDeclaringClass().getName()+'#'+m.getName(); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | } |