How can I use a dynamic proxy for constructors that take arguments?

I tried using the Sun code posted on the proxy usage page , and I tried using DebugProxy to print which method is being called. The fact is that there must be an argument for the object for which I am creating a proxy server. If I try to create a proxy with an argument for the constructor, I get the following error:

Exception in thread "main" java.lang.ClassCastException: $ Proxy0 cannot be cast to myPackage.myClass

I created a proxy server as follows:


MyClass mc = (MyClass) DebugProxy.newInstance(new MyClass(props));

How to instantiate a proxy and still call the right constructor?

+3
source share
3

-, JDK, , . . .

, , Foo, FooImpl.

+4

- , ? - . / ( ), Aspect- (AOP). , AspectJ - , .

public interface SomeInterface {
    public void someMethod();
}

public MyClass implements SomeInterface {
...
}

// DebugProxy doesn't return your class, but a class which implements all of the interfaces
// you class implements
SomeInterface mc = (SomeInterface ) DebugProxy.newInstance(new MyClass(props));
+2

-, . .

Interfaces are not described by constructors, so what you want to do is not immediately feasible.

+2
source

Source: https://habr.com/ru/post/1710570/


All Articles