2012-04-29 162 views
0

创建TypeLiteral使用:吉斯3从泛型方法参数类型

我想知道如何创建一个通用的方法参数类型一TypeLiteral用注射器注入它)。

下面是一个例子:

语境:

bind(new TypeLiteral<Set<String>>(){}).toInstance(x); 
bind(new TypeLiteral<Set<Integer>>(){}).annotatedWith(INJECTOR.class).toInstance(y); 

public class Foo 
{ 

public void bar(Injector injector, Set<String> param, @INJECTOR Set<Integer> value) 
{ 
    //... 
} 

} 

public class Example 
{ 
@Inject 
Injector injector; 

public void methodInjector() 
{ 
    Method[] fooMethods = Foo.class.getMethods(); 
    for(Method m : fooMethods) 
    { 
     for(Class<?> c : m.getParameter.getParameterTypes()) 
     { 
      Object o = injector.getInstance(c); 

      /* 
       this work for simple Type like "Injector" but 
       don't work for generic type (Set<String>, List<Set<String>>, ...). 
      */ 
     } 
    } 

} 

} 

显然注入java.util.Set不要在我的情况下工作...也许有与TypeLiteral做一个窍门...

我希望我的解释清楚, 谢谢。

回答

0

正如我理解你正确here你已经解释了如何在运行时访问泛型类型。如果你想获得所有类的参数类型,它是impossible

+0

它可以被可能的原因可能吉斯时使用它的方法与@Inject注解,没有解决的办法? – pass1 2012-04-29 11:03:15