Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从代理调用方法调用另一个方法_Java - Fatal编程技术网

Java 从代理调用方法调用另一个方法

Java 从代理调用方法调用另一个方法,java,Java,我已经实现了名为clientcontainerinholidationhandler的代理。它用于我的类SocketContainer的对象,这些对象实现了接口IClient,该接口具有方法public int getId() 现在,我尝试在代理下调用当前对象的一个方法,该方法的类型为SocketContainer我在下面的注释中显示的getId()方法(请参见下面的代码)。这可能吗 public class ClientContainerInvocationHandler implements

我已经实现了名为
clientcontainerinholidationhandler
的代理。它用于我的类
SocketContainer
的对象,这些对象实现了接口
IClient
,该接口具有方法
public int getId()

现在,我尝试在代理下调用当前对象的一个方法,该方法的类型为
SocketContainer
我在下面的注释中显示的
getId()
方法(请参见下面的代码)。这可能吗

public class ClientContainerInvocationHandler implements InvocationHandler {

   private final Object target;

   private ClientContainerInvocationHandler(final Object object) {
     this.target = object;
   }

   public static Object createProxy(final Object object) {
     final Object proxy = Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), new ClientContainerInvocationHandler(object));
     return proxy;
   }

   @Override
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
     if (method.getName().equals("addMessage")) {
       // invoke on the current object
       // the method getID and retrieve the result here
       // I have tried this : 
       //   Method m =target.getClass().getMethod("getId"); 
       // but I get exception if I do this
    }
  }
}
完全例外:

java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy5.getID(Unknown Source)
at com.project.chat_system.ChatRoom.addClient(ChatRoom.java:18)
at com.project.chat_system_tests.RealTest.testMessagesReceived(RealTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)


我想你在找这样的东西

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getName().equals("addMessage")) {
    // invoke on the current object
    //the method getID and retrieve the result here
    Method getIdMethod = proxy.getClass().getMethod("getID");

    //proxy is the object in which you want to call your method on
    SomeResult result = getIdMethod.invoke(proxy);

    //if you need to pass arguments
    SomeResult result = getIdMethod.invoke(proxy, args);
    }

您得到了什么异常?$Proxy7.getID(未知源)、java.lang.reflect.UndeclaredThrowableException请将异常和所有详细信息添加到您的问题中。这将调用传递给对象的方法,我需要其他方法。@Joel根据您的注释进行了更新。这不起作用,我已经尝试过了,因为它使$Proxy5.getID()这就是我提到的例外
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getName().equals("addMessage")) {
    // invoke on the current object
    //the method getID and retrieve the result here
    Method getIdMethod = proxy.getClass().getMethod("getID");

    //proxy is the object in which you want to call your method on
    SomeResult result = getIdMethod.invoke(proxy);

    //if you need to pass arguments
    SomeResult result = getIdMethod.invoke(proxy, args);
    }