带有输出参数的java代理方法调用

时间:2014-11-18 10:51:13

标签: java reflection methods arguments

我有问题。我创建了一个新的调用处理程序,我想设置方法的(输出)参数,但不成功。非常感谢!

我的代码:

class MyInvocationHandler extends InvocationHandler {
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                 if (method.getName().equals("example")) {
                     args[0] = "abcd"; //not successful
                     return "example";
                 }
      }
}
interface Test {
       String example(String s);
}
public static void main(String[] args) {
          MyInvocationHandler myhandler = new MyInvocationHandler();
          Test test = (Test) Proxy.newProxyInstance(loader, new Class[]{Test.class}, myhandler);
          String a = new String("ab");
          String ret = test.example(a);
          System.out.println(a); //a value is ab, not abcd! Why?
}

0 个答案:

没有答案