2012-04-29 156 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