Javareflect-proxy-getinvocationhandler
提供:Dev Guides
java.lang.reflect.Proxy.getInvocationHandler()メソッドの例
説明
- java.lang.reflect.Proxy.getInvocationHandler(Object proxy)*メソッドは、指定されたプロキシインスタンスの呼び出しハンドラーを返します。
宣言
次に、* java.lang.reflect.Proxy.getInvocationHandler(Object proxy)*メソッドの宣言を示します。
public static InvocationHandler getInvocationHandler(Object proxy)
throws IllegalArgumentException
パラメーター
*proxy* -呼び出しハンドラを返すプロキシインスタンス。
返品
プロキシインスタンスの呼び出しハンドラ。
例外
*IllegalArgumentException* -引数がプロキシインスタンスではない場合。
例
次の例は、java.lang.reflect.Proxy.getInvocationHandler(Object proxy)メソッドの使用方法を示しています。
package com.finddevguides;
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 finddevguides");
return null;
}
}
interface SampleInterface {
void showMessage();
}
class SampleClass implements SampleInterface {
public void showMessage(){
System.out.println("Hello World");
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
com.finddevguides.SampleInvocationHandler