- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定一个注入(inject)器,我想知道如何检索某个参数化类型的特定实例(但我没有 Type
本身)。让我解释一下:
假设您进行了以下绑定(bind):
List<Apple>
绑定(bind)到 ArrayList<Apple>
Set<Pears>
绑定(bind)到 HashSet<Pear>
Collection
的 Fruit
.现在我有一个 Fruit fruit
实例,我想检索适当的 Collection
实例。我怎样才能做到这一点?
这是一个小的工作片段,用代码说明了我所有的解释:
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
public class TestGuiceDynamicType {
public static interface Fruit {
}
public static class Apple implements Fruit {
}
public static class Pear implements Fruit {
}
public static class FruitModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<Collection<Apple>>() {
}).to(new TypeLiteral<ArrayList<Apple>>() {
});
bind(new TypeLiteral<Collection<Pear>>() {
}).to(new TypeLiteral<HashSet<Pear>>() {
});
}
}
private <T extends Fruit> static void addFruit(Injector injector, T fruit) {
Collection<T> collection = ????? // What to do here to get the appropriate collection
collection.add(fruit);
}
public static void main(String[] args) {
Injector injector = Guice.createInjector(new FruitModule());
Collection<Apple> appleCollection = injector.getInstance(Key.get(new TypeLiteral<Collection<Apple>>() {
}));
appleCollection.add(new Apple());
addFruit(injector, new Pear())
}
}
最佳答案
好的,我最终找到了解决方案:
private static <T extends Fruit> void addFruit(Injector injector, T fruit) {
Collection<T> collection = (Collection<T>) injector.getInstance(Key.get(Types.newParameterizedType(Collection.class,
fruit.getClass())));
collection.add(fruit);
}
关键是使用 com.google.inject.util.Types
类的 Types.newParameterizedType()
。
关于java - 如何在 Guice 中获取动态类型/动态构建的 ParameterizedType 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16902707/
给出下面的代码片段,对于 POJO 类的每个字段,有没有办法检查类型是否是整数列表?这里真正的问题是类型参数,因为通过 instanceof 或 isAssignableFrom 很容易检查它是否是一
public class FrontUtil { public List> tableGen(List> theads, List tbodys){ List> frontLi
我无法完全理解反射的工作原理,因此在尝试为我的 GsonBuilder 注册自定义类型适配器时遇到了一些麻烦。 . 我需要为 List 注册类型适配器类型。 这是一个工作代码: gsonBui
考虑这个类结构。 class Class1 { Class2 field1; } 如果我有一个 ParameterizedType代表 Class1 的实例通过反射,我如何获取/创建 Para
我正在从 Field.getGenericType() 中检索一个 ParameterizedType 对象,我想创建该类型的一个实例。例如,此类型可能代表一个 HashMap。 我想也许我可以将它转
阅读 ParameterizedType 的文档后interface 我认为 ParameterizedType 实例的示例可以是任何参数化类型,例如我代码中的 col: public class
我尝试这个通用代码是因为我不想为数据库中的每个实体都创建一个 Dao 类,因为我有 80 个专门用于那些我将只执行 CRUD 查询的人。因为在大多数情况下,我只需要坚持或通过 id 进行查找。 pub
有用的是,scala 的 Universe.typeOf 保留了类的类型参数。 import scala.reflect.runtime.universe._ case class X[T:TypeT
由于错误,给定的断言将失败 Failed to find the generated JsonAdapter constructor for class GenericType 如何从具体化的类型中获
我有以下代码。在运行时 typeClassz 是 java.util.Set。但是 typeClassz instanceof ParameterizedType 的计算结果为 false。对于 ja
我正在尝试实现这样的模型结构: Generic (abstract) TplInsurance (extends Generic) TplInsuranceDoctor (nested class i
假设我有一个Type a这是 java.util.TreeSet ,已通过泛型参数化,由于类型删除,我们看不到。我们还有一个Type b ,这是一个“java.lang.Integer”。如何构建 P
如果我有一个无法更改的接口(interface)和类,看起来像这样: public interface ISport { String getName(); List ge
我想使用新的 JPA 2.1 功能来创建自定义通用枚举转换器。但在部署时我收到此错误:Caused by: org.hibernate.AssertionFailure: Could not extr
目前 VariableService 在我的 Controller 中是 @Autowired。 我知道我可以实现类 ParameterizedType 来消除这个错误,但我担心我可能会走错方向。有没
我有一个简单的静态方法: public static U findStuff(String id) { // how can i get the class of type U here ?
我已经在 Stack 上寻找了一段时间的答案。所有的答案看起来都好像他们说我已经有了正确的答案,但我仍然在下面的构造函数的第一行收到类强制转换异常。 SEVERE: Exception while l
给定一个注入(inject)器,我想知道如何检索某个参数化类型的特定实例(但我没有 Type 本身)。让我解释一下: 假设您进行了以下绑定(bind): List绑定(bind)到 ArrayList
调用 java.lang.reflect.Type.toString() 提供了非常好的通用类型表示: @Test public void typeToStringWithTypeToken() {
我被这个问题困扰了很长时间。我搜索了这个问题一段时间,但没有一个解决方案有效。 结构: public interface GenericDAO @Repository public class Abs
我是一名优秀的程序员,十分优秀!