In fact, little can be done with the actual proxy. Nevertheless, this is part of the call context, and you can use it to obtain information about the proxy server using reflection or use it in subsequent calls (when calling another method with this proxy server or as a result.
: acccount, , deposit() , :
private interface Account {
public Account deposit (double value);
public double getBalance ();
}
Handler:
private class ExampleInvocationHandler implements InvocationHandler {
private double balance;
@Override
public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
if ("deposit".equals(method.getName())) {
Double value = (Double) args[0];
System.out.println("deposit: " + value);
balance += value;
return proxy;
}
if ("getBalance".equals(method.getName())) {
return balance;
}
return null;
}
}
:
Account account = (Account) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {Account.class, Serializable.class},
new ExampleInvocationHandler());
account.deposit(5000).deposit(4000).deposit(-2500);
System.out.println("Balance: " + account.getBalance());
: :
for (Class<?> interfaceType : account.getClass().getInterfaces()) {
System.out.println("- " + interfaceType);
}
: "this" , .