jaroslav@601: /* jaroslav@601: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@601: * jaroslav@601: * This code is free software; you can redistribute it and/or modify it jaroslav@601: * under the terms of the GNU General Public License version 2 only, as jaroslav@601: * published by the Free Software Foundation. Oracle designates this jaroslav@601: * particular file as subject to the "Classpath" exception as provided jaroslav@601: * by Oracle in the LICENSE file that accompanied this code. jaroslav@601: * jaroslav@601: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@601: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@601: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@601: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@601: * accompanied this code). jaroslav@601: * jaroslav@601: * You should have received a copy of the GNU General Public License version jaroslav@601: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@601: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@601: * jaroslav@601: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@601: * or visit www.oracle.com if you need additional information or have any jaroslav@601: * questions. jaroslav@601: */ jaroslav@601: jaroslav@601: package java.lang.reflect; jaroslav@601: jaroslav@601: jaroslav@601: /** jaroslav@601: * {@code Proxy} provides static methods for creating dynamic proxy jaroslav@601: * classes and instances, and it is also the superclass of all jaroslav@601: * dynamic proxy classes created by those methods. jaroslav@601: * jaroslav@601: *

To create a proxy for some interface {@code Foo}: jaroslav@601: *

jaroslav@601:  *     InvocationHandler handler = new MyInvocationHandler(...);
jaroslav@601:  *     Class proxyClass = Proxy.getProxyClass(
jaroslav@601:  *         Foo.class.getClassLoader(), new Class[] { Foo.class });
jaroslav@601:  *     Foo f = (Foo) proxyClass.
jaroslav@601:  *         getConstructor(new Class[] { InvocationHandler.class }).
jaroslav@601:  *         newInstance(new Object[] { handler });
jaroslav@601:  * 
jaroslav@601: * or more simply: jaroslav@601: *
jaroslav@601:  *     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
jaroslav@601:  *                                          new Class[] { Foo.class },
jaroslav@601:  *                                          handler);
jaroslav@601:  * 
jaroslav@601: * jaroslav@601: *

A dynamic proxy class (simply referred to as a proxy jaroslav@601: * class below) is a class that implements a list of interfaces jaroslav@601: * specified at runtime when the class is created, with behavior as jaroslav@601: * described below. jaroslav@601: * jaroslav@601: * A proxy interface is such an interface that is implemented jaroslav@601: * by a proxy class. jaroslav@601: * jaroslav@601: * A proxy instance is an instance of a proxy class. jaroslav@601: * jaroslav@601: * Each proxy instance has an associated invocation handler jaroslav@601: * object, which implements the interface {@link InvocationHandler}. jaroslav@601: * A method invocation on a proxy instance through one of its proxy jaroslav@601: * interfaces will be dispatched to the {@link InvocationHandler#invoke jaroslav@601: * invoke} method of the instance's invocation handler, passing the proxy jaroslav@601: * instance, a {@code java.lang.reflect.Method} object identifying jaroslav@601: * the method that was invoked, and an array of type {@code Object} jaroslav@601: * containing the arguments. The invocation handler processes the jaroslav@601: * encoded method invocation as appropriate and the result that it jaroslav@601: * returns will be returned as the result of the method invocation on jaroslav@601: * the proxy instance. jaroslav@601: * jaroslav@601: *

A proxy class has the following properties: jaroslav@601: * jaroslav@601: *

jaroslav@601: * jaroslav@601: *

A proxy instance has the following properties: jaroslav@601: * jaroslav@601: *

jaroslav@601: * jaroslav@601: *

Methods Duplicated in Multiple Proxy Interfaces

jaroslav@601: * jaroslav@601: *

When two or more interfaces of a proxy class contain a method with jaroslav@601: * the same name and parameter signature, the order of the proxy class's jaroslav@601: * interfaces becomes significant. When such a duplicate method jaroslav@601: * is invoked on a proxy instance, the {@code Method} object passed jaroslav@601: * to the invocation handler will not necessarily be the one whose jaroslav@601: * declaring class is assignable from the reference type of the interface jaroslav@601: * that the proxy's method was invoked through. This limitation exists jaroslav@601: * because the corresponding method implementation in the generated proxy jaroslav@601: * class cannot determine which interface it was invoked through. jaroslav@601: * Therefore, when a duplicate method is invoked on a proxy instance, jaroslav@601: * the {@code Method} object for the method in the foremost interface jaroslav@601: * that contains the method (either directly or inherited through a jaroslav@601: * superinterface) in the proxy class's list of interfaces is passed to jaroslav@601: * the invocation handler's {@code invoke} method, regardless of the jaroslav@601: * reference type through which the method invocation occurred. jaroslav@601: * jaroslav@601: *

If a proxy interface contains a method with the same name and jaroslav@601: * parameter signature as the {@code hashCode}, {@code equals}, jaroslav@601: * or {@code toString} methods of {@code java.lang.Object}, jaroslav@601: * when such a method is invoked on a proxy instance, the jaroslav@601: * {@code Method} object passed to the invocation handler will have jaroslav@601: * {@code java.lang.Object} as its declaring class. In other words, jaroslav@601: * the public, non-final methods of {@code java.lang.Object} jaroslav@601: * logically precede all of the proxy interfaces for the determination of jaroslav@601: * which {@code Method} object to pass to the invocation handler. jaroslav@601: * jaroslav@601: *

Note also that when a duplicate method is dispatched to an jaroslav@601: * invocation handler, the {@code invoke} method may only throw jaroslav@601: * checked exception types that are assignable to one of the exception jaroslav@601: * types in the {@code throws} clause of the method in all of jaroslav@601: * the proxy interfaces that it can be invoked through. If the jaroslav@601: * {@code invoke} method throws a checked exception that is not jaroslav@601: * assignable to any of the exception types declared by the method in one jaroslav@601: * of the proxy interfaces that it can be invoked through, then an jaroslav@601: * unchecked {@code UndeclaredThrowableException} will be thrown by jaroslav@601: * the invocation on the proxy instance. This restriction means that not jaroslav@601: * all of the exception types returned by invoking jaroslav@601: * {@code getExceptionTypes} on the {@code Method} object jaroslav@601: * passed to the {@code invoke} method can necessarily be thrown jaroslav@601: * successfully by the {@code invoke} method. jaroslav@601: * jaroslav@601: * @author Peter Jones jaroslav@601: * @see InvocationHandler jaroslav@601: * @since 1.3 jaroslav@601: */ jaroslav@601: public class Proxy implements java.io.Serializable { jaroslav@601: jaroslav@601: private static final long serialVersionUID = -2222568056686623797L; jaroslav@601: jaroslav@601: jaroslav@601: jaroslav@601: /** jaroslav@601: * the invocation handler for this proxy instance. jaroslav@601: * @serial jaroslav@601: */ jaroslav@601: protected InvocationHandler h; jaroslav@601: jaroslav@601: /** jaroslav@601: * Prohibits instantiation. jaroslav@601: */ jaroslav@601: private Proxy() { jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Constructs a new {@code Proxy} instance from a subclass jaroslav@601: * (typically, a dynamic proxy class) with the specified value jaroslav@601: * for its invocation handler. jaroslav@601: * jaroslav@601: * @param h the invocation handler for this proxy instance jaroslav@601: */ jaroslav@601: protected Proxy(InvocationHandler h) { jaroslav@601: this.h = h; jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Returns the {@code java.lang.Class} object for a proxy class jaroslav@601: * given a class loader and an array of interfaces. The proxy class jaroslav@601: * will be defined by the specified class loader and will implement jaroslav@601: * all of the supplied interfaces. If a proxy class for the same jaroslav@601: * permutation of interfaces has already been defined by the class jaroslav@601: * loader, then the existing proxy class will be returned; otherwise, jaroslav@601: * a proxy class for those interfaces will be generated dynamically jaroslav@601: * and defined by the class loader. jaroslav@601: * jaroslav@601: *

There are several restrictions on the parameters that may be jaroslav@601: * passed to {@code Proxy.getProxyClass}: jaroslav@601: * jaroslav@601: *

jaroslav@601: * jaroslav@601: *

If any of these restrictions are violated, jaroslav@601: * {@code Proxy.getProxyClass} will throw an jaroslav@601: * {@code IllegalArgumentException}. If the {@code interfaces} jaroslav@601: * array argument or any of its elements are {@code null}, a jaroslav@601: * {@code NullPointerException} will be thrown. jaroslav@601: * jaroslav@601: *

Note that the order of the specified proxy interfaces is jaroslav@601: * significant: two requests for a proxy class with the same combination jaroslav@601: * of interfaces but in a different order will result in two distinct jaroslav@601: * proxy classes. jaroslav@601: * jaroslav@601: * @param loader the class loader to define the proxy class jaroslav@601: * @param interfaces the list of interfaces for the proxy class jaroslav@601: * to implement jaroslav@601: * @return a proxy class that is defined in the specified class loader jaroslav@601: * and that implements the specified interfaces jaroslav@601: * @throws IllegalArgumentException if any of the restrictions on the jaroslav@601: * parameters that may be passed to {@code getProxyClass} jaroslav@601: * are violated jaroslav@601: * @throws NullPointerException if the {@code interfaces} array jaroslav@601: * argument or any of its elements are {@code null} jaroslav@601: */ jaroslav@601: public static Class getProxyClass(ClassLoader loader, jaroslav@601: Class... interfaces) jaroslav@601: throws IllegalArgumentException jaroslav@601: { jaroslav@604: throw new IllegalArgumentException(); jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Returns an instance of a proxy class for the specified interfaces jaroslav@601: * that dispatches method invocations to the specified invocation jaroslav@601: * handler. This method is equivalent to: jaroslav@601: *

jaroslav@601:      *     Proxy.getProxyClass(loader, interfaces).
jaroslav@601:      *         getConstructor(new Class[] { InvocationHandler.class }).
jaroslav@601:      *         newInstance(new Object[] { handler });
jaroslav@601:      * 
jaroslav@601: * jaroslav@601: *

{@code Proxy.newProxyInstance} throws jaroslav@601: * {@code IllegalArgumentException} for the same reasons that jaroslav@601: * {@code Proxy.getProxyClass} does. jaroslav@601: * jaroslav@601: * @param loader the class loader to define the proxy class jaroslav@601: * @param interfaces the list of interfaces for the proxy class jaroslav@601: * to implement jaroslav@601: * @param h the invocation handler to dispatch method invocations to jaroslav@601: * @return a proxy instance with the specified invocation handler of a jaroslav@601: * proxy class that is defined by the specified class loader jaroslav@601: * and that implements the specified interfaces jaroslav@601: * @throws IllegalArgumentException if any of the restrictions on the jaroslav@601: * parameters that may be passed to {@code getProxyClass} jaroslav@601: * are violated jaroslav@601: * @throws NullPointerException if the {@code interfaces} array jaroslav@601: * argument or any of its elements are {@code null}, or jaroslav@601: * if the invocation handler, {@code h}, is jaroslav@601: * {@code null} jaroslav@601: */ jaroslav@601: public static Object newProxyInstance(ClassLoader loader, jaroslav@601: Class[] interfaces, jaroslav@601: InvocationHandler h) jaroslav@601: throws IllegalArgumentException jaroslav@601: { jaroslav@601: if (h == null) { jaroslav@601: throw new NullPointerException(); jaroslav@601: } jaroslav@604: throw new IllegalArgumentException(); jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Returns true if and only if the specified class was dynamically jaroslav@601: * generated to be a proxy class using the {@code getProxyClass} jaroslav@601: * method or the {@code newProxyInstance} method. jaroslav@601: * jaroslav@601: *

The reliability of this method is important for the ability jaroslav@601: * to use it to make security decisions, so its implementation should jaroslav@601: * not just test if the class in question extends {@code Proxy}. jaroslav@601: * jaroslav@601: * @param cl the class to test jaroslav@601: * @return {@code true} if the class is a proxy class and jaroslav@601: * {@code false} otherwise jaroslav@601: * @throws NullPointerException if {@code cl} is {@code null} jaroslav@601: */ jaroslav@601: public static boolean isProxyClass(Class cl) { jaroslav@601: if (cl == null) { jaroslav@601: throw new NullPointerException(); jaroslav@601: } jaroslav@601: jaroslav@604: return false; jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Returns the invocation handler for the specified proxy instance. jaroslav@601: * jaroslav@601: * @param proxy the proxy instance to return the invocation handler for jaroslav@601: * @return the invocation handler for the proxy instance jaroslav@601: * @throws IllegalArgumentException if the argument is not a jaroslav@601: * proxy instance jaroslav@601: */ jaroslav@601: public static InvocationHandler getInvocationHandler(Object proxy) jaroslav@601: throws IllegalArgumentException jaroslav@601: { jaroslav@601: /* jaroslav@601: * Verify that the object is actually a proxy instance. jaroslav@601: */ jaroslav@601: if (!isProxyClass(proxy.getClass())) { jaroslav@601: throw new IllegalArgumentException("not a proxy instance"); jaroslav@601: } jaroslav@601: jaroslav@601: Proxy p = (Proxy) proxy; jaroslav@601: return p.h; jaroslav@601: } jaroslav@601: jaroslav@601: private static native Class defineClass0(ClassLoader loader, String name, jaroslav@601: byte[] b, int off, int len); jaroslav@601: }