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.InvocationHandler; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.lang.reflect.Proxy; aoqi@0: import java.lang.reflect.Modifier; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Location; aoqi@0: aoqi@0: /** aoqi@0: * {@link Annotation} that also implements {@link Locatable}. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public class LocatableAnnotation implements InvocationHandler, Locatable, Location { aoqi@0: private final Annotation core; aoqi@0: aoqi@0: private final Locatable upstream; aoqi@0: aoqi@0: /** aoqi@0: * Wraps the annotation into a proxy so that the returned object will also implement aoqi@0: * {@link Locatable}. aoqi@0: */ aoqi@0: public static A create( A annotation, Locatable parentSourcePos ) { aoqi@0: if(annotation==null) return null; aoqi@0: Class type = annotation.annotationType(); aoqi@0: if(quicks.containsKey(type)) { aoqi@0: // use the existing proxy implementation if available aoqi@0: return (A)quicks.get(type).newInstance(parentSourcePos,annotation); aoqi@0: } aoqi@0: aoqi@0: // otherwise take the slow route aoqi@0: aoqi@0: ClassLoader cl = SecureLoader.getClassClassLoader(LocatableAnnotation.class); aoqi@0: aoqi@0: try { aoqi@0: Class loadableT = Class.forName(type.getName(), false, cl); aoqi@0: if(loadableT !=type) aoqi@0: return annotation; // annotation type not loadable from this class loader aoqi@0: aoqi@0: return (A)Proxy.newProxyInstance(cl, aoqi@0: new Class[]{ type, Locatable.class }, aoqi@0: new LocatableAnnotation(annotation,parentSourcePos)); aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: // annotation not loadable aoqi@0: return annotation; aoqi@0: } catch (IllegalArgumentException e) { aoqi@0: // Proxy.newProxyInstance throws this if it cannot resolve this annotation aoqi@0: // in this classloader aoqi@0: return annotation; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: LocatableAnnotation(Annotation core, Locatable upstream) { aoqi@0: this.core = core; aoqi@0: this.upstream = upstream; aoqi@0: } aoqi@0: aoqi@0: public Locatable getUpstream() { aoqi@0: return upstream; aoqi@0: } aoqi@0: aoqi@0: public Location getLocation() { aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { aoqi@0: try { aoqi@0: if(method.getDeclaringClass()==Locatable.class) aoqi@0: return method.invoke(this,args); aoqi@0: if(Modifier.isStatic(method.getModifiers())) aoqi@0: // malicious code can pass in a static Method object. aoqi@0: // doing method.invoke() would end up executing it, aoqi@0: // so we need to protect against it. aoqi@0: throw new IllegalArgumentException(); aoqi@0: aoqi@0: return method.invoke(core,args); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: if(e.getTargetException()!=null) aoqi@0: throw e.getTargetException(); aoqi@0: throw e; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public String toString() { aoqi@0: return core.toString(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * List of {@link Quick} implementations keyed by their annotation type. aoqi@0: */ aoqi@0: private static final Map quicks = new HashMap(); aoqi@0: aoqi@0: static { aoqi@0: for( Quick q : Init.getAll() ) { aoqi@0: quicks.put(q.annotationType(),q); aoqi@0: } aoqi@0: } aoqi@0: }