1.Proxy.newProxyInstance(source.getClass().getClassLoader(), source.getClass().getInterfaces(), myInvoHandler)
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
class MyInvocationHandler implements InvocationHandler{
private Object source ;
public MyInvocationHandler(Object source){
this.source = source ;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// System.out.println(proxy);// StackOverflowError course by recurse
System.out.println(proxy.getClass());//proxy-class,not source-class
if(null!=args){
for(Object param : args){
System.out.println(param);//method-input-param
}
}
System.out.println("u can do special things before method-execute");
Object obj = method.invoke(source, args);//Point:source,not proxy. proxy's meaning?
System.out.println("u can do special things after method-execute");
return obj ;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ClassProxyTest {
/**
* @param args
*/
public static void main(String[] args) {
PojoWithoutInterface pojo = new PojoWithoutInterface();
pojo.setName(1111);
Object pojoProxy = ClassProxyTest.getProxyInstance(pojo);
// pojo = (PojoWithoutInterface) pojoProxy ;// class cast exception
System.out.println(pojoProxy);
Object pojoProxy1 = ClassProxyTest.getProxyInstance(pojo);// not equals,new instance
System.out.println(pojoProxy.equals(pojoProxy1));//object 's method should be proxy
ClassImplInterface impl = new ClassImplInterface() ;
Object implProxy = ClassProxyTest.getProxyInstance(impl) ;
InterfaceA interA = (InterfaceA) implProxy ;
interA.methodA() ;
}
public static Object getProxyInstance(Object source){
InvocationHandler myInvoHandler = new MyInvocationHandler(source) ;
Object proxy = Proxy.newProxyInstance(source.getClass().getClassLoader(), source.getClass().getInterfaces(), myInvoHandler) ;
Method[] publicMethods = proxy.getClass().getMethods() ;// proxy only have source's interface-method,not all-method
if(null!=publicMethods){
for(Method mthd : publicMethods){
System.out.println(mthd.getName());
}
}
return proxy ;
}
}
class ClassImplInterface implements InterfaceA,InterfaceB{
@Override
public void methodA() {
System.out.println("methodA execute");
}
@Override
public void methodB() {
System.out.println("methodB execute");
}
public void methodC(){//proxy can not execute
System.out.println("self-method execute");
}
}
public class PojoWithoutInterface {
private int name ;
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
}
public interface InterfaceA {
void methodA() ;
}
public interface InterfaceB {
void methodB() ;
}
分享到:
相关推荐
class Proxy : public SubjectInterface { private: std::unique_ptr<SubjectInterface> real_subject; public: Proxy() : real_subject(std::make_unique()) {} void operation() override { // 在调用目标...
class Proxy : public Subject { private: RealSubject* _realSubject; public: Proxy() : _realSubject(new RealSubject()) {} ~Proxy() { delete _realSubject; } void request() override { // 在调用...
class Proxy implements Target { private Target realTarget; public Proxy(Target realTarget) { this.realTarget = realTarget; } @Override public void doSomething() { before(); realTarget....
public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...
public class proxy { public static void main(String[] args) throws Exception { HttpHost entry = new HttpHost("43.248.99.36", 62084); String resp= Executor.newInstance() .auth(entry, "fd", "123")//...
class DynamicProxy implements InvocationHandler { private Service target; public DynamicProxy(Service target) { this.target = target; } @Override public Object invoke(Object proxy, Method ...
- **创建代理对象**:使用`ClassProxy`类创建代理对象,这里的`ClassProxy`类需要实现通过反射创建类实例的功能。 - **执行初始化方法**:如果成功创建了类的实例,则调用`init`方法,并传入`HttpServletRequest`...
class Proxy implements Target { private Target target; public Proxy(Target target) { this.target = target; } @Override public void doSomething() { before(); target.doSomething(); after(); ...
class Proxy implements Programmer { private Programmer programmer; public Proxy(Programmer programmer) { this.programmer = programmer; } @Override public void develop() { bookTicket(); ...
public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...
public class ProxyService implements MyService { private MyService realService; public ProxyService(MyService realService) { this.realService = realService; } @Override public void doSomething...
class Proxy: def __init__(self): self._real_subject = RealSubject() def request(self): # 在调用真实对象之前可以进行额外操作 print("Proxy is preprocessing.") self._real_subject.request() # 在...
DATA: lo_proxy TYPE REF TO zcl_my_proxy_class. CREATE OBJECT lo_proxy. lo_proxy->set_parameter( 'PARAM1', 'Value1' ). lo_proxy->set_parameter( 'PARAM2', 'Value2' ). TRY. DATA(lr_result) = lo_proxy-...
class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { before...
public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...
class Proxy implements Target { private Target realTarget; public Proxy(Target realTarget) { this.realTarget = realTarget; } @Override public void doSomething() { before(); realTarget....
例如,你可能有一个基础类`BaseClass`,然后创建一个代理类`ProxyClass`,该类继承自`BaseClass`并在其上添加额外的功能。 ```csharp public class BaseClass { public virtual void Execute() { Console....
class Proxy implements Target { private Target realTarget; public Proxy(Target realTarget) { this.realTarget = realTarget; } @Override public void doSomething() { before(); realTarget....
public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...