站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题About the return type of invoke method in InvocationHandler

About the return type of invoke method in InvocationHandler

摘要: About the return type of invoke method in InvocationHandler


The return type of Proxy should be Object, when i ni debug mode, i see that the return type value is null, strange wasn't it? I still can call method in the interface, who can tell me why? And is that also legal when it return Exception?
I do a project using hivemind, using Interceptor. When i Call Proxy, it returns the "com.sun.jdi.InvocationException occurred invoking method", is that legal?


Object 是 null ,是说明Object 没有创建或被实例化


methods of Proxy are all declared as static ,so you can call method in the interface


but i can do cast and call the method in the interface
Class cls = proxyObj.getClass();
s = Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), this);
((IService) s).print();// that runs ok no error or exceptions.


UpUp...


大概因为你用到的这些方法是定义成静态方法了


I have paste all my code there, hoping some one can tell me the reason :(

Using InvocationHandler

1. Create an interface
public interface IService {
public void printInfo(String info);
public void printSelf();
}


2. Create an implement
public class PringService implements IService {

public void printInfo(String info) {
System.out.println(info);
}

public void printSelf() {
System.out.println(this.toString());
}

}

3. Create an invocation handler
public class MyInvocation implements InvocationHandler {

private Object proxyObj;
/**
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object obj, Method method, Object[] params)
throws Throwable {
Object result = null;
System.out.println("Before method");
method.invoke(proxyObj, params);
System.out.println("After method");
return result;
}

public Object bind(Object obj) {
this.proxyObj = obj;
Class cls = proxyObj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), this);
}

}

4. How to use
public class MyInvocation implements InvocationHandler {

private Object proxyObj;
/**
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object obj, Method method, Object[] params)
throws Throwable {
Object result = null;
System.out.println("Before method");
method.invoke(proxyObj, params);
System.out.println("After method");
return result;
}

public Object bind(Object obj) {
this.proxyObj = obj;
Class cls = proxyObj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), this);
}

}

5. Test & Use
public class TestInvocation extends TestCase {
public void testAll() {
MyInvocation invo = new MyInvocation();
Object prin;
try {
prin = Class.forName("com.invocation.test.PringService").newInstance();
Object s = invo.bind(prin);
((IService) s).printInfo("sa");
((IService) s).printSelf();
} catch (ClassNotFoundException e) {
assertTrue(e.getLocalizedMessage(), false);
} catch (InstantiationException e) {
assertTrue(e.getLocalizedMessage(), false);
} catch (IllegalAccessException e) {
e.printStackTrace();
}

}
}


Up Up...


>return type value is null
Means that the called mathod return null, not anything else.

>I still can call method in the interface
interface can not contain any code, it is just define.

>And is that also legal when it return Exception?
Exception can be a object, so it can be return. but you know, exception offen us for throw instead of return.

>I do a project using hivemind, using Interceptor. When i Call Proxy, it returns the "com.sun.jdi.InvocationException occurred invoking method", is that legal?
It seem like your calling mathod is illeagal, call mathod by a Proxy, you should be know the mathod name, params and return value.


thx, but still have a question:
Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), this); This return null, I haven't call any method yet. So why did you say:"Means that the called method return null, not anything else."


package test;

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

/**
* @author Jimmy
*/
public class Test {
public static void main(String[] args) throws Exception {
new Test().testAll();
}
public void testAll() throws Exception {
MyInvocation invo = new MyInvocation();
Object prin;
prin = Class.forName("test.PringService").newInstance();
Object s = invo.bind(prin);
((IService) s).printInfo("sa");
((IService) s).printSelf();
}
}

interface IService {
public void printInfo(String info);
public void printSelf();
}

class PringService implements IService {
public void printInfo(String info) {
System.out.println(info);
}

public void printSelf() {
System.out.println(this.toString());
}
}

class MyInvocation implements InvocationHandler {
private Object proxyObj;
public Object invoke(Object obj, Method method, Object[] params) throws Throwable {
Object result = null;
System.out.println("Before method");
method.invoke(proxyObj, params);
System.out.println("After method");
return result;
}

public Object bind(Object obj) {
this.proxyObj = obj;
Class cls = proxyObj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), this);
}
}

the result after it run
Before method
sa
After method
Before method
test.PringService@1e63e3d
After method

it ok for return value!!


Yes, you do in debug mode and add watching to "s". It's null. My question is why that is null. If this resolved, the one in hivemind project can also be resolved.


The code I commit previous can also runs well. U can tear them to five files and run the test to see the result.


up up!!


should be:
result=method.invoke(proxyObj, params);

in invoke method.


Yes, but u know, if this can be right, Cast a null Object can call the method. It's a BUG in J2se of Invocation, haha.


O, my god , i get it. while in debug mode, it seem that the vm does not understand the return type of invoke, then runtime show a window for debuger to choose the object type.

it 's really funny. haha


↑返回目录
前一篇: 请教一个关于Servlet的很奇怪的问题..~~~~~~~~~~!!!!!!!!!!!!!!!
后一篇: 100分求教:十万条数据,如何一次性插入数据库,才能保证效率?