目录

static InvocationHandler getInvocationHandler(Object proxy)

描述 (Description)

java.lang.reflect.Proxy.getInvocationHandler(Object proxy)方法返回指定代理实例的调用处理程序。

声明 (Declaration)

以下是java.lang.reflect.Proxy.getInvocationHandler(Object proxy)方法的声明。

public static InvocationHandler getInvocationHandler(Object proxy)
   throws IllegalArgumentException

参数 (Parameters)

proxy - 返回调用处理程序的代理实例。

返回值 (Returns)

代理实例的调用处理程序。

异常 (Exceptions)

IllegalArgumentException - 如果参数不是代理实例。

例子 (Example)

以下示例显示了java.lang.reflect.Proxy.getInvocationHandler(Object proxy)方法的用法。

package com.iowiki;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyDemo {
   public static void main(String[] args) throws IllegalArgumentException {
      InvocationHandler handler = new SampleInvocationHandler() ;
      SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(
         SampleInterface.class.getClassLoader(),
         new Class[] { SampleInterface.class },
         handler);
      Class invocationHandler = Proxy.getInvocationHandler(proxy).getClass();
      System.out.println(invocationHandler.getName());
   }
}
class SampleInvocationHandler implements InvocationHandler {
   @Override
   public Object invoke(Object proxy, Method method, Object[] args)
      throws Throwable {
      System.out.println("Welcome to IoWiki");   
      return null;
   }
}
interface SampleInterface {
   void showMessage();
}
class SampleClass implements SampleInterface {
   public void showMessage(){
      System.out.println("Hello World");   
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

com.iowiki.SampleInvocationHandler
↑回到顶部↑
WIKI教程 @2018