类型带反射的文字注入

发布于 2024-12-09 17:38:18 字数 762 浏览 0 评论 0原文

上下文:java使用guice(最新版本)

大家好,是否可以通过这种方式用Guice注入一些TypeLiteral:

public MyClass<?,?> getMyClass(Injector injector, Class<?> a, Class<?> b)
{
     //how to Inject MyClass with type a & b ?
     //e.g : injector.getInstance(MyClass<"a class","b class">)
}

public interface MyClass<S,T>
{
     public T do(S s);
}

public class ClassOne implements MyClass<String,Integer>
{
     public Integer do(String s)
     {
          //do something
     }
}

Module :
bind.(new TypeLiteral<MyClass<String,Integer>(){}).to(ClassOne.class);
bind.(new TypeLiteral<MyClass<String,Double>(){}).to(ClassTwo.class);
...

处理这个问题的最佳方法是什么(使用吉斯)?

谢谢 !

Context : java using guice (last version)

Hi everybody, is it possible to inject some TypeLiteral with Guice by this way :

public MyClass<?,?> getMyClass(Injector injector, Class<?> a, Class<?> b)
{
     //how to Inject MyClass with type a & b ?
     //e.g : injector.getInstance(MyClass<"a class","b class">)
}

public interface MyClass<S,T>
{
     public T do(S s);
}

public class ClassOne implements MyClass<String,Integer>
{
     public Integer do(String s)
     {
          //do something
     }
}

Module :
bind.(new TypeLiteral<MyClass<String,Integer>(){}).to(ClassOne.class);
bind.(new TypeLiteral<MyClass<String,Double>(){}).to(ClassTwo.class);
...

What is the best way to handle this problem (with Guice)?

Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2024-12-16 17:38:18

为您的类型创建一个 ParameterizeType :

// It's supposed to be internal.
// You could use sun.reflect.generics.reflectiveObjects but it is not portable.
// Or you can implement it yourself (see below)
ParameterizedType type = new com.google.inject.internal.MoreTypes.ParameterizedTypeImpl(null, MyClass.class, a, b);

从中创建一个 TypeLiteral :

TypeLiteral typeLiteral = TypeLiteral.get(type);

现在创建注入实例:

return (MyClass<A,B>) injector.getInstance(Key.get(typeLiteral))

实际上,您想自己实现 ParameteriedType :

 final Type[] types = {a, b};
 ParameterizedType type = ParameterizedType() {
   @Override
   public Type[] getActualTypeArguments() {
     return types;
   }

   @Override
   public Type getOwnerType() {
     return null;
   }

   @Override
   public Type getRawType() {
     return MyClass.class;
   };
}

编辑:事实上,您可以使用:

Types.newParameterizedType(MyClass.class,a,b)

参见 带有类型参数的 Guice 模块

Create a ParameterizeType for your type :

// It's supposed to be internal.
// You could use sun.reflect.generics.reflectiveObjects but it is not portable.
// Or you can implement it yourself (see below)
ParameterizedType type = new com.google.inject.internal.MoreTypes.ParameterizedTypeImpl(null, MyClass.class, a, b);

Create a TypeLiteral from it:

TypeLiteral typeLiteral = TypeLiteral.get(type);

Now create injected instance:

return (MyClass<A,B>) injector.getInstance(Key.get(typeLiteral))

In practice you want to implement the ParameteriedType yourself:

 final Type[] types = {a, b};
 ParameterizedType type = ParameterizedType() {
   @Override
   public Type[] getActualTypeArguments() {
     return types;
   }

   @Override
   public Type getOwnerType() {
     return null;
   }

   @Override
   public Type getRawType() {
     return MyClass.class;
   };
}

EDIT: In fact, you can use:

Types.newParameterizedType(MyClass.class,a,b)

see Guice module with type parameters

倥絔 2024-12-16 17:38:18

事实上,我认为这是不可能的。泛型在运行时并未具体化。这意味着该信息在运行时不存在。所以它并不关心你给它什么泛型类型。

所以,我认为这个方法是没有必要的。简单来说就是这样:

public MyClass<?,?> getMyClass(Injector injector, Class<?> a, Class<?> b)
{
     return new MyClass();
     // Or maybe something like this, if you use custom constructors
     // return injector.getInstance();
}

由于 MyClass 是一个接口,所以你想要实现的东西是完全无用且不可能的。当您只指定参数类型和返回类型时,您希望如何实例化具有特定行为的对象。
我认为你应该尝试寻找一种不同的工作方式。

In fact, I think it isn't possible. Generics are not reified at run-time. This means the information is not present at run-time. So it doesn't care what generic type you give it.

So, I think this method is not needed. Simply something like this:

public MyClass<?,?> getMyClass(Injector injector, Class<?> a, Class<?> b)
{
     return new MyClass();
     // Or maybe something like this, if you use custom constructors
     // return injector.getInstance();
}

Since MyClass is an interface, what you want to achieve is totally useless and impossible. How do you want to instantiate an object with a specific behavior when you only specify the param type and return type.
I think you should try to find a different way of working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文