Creating generic wrapper of return type
I'm writing a proxy object that adapts an interface to wrap the return type (the instance of the return type) as a Future. I think this is not possible, but I want to make sure before I try another approach.
Let's say I start with this interface:
Code:
public interface IGatewayInterface {
public MyObject1 call1();
public MyObject2 call2();
}
And a proxy factory that contains a concrete implementation instance of IGatewayInterface:
Code:
public class GatewayServiceProxy implements InvocationHandler {
private GatewayServiceImpl gatewayImpl;
public GatewayServiceProxy(IGatewayInterface gatewayImpl) {
this.gatewayImpl = gatewayImpl;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(gatewayImpl, args);
}
}
The method that is given to invoke is either call1() or call2(). What my proxy would like to do is wrap the result of the method invocation in a Future object. If I knew at compile-time the exact type I could write something like:
Code:
MyObject1 myObject1 = method.invoke(gatewayImpl, args);
retur new FutureImpl<MyObject1>(myObject1);
I've looked at several posts concerning generics, and I've even seen the question asked similar to what I'm asking here, but I still don't see the answer. Can I create an instance of Future where the generic type that must be supplied to it during construction is not known at compile-time?
Re: Creating generic wrapper of return type
Quote:
Originally Posted by
jackett_dad
Can I create an instance of Future where the generic type that must be supplied to it during construction is not known at compile-time?
Generics are compile-time constructs, and are not used during run time. So, a) you cannot, and b) it would be of no sense even if you were able to do so.
However, if you know exact method at the place of calling GatewayServiceProxy.invoke(), you just can make 2 methods invoke1 and invoke2 with exact return types instead of single invoke() (and the parameter method is no longer needed).
Or, if you know the required return type as a generic variable, you can make a parameterized wrapper around the java.reflect Method:
Code:
class MyMethod<R> {
Method m;
public R invoke(IGatewayInterface obj, Object... args) throws Exception {
return (R) m.invoke(obj, args);
}
}
class GatewayServiceProxy implements InvocationHandler {
private IGatewayInterface gatewayImpl;
public GatewayServiceProxy(GatewayServiceImpl gatewayImpl) {
this.gatewayImpl = gatewayImpl;
}
public <R> FutureImpl<R> invoke(MyMethod<R> method, Object[] args) throws Throwable {
R myObject = method.invoke(gatewayImpl, args);
return new FutureImpl<R>(myObject);
}
}
class Caller<R> {
GatewayServiceProxy proxy;
MyMethod<R> method;
public void run() {
FutureImpl<R> = proxy.invoke(method, args);
}
}
This is not very elegant, however, as classes MyMethod and IGatewayInterface play similar roles. Probably a design is possible where that roles are orthogonal, and reflection is not used.