aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.model.annotation; aoqi@0: aoqi@0: import java.lang.annotation.Annotation; aoqi@0: import java.lang.reflect.Field; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.lang.reflect.Type; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: /** aoqi@0: * {@link AnnotationReader} that uses {@code java.lang.reflect} to aoqi@0: * read annotations from class files. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi (kk@kohsuke.org) aoqi@0: */ aoqi@0: public final class RuntimeInlineAnnotationReader extends AbstractInlineAnnotationReaderImpl aoqi@0: implements RuntimeAnnotationReader { aoqi@0: aoqi@0: public A getFieldAnnotation(Class annotation, Field field, Locatable srcPos) { aoqi@0: return LocatableAnnotation.create(field.getAnnotation(annotation),srcPos); aoqi@0: } aoqi@0: aoqi@0: public boolean hasFieldAnnotation(Class annotationType, Field field) { aoqi@0: return field.isAnnotationPresent(annotationType); aoqi@0: } aoqi@0: aoqi@0: public boolean hasClassAnnotation(Class clazz, Class annotationType) { aoqi@0: return clazz.isAnnotationPresent(annotationType); aoqi@0: } aoqi@0: aoqi@0: public Annotation[] getAllFieldAnnotations(Field field, Locatable srcPos) { aoqi@0: Annotation[] r = field.getAnnotations(); aoqi@0: for( int i=0; i A getMethodAnnotation(Class annotation, Method method, Locatable srcPos) { aoqi@0: return LocatableAnnotation.create(method.getAnnotation(annotation),srcPos); aoqi@0: } aoqi@0: aoqi@0: public boolean hasMethodAnnotation(Class annotation, Method method) { aoqi@0: return method.isAnnotationPresent(annotation); aoqi@0: } aoqi@0: aoqi@0: public Annotation[] getAllMethodAnnotations(Method method, Locatable srcPos) { aoqi@0: Annotation[] r = method.getAnnotations(); aoqi@0: for( int i=0; i A getMethodParameterAnnotation(Class annotation, Method method, int paramIndex, Locatable srcPos) { aoqi@0: Annotation[] pa = method.getParameterAnnotations()[paramIndex]; aoqi@0: for( Annotation a : pa ) { aoqi@0: if(a.annotationType()==annotation) aoqi@0: return LocatableAnnotation.create((A)a,srcPos); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public A getClassAnnotation(Class a, Class clazz, Locatable srcPos) { aoqi@0: return LocatableAnnotation.create(((Class)clazz).getAnnotation(a),srcPos); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Cache for package-level annotations. aoqi@0: */ aoqi@0: private final Map,Map> packageCache = aoqi@0: new HashMap,Map>(); aoqi@0: aoqi@0: public A getPackageAnnotation(Class a, Class clazz, Locatable srcPos) { aoqi@0: Package p = clazz.getPackage(); aoqi@0: if(p==null) return null; aoqi@0: aoqi@0: Map cache = packageCache.get(a); aoqi@0: if(cache==null) { aoqi@0: cache = new HashMap(); aoqi@0: packageCache.put(a,cache); aoqi@0: } aoqi@0: aoqi@0: if(cache.containsKey(p)) aoqi@0: return (A)cache.get(p); aoqi@0: else { aoqi@0: A ann = LocatableAnnotation.create(p.getAnnotation(a),srcPos); aoqi@0: cache.put(p,ann); aoqi@0: return ann; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public Class getClassValue(Annotation a, String name) { aoqi@0: try { aoqi@0: return (Class)a.annotationType().getMethod(name).invoke(a); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: // impossible aoqi@0: throw new IllegalAccessError(e.getMessage()); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: // impossible aoqi@0: throw new InternalError(Messages.CLASS_NOT_FOUND.format(a.annotationType(), e.getMessage())); aoqi@0: } catch (NoSuchMethodException e) { aoqi@0: throw new NoSuchMethodError(e.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public Class[] getClassArrayValue(Annotation a, String name) { aoqi@0: try { aoqi@0: return (Class[])a.annotationType().getMethod(name).invoke(a); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: // impossible aoqi@0: throw new IllegalAccessError(e.getMessage()); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: // impossible aoqi@0: throw new InternalError(e.getMessage()); aoqi@0: } catch (NoSuchMethodException e) { aoqi@0: throw new NoSuchMethodError(e.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected String fullName(Method m) { aoqi@0: return m.getDeclaringClass().getName()+'#'+m.getName(); aoqi@0: } aoqi@0: }