- java.lang.reflect 包类
- java.lang.reflect - 主页
- java.lang.reflect - AccessibleObject
- java.lang.reflect - 阵列
- java.lang.reflect - Constructor<T>
- java.lang.reflect - 字段
- java.lang.reflect - 方法
- java.lang.reflect - 修饰符
- java.lang.reflect - 代理
- java.lang.reflect 包额外信息
- java.lang.reflect - 接口
- java.lang.reflect - 异常
- java.lang.reflect - 错误
- java.lang.reflect 实用资源
- java.lang.reflect - 快速指南
- java.lang.reflect - 实用资源
- java.lang.reflect - 讨论
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.tutorialspoint;
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 TutorialsPoint");
return null;
}
}
interface SampleInterface {
void showMessage();
}
class SampleClass implements SampleInterface {
public void showMessage(){
System.out.println("Hello World");
}
}
让我们编译并运行上述程序,这将产生以下结果 −
true Welcome to TutorialsPoint
java_reflect_proxy.htm
广告