📄 proxy.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:35:26 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class Proxy</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/Proxy.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../../java/lang/reflect/Modifier.html"><B>PREV CLASS</B></A> <A HREF="../../../java/lang/reflect/ReflectPermission.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="Proxy.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | <A HREF="#field_summary">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: <A HREF="#field_detail">FIELD</A> | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.lang.reflect</FONT><BR>Class Proxy</H2><PRE><A HREF="../../../java/lang/Object.html">java.lang.Object</A> | +--<B>java.lang.reflect.Proxy</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../../java/io/Serializable.html">Serializable</A></DD></DL><HR><DL><DT>public class <B>Proxy</B><DT>extends <A HREF="../../../java/lang/Object.html">Object</A><DT>implements <A HREF="../../../java/io/Serializable.html">Serializable</A></DL><P><code>Proxy</code> provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods. <p>To create a proxy for some interface <code>Foo</code>: <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> <p>A <i>dynamic proxy class</i> (simply referred to as a <i>proxy class</i> below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below. A <i>proxy interface</i> is such an interface that is implemented by a proxy class. A <i>proxy instance</i> is an instance of a proxy class. Each proxy instance has an associated <i>invocation handler</i> object, which implements the interface <A HREF="../../../java/lang/reflect/InvocationHandler.html"><CODE>InvocationHandler</CODE></A>. A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the <A HREF="../../../java/lang/reflect/InvocationHandler.html#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])"><CODE>invoke</CODE></A> method of the instance's invocation handler, passing the proxy instance, a <code>java.lang.reflect.Method</code> object identifying the method that was invoked, and an array of type <code>Object</code> containing the arguments. The invocation handler processes the encoded method invocation as appropriate and the result that it returns will be returned as the result of the method invocation on the proxy instance. <p>A proxy class has the following properties: <ul> <li>Proxy classes are public, final, and not abstract. <li>The unqualified name of a proxy class is unspecified. The space of class names that begin with the string <code>"$Proxy"</code> should be, however, reserved for proxy classes. <li>A proxy class extends <code>java.lang.reflect.Proxy</code>. <li>A proxy class implements exactly the interfaces specified at its creation, in the same order. <li>If a proxy class implements a non-public interface, then it will be defined in the same package as that interface. Otherwise, the package of a proxy class is also unspecified. Note that package sealing will not prevent a proxy class from being successfully defined in a particular package at runtime, and neither will classes already defined in the same class loader and the same package with particular signers. <li>Since a proxy class implements all of the interfaces specified at its creation, invoking <code>getInterfaces</code> on its <code>Class</code> object will return an array containing the same list of interfaces (in the order specified at its creation), invoking <code>getMethods</code> on its <code>Class</code> object will return an array of <code>Method</code> objects that include all of the methods in those interfaces, and invoking <code>getMethod</code> will find methods in the proxy interfaces as would be expected. <li>The <A HREF="../../../java/lang/reflect/Proxy.html#isProxyClass(java.lang.Class)"><CODE>Proxy.isProxyClass</CODE></A> method will return true if it is passed a proxy class-- a class returned by <code>Proxy.getProxyClass</code> or the class of an object returned by <code>Proxy.newProxyInstance</code>-- and false otherwise. <li>The <code>java.security.ProtectionDomain</code> of a proxy class is the same as that of system classes loaded by the bootstrap class loader, such as <code>java.lang.Object</code>, because the code for a proxy class is generated by trusted system code. This protection domain will typically be granted <code>java.security.AllPermission</code>. <li>Each proxy class has one public constructor that takes one argument, an implementation of the interface <A HREF="../../../java/lang/reflect/InvocationHandler.html"><CODE>InvocationHandler</CODE></A>, to set the invocation handler for a proxy instance. Rather than having to use the reflection API to access the public constructor, a proxy instance can be also be created by calling the <A HREF="../../../java/lang/reflect/Proxy.html#newProxyInstance(java.lang.ClassLoader, java.lang.Class[], java.lang.reflect.InvocationHandler)"><CODE>Proxy.newInstance</CODE></A> method, which combines the actions of calling <A HREF="../../../java/lang/reflect/Proxy.html#getProxyClass(java.lang.ClassLoader, java.lang.Class[])"><CODE>Proxy.getProxyClass</CODE></A> with invoking the constructor with an invocation handler. </ul> <p>A proxy instance has the following properties: <ul> <li>Given a proxy instance <code>proxy</code> and one of the interfaces implemented by its proxy class <code>Foo</code>, the following expression will return true: <pre> <code>proxy instanceof Foo</code> </pre> and the following cast operation will succeed (rather than throwing a <code>ClassCastException</code>): <pre> <code>(Foo) proxy</code> </pre> <li>Each proxy instance has an associated invocation handler, the one that was passed to its constructor. The static <A HREF="../../../java/lang/reflect/Proxy.html#getInvocationHandler(java.lang.Object)"><CODE>Proxy.getInvocationHandler</CODE></A> method will return the invocation handler associated with the proxy instance passed as its argument. <li>An interface method invocation on a proxy instance will be encoded and dispatched to the invocation handler's <A HREF="../../../java/lang/reflect/InvocationHandler.html#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])"><CODE>invoke</CODE></A> method as described in the documentation for that method. <li>An invocation of the <code>hashCode</code>, <code>equals</code>, or <code>toString</code> methods declared in <code>java.lang.Object</code> on a proxy instance will be encoded and dispatched to the invocation handler's <code>invoke</code> method in the same manner as interface method invocations are encoded and dispatched, as described above. The declaring class of the <code>Method</code> object passed to <code>invoke</code> will be <code>java.lang.Object</code>. Other public methods of a proxy
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -