📄 proxy.java
字号:
/* Proxy.java -- build a proxy class that implements reflected interfaces Copyright (C) 2001, 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.lang.reflect;import java.io.Serializable;import java.security.ProtectionDomain;import java.util.Map;import java.util.HashMap;import java.util.Set;import java.util.HashSet;import java.util.Iterator;import gnu.classpath.Configuration;import gnu.java.lang.reflect.TypeSignature;/** * This class allows you to dynamically create an instance of any (or * even multiple) interfaces by reflection, and decide at runtime * how that instance will behave by giving it an appropriate * {@link InvocationHandler}. Proxy classes serialize specially, so * that the proxy object can be reused between VMs, without requiring * a persistent copy of the generated class code. * * <h3>Creation</h3> * To create a proxy for some interface Foo: * * <pre> * InvocationHandler handler = new MyInvocationHandler(...); * Class proxyClass = Proxy.getProxyClass( * Foo.class.getClassLoader(), new Class[] { Foo.class }); * Foo f = (Foo) proxyClass * .getConstructor(new Class[] { InvocationHandler.class }) * .newInstance(new Object[] { handler }); * </pre> * or more simply: * <pre> * Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), * new Class[] { Foo.class }, * handler); * </pre> * * <h3>Dynamic Proxy Classes</h3> * A dynamic proxy class is created at runtime, and has the following * properties: * <ul> * <li>The class is <code>public</code> and <code>final</code>, * and is neither <code>abstract</code> nor an inner class.</li> * <li>The class has no canonical name (there is no formula you can use * to determine or generate its name), but begins with the * sequence "$Proxy". Abuse this knowledge at your own peril. * (For now, '$' in user identifiers is legal, but it may not * be that way forever. You weren't using '$' in your * identifiers, were you?)</li> * <li>The class extends Proxy, and explicitly implements all the * interfaces specified at creation, in order (this is important * for determining how method invocation is resolved). Note that * a proxy class implements {@link Serializable}, at least * implicitly, since Proxy does, but true serial behavior * depends on using a serializable invocation handler as well.</li> * <li>If at least one interface is non-public, the proxy class * will be in the same package. Otherwise, the package is * unspecified. This will work even if the package is sealed * from user-generated classes, because Proxy classes are * generated by a trusted source. Meanwhile, the proxy class * belongs to the classloader you designated.</li> * <li>Reflection works as expected: {@link Class#getInterfaces()} and * {@link Class#getMethods()} work as they do on normal classes.</li> * <li>The method {@link #isProxyClass()} will distinguish between * true proxy classes and user extensions of this class. It only * returns true for classes created by {@link #getProxyClass}.</li> * <li>The {@link ProtectionDomain} of a proxy class is the same as for * bootstrap classes, such as Object or Proxy, since it is created by * a trusted source. This protection domain will typically be granted * {@link java.security.AllPermission}. But this is not a security * risk, since there are adequate permissions on reflection, which is * the only way to create an instance of the proxy class.</li> * <li>The proxy class contains a single constructor, which takes as * its only argument an {@link InvocationHandler}. The method * {@link #newInstance} is shorthand to do the necessary * reflection.</li> * </ul> * * <h3>Proxy Instances</h3> * A proxy instance is an instance of a proxy class. It has the * following properties, many of which follow from the properties of a * proxy class listed above: * <ul> * <li>For a proxy class with Foo listed as one of its interfaces, the * expression <code>proxy instanceof Foo</code> will return true, * and the expression <code>(Foo) proxy</code> will succeed without * a {@link ClassCastException}.</li> * <li>Each proxy instance has an invocation handler, which can be * accessed by {@link #getInvocationHandler(Object)}. Any call * to an interface method, including {@link Object#hashcode()}, * {@link Object#equals(Object)}, or {@link Object#toString()}, * but excluding the public final methods of Object, will be * encoded and passed to the {@link InvocationHandler#invoke} * method of this handler.</li> * </ul> * * <h3>Inheritance Issues</h3> * A proxy class may inherit a method from more than one interface. * The order in which interfaces are listed matters, because it determines * which reflected {@link Method} object will be passed to the invocation * handler. This means that the dynamically generated class cannot * determine through which interface a method is being invoked.<p> * * In short, if a method is declared in Object (namely, hashCode, * equals, or toString), then Object will be used; otherwise, the * leftmost interface that inherits or declares a method will be used, * even if it has a more permissive throws clause than what the proxy * class is allowed. Thus, in the invocation handler, it is not always * safe to assume that every class listed in the throws clause of the * passed Method object can safely be thrown; fortunately, the Proxy * instance is robust enough to wrap all illegal checked exceptions in * {@link UndeclaredThrowableException}. * * @see InvocationHandler * @see UndeclaredThrowableException * @see Class * @author Eric Blake <ebb9@email.byu.edu> * @since 1.3 * @status updated to 1.4, except for the use of ProtectionDomain */public class Proxy implements Serializable{ /** * Compatible with JDK 1.3+. */ private static final long serialVersionUID = -2222568056686623797L; /** * Map of ProxyType to proxy class. * * @XXX This prevents proxy classes from being garbage collected. * java.util.WeakHashSet is not appropriate, because that collects the * keys, but we are interested in collecting the elements. */ private static final Map proxyClasses = new HashMap(); /** * The invocation handler for this proxy instance. For Proxy, this * field is unused, but it appears here in order to be serialized in all * proxy classes. * * <em>NOTE</em>: This implementation is more secure for proxy classes * than what Sun specifies. Sun does not require h to be immutable, but * this means you could change h after the fact by reflection. However, * by making h immutable, we may break non-proxy classes which extend * Proxy. * @serial invocation handler associated with this proxy instance */ protected InvocationHandler h; /** * Constructs a new Proxy from a subclass (usually a proxy class), * with the specified invocation handler. * * <em>NOTE</em>: This throws a NullPointerException if you attempt * to create a proxy instance with a null handler using reflection. * This behavior is not yet specified by Sun; see Sun Bug 4487672. * * @param handler the invocation handler, may be null if the subclass * is not a proxy class * @throws NullPointerException if handler is null and this is a proxy * instance */ protected Proxy(InvocationHandler handler) { if (handler == null && isProxyClass(getClass())) throw new NullPointerException("invalid handler"); h = handler; } /** * Returns the proxy {@link Class} for the given ClassLoader and array * of interfaces, dynamically generating it if necessary. * * There are several restrictions on this method, the violation of * which will result in an IllegalArgumentException or * NullPointerException: * <ul> * <li>All objects in `interfaces' must represent distinct interfaces. * Classes, primitive types, null, and duplicates are forbidden.</li> * <li>The interfaces must be visible in the specified ClassLoader. * In other words, for each interface i: * <code>Class.forName(i.getName(), false, loader) == i</code> * must be true.</li> * <li>All non-public interfaces (if any) must reside in the same * package, or the proxy class would be non-instantiable. If * there are no non-public interfaces, the package of the proxy * class is unspecified.</li> * <li>All interfaces must be compatible - if two declare a method * with the same name and parameters, the return type must be * the same and the throws clause of the proxy class will be * the maximal subset of subclasses of the throws clauses for * each method that is overridden.</li> * <li>VM constraints limit the number of interfaces a proxy class * may directly implement (however, the indirect inheritance * of {@link Serializable} does not count against this limit). * Even though most VMs can theoretically have 65535 * superinterfaces for a class, the actual limit is smaller * because a class's constant pool is limited to 65535 entries, * and not all entries can be interfaces.</li> * </ul><p> * * Note that different orders of interfaces produce distinct classes. * * @param loader the class loader to define the proxy class in; null * implies the bootstrap class loader * @param interfaces the array of interfaces the proxy class implements, * may be empty, but not null * @return the Class object of the proxy class * @throws IllegalArgumentException if the constraints above were * violated, except for problems with null * @throws NullPointerException if `interfaces' is null or contains * a null entry */ // synchronized so that we aren't trying to build the same class // simultaneously in two threads public static synchronized Class getProxyClass(ClassLoader loader, Class[] interfaces) { interfaces = (Class[]) interfaces.clone(); ProxyType pt = new ProxyType(loader, interfaces); Class clazz = (Class) proxyClasses.get(pt); if (clazz == null) { if (Configuration.HAVE_NATIVE_GET_PROXY_CLASS) clazz = getProxyClass0(loader, interfaces); else { ProxyData data = (Configuration.HAVE_NATIVE_GET_PROXY_DATA ? getProxyData0(loader, interfaces) : ProxyData.getProxyData(pt)); // FIXME workaround for bug in gcj 3.0.x // Not needed with the latest gcj from cvs //clazz = (Configuration.HAVE_NATIVE_GENERATE_PROXY_CLASS // ? generateProxyClass0(loader, data) // : new ClassFactory(data).generate(loader)); if (Configuration.HAVE_NATIVE_GENERATE_PROXY_CLASS) clazz = generateProxyClass0(loader, data); else { ClassFactory cf = new ClassFactory(data); clazz = cf.generate(loader); } } Object check = proxyClasses.put(pt, clazz); // assert check == null && clazz != null; if (check != null || clazz == null) throw new InternalError(/*"Fatal flaw in getProxyClass"*/); } return clazz; } /** * Combines several methods into one. This is equivalent to: * <pre> * Proxy.getProxyClass(loader, interfaces) * .getConstructor(new Class[] {InvocationHandler.class}) * .newInstance(new Object[] {handler}); * </pre> * except that it will not fail with the normal problems caused * by reflection. It can still fail for the same reasons documented * in getProxyClass, or if handler is null. * * @param loader the class loader to define the proxy class in; null * implies the bootstrap class loader * @param interfaces the array of interfaces the proxy class implements, * may be empty, but not null * @param handler the invocation handler, may not be null * @return a proxy instance implementing the specified interfaces * @throws IllegalArgumentException if the constraints for getProxyClass * were violated, except for problems with null * @throws NullPointerException if `interfaces' is null or contains * a null entry, or if handler is null * @see #getProxyClass(ClassLoader, Class[]) * @see Class#getConstructor(Class[]) * @see Constructor#newInstance(Object[]) */ public static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler handler) { try { // getProxyClass() and Proxy() throw the necessary exceptions return getProxyClass(loader, interfaces) .getConstructor(new Class[] {InvocationHandler.class}) .newInstance(new Object[] {handler}); } catch (RuntimeException e) { // Let IllegalArgumentException, NullPointerException escape. // assert e instanceof IllegalArgumentException // || e instanceof NullPointerException; throw e; } catch (InvocationTargetException e) { // Let wrapped NullPointerException escape. // assert e.getTargetException() instanceof NullPointerException throw (NullPointerException) e.getCause(); } catch (Exception e) { // Covers InstantiationException, IllegalAccessException, // NoSuchMethodException, none of which should be generated // if the proxy class was generated correctly. // assert false; throw (Error) new InternalError("Unexpected: " + e).initCause(e); } } /** * Returns true if and only if the Class object is a dynamically created * proxy class (created by <code>getProxyClass</code> or by the * syntactic sugar of <code>newProxyInstance</code>). * * <p>This check is secure (in other words, it is not simply * <code>clazz.getSuperclass() == Proxy.class</code>), it will not * be spoofed by non-proxy classes that extend Proxy. * * @param clazz the class to check, must not be null * @return true if the class represents a proxy class * @throws NullPointerException if clazz is null */ // This is synchronized on the off chance that another thread is // trying to add a class to the map at the same time we read it. public static synchronized boolean isProxyClass(Class clazz) { if (! Proxy.class.isAssignableFrom(clazz)) return false; // This is a linear search, even though we could do an O(1) search // using new ProxyType(clazz.getClassLoader(), clazz.getInterfaces()). return proxyClasses.containsValue(clazz); } /** * Returns the invocation handler for the given proxy instance.<p> * * <em>NOTE</em>: We guarantee a non-null result if successful, * but Sun allows the creation of a proxy instance with a null * handler. See the comments for {@link #Proxy(InvocationHandler)}. * * @param proxy the proxy instance, must not be null * @return the invocation handler, guaranteed non-null. * @throws IllegalArgumentException if * <code>Proxy.isProxyClass(proxy.getClass())</code> returns false. * @throws NullPointerException if proxy is null */ public static InvocationHandler getInvocationHandler(Object proxy) { if (! isProxyClass(proxy.getClass())) throw new IllegalArgumentException("not a proxy instance"); return ((Proxy) proxy).h; } /** * Optional native method to replace (and speed up) the pure Java * implementation of getProxyClass. Only needed if * Configuration.HAVE_NATIVE_GET_PROXY_CLASS is true, this does the * work of both getProxyData0 and generateProxyClass0 with no * intermediate form in Java. The native code may safely assume that * this class must be created, and does not already exist. * * @param loader the class loader to define the proxy class in; null * implies the bootstrap class loader * @param interfaces the interfaces the class will extend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -