Search code examples
javadistributeddistributed-transactionsproxy-classes

What is Return type of method invocation?


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


Solution

  • 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.