0

I'm learning about java dynamic proxy, and here's my code:

//interface  Move.java
 public interface Move {
    public void testMove();
}

then the implementation class

public class Tank implements Move{
    public void testMove() {
        System.out.println("test in Tank");
    }
}

followed by

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class MoveHandler implements InvocationHandler{
    private Object target;

    public MoveHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
        System.out.println("in MoveHandler");
        Object ret;
        try {
            ret = method.invoke(target, args);
        }catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        System.out.println("after MoveHandler");
        return ret;
    }
}

test class

public class Main {
    public static void main(String[] args) {
        Move test = new Tank();
        MoveHandler proxy = new MoveHandler(test);
        Move real = (Move) Proxy.newProxyInstance(test.getClass().getClassLoader(), test.getClass().getInterfaces(), proxy);
        real.testMove();
    }
}

I can get the right result when I run the Main class. If I change method.invoke(target, args) into method.invoke(proxy, args), there will be thousand lines of exception and errors. What's the usage of the argument proxy in invoke(Object proxy, Method method, Object[] args) and how can I use it correctly?

2 Answers 2

2

The proxy argument is the object returned by Proxy.newProxyInstance(), on which the actual method (testMove()) is called. You usually don't need it, but it can be necessary to know which interfaces the proxy implements, for example.

Invoking the method on this argument is a really bad idea since it basically does a recursive method call : you invoke a method on a proxy, which calls the invocation handler, which calls the method on the proxy, which calls the handler, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it means the argument proxy represents the proxy object, but not the proxyed one?
Yes. proxy is... the proxy.
0

Using Java Reflection you create dynamic implementations of interfaces at runtime. You do so using the class java.lang.reflect.Proxy. The name of this class is why I refer to these dynamic interface implementations as dynamic proxies. Dynamic proxies can be used for many different purposes, e.g. database connection and transaction management, dynamic mock objects for unit testing, and other AOP-like method intercepting purposes.

Further explanation:

The proxy parameter passed to the invoke() method is the dynamic proxy object implementing the interface. Most often you don't need this object.

This is from this tutorial, which explains in a lot more detail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.