Results 1 to 2 of 2
  1. #1
    jackett_dad is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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:
    Java Code:
    public interface IGatewayInterface {
        public MyObject1 call1();
        public MyObject2 call2();
    }
    And a proxy factory that contains a concrete implementation instance of IGatewayInterface:
    Java 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:
    Java 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?

  2. #2
    rfq
    rfq is offline Member
    Join Date
    Mar 2012
    Location
    Novosibirsk, Russia
    Posts
    15
    Rep Power
    0

    Default Re: Creating generic wrapper of return type

    Quote Originally Posted by jackett_dad View Post
    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:
    Java 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.

Similar Threads

  1. Creating an array of generic type
    By colerelm in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 03:58 AM
  2. Type wildcards in Generic
    By gvm in forum New To Java
    Replies: 1
    Last Post: 10-22-2010, 04:39 PM
  3. Generic return type is not inferred in for-each loop
    By ranma173 in forum Advanced Java
    Replies: 5
    Last Post: 10-08-2010, 11:50 AM
  4. Generic collection type
    By dreuzel in forum Advanced Java
    Replies: 7
    Last Post: 12-23-2009, 06:05 PM
  5. Replies: 1
    Last Post: 05-16-2008, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •