문제

I am using Proxy Pattern in Java (InvocationHandler), to implement a lock manager for a remote object, in the proxy class (which implements InvocationHandler). I am calling the remote object (here : flighRMConnection) :

if (method.getName().toLowerCase().contains("query")){
    lm.Lock(Thread.currentThread(), READ);
} else {
    lm.Lock(Thread.currentThread(), WRITE)
}               
method.invoke(flightRMConnection, args);

How can I check the value returned by the invocation?(there might be different types of results)

Thanks , Arian

도움이 되었습니까?

해결책

Well, the static return type of invoke is naturally Object. If you want to determine the dynamic type of an instance returned by the invocation, you can call getClass() on it to get the Class object representing its type.

If you need to inspect its contents, you can do further reflection using the Class (see getDeclaredFields(), etc.). If there's a known class or interface the object might extend, you could also check that with instanceof and then cast it.

Oh and don't forget to make sure the returned object isn't null before you call anything on it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top