Javareflect-proxy-isproxyclass
提供:Dev Guides
java.lang.reflect.Proxy.isProxyClass()メソッドの例
説明
- java.lang.reflect.Proxy.isProxyClass(Class <?> cl)*メソッドは、指定されたクラスがgetProxyClassメソッドまたはnewProxyInstanceメソッドを使用してプロキシクラスとして動的に生成された場合にのみtrueを返します。
宣言
以下は* java.lang.reflect.Proxy.isProxyClass(Class <?> cl)*メソッドの宣言です。
public static boolean isProxyClass(Class<?> cl)
パラメーター
*cl* -テストするクラス。
返品
クラスがプロキシクラスである場合はtrue、そうでない場合はfalse。
例外
*NullPointerException* -clがnullの場合。
例
次の例は、java.lang.reflect.Proxy.isProxyClass(Class <?> cl)メソッドの使用方法を示しています。
package com.finddevguides;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyDemo {
public static void main(String[] args)
throws IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException,
NoSuchMethodException, SecurityException {
InvocationHandler handler = new SampleInvocationHandler() ;
Class proxyClass = Proxy.getProxyClass(
SampleClass.class.getClassLoader(), new Class[] { SampleInterface.class });
SampleInterface proxy = (SampleInterface) proxyClass.
getConstructor(new Class[] { InvocationHandler.class }).
newInstance(new Object[] { handler });
System.out.println(Proxy.isProxyClass(proxyClass));
proxy.showMessage();
}
}
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");
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
true
Welcome to finddevguides