简体   繁体   English

Java 8:使用反射使用泛型创建类的新实例

[英]Java 8: create new instance of class with generics using reflection

I have the following method: 我有以下方法:

public Comparator<T> getComparator()  throws ReflectiveOperationException {
    String className = "some.ClassName";
    Class<Comparator<T>> c = Class.forName(className); // (1)
    return (Comparator<T>) c. newInstance();
}

In the line (1) I get this error: 在第(1)行中,出现此错误:

Type mismatch: cannot convert from Class <capture#1-of ?> to Class<Comparator<T>> 类型不匹配:无法从类别<capture#1-of?>转换为Class <Comparator <T >>

What's wrong in this code and how should I make an instance of Comparator<T> ? 这段代码有什么问题,我应该如何创建Comparator<T>的实例?

The best you can get so far is 到目前为止你能得到的最好的是

public <T> Comparator<T> getComparator()  throws ReflectiveOperationException {
    Class<? extends Comparator> implementation
        = Class.forName("some.ClassName").asSubclass(Comparator.class);
    @SuppressWarnings("unchecked")
    final Comparator<T> c = implementation.newInstance();
    return c;
}

Note that there is still an unchecked operation which is unavoidable. 请注意,仍然存在无法避免的未经检查的操作。 The runtime type token Comparator.class produces a Class<Comparator> rather than Class<Comparator<T>> which reflects the type erasure and implies that you can use it via asSubclass to ensure that a Class indeed implements Comparator , but you can't ensure that it implements Comparator<T> regarding any <T> . 运行时类型令牌Comparator.class生成Class<Comparator>而不是Class<Comparator<T>> ,这反映了类型擦除,并暗示您可以通过asSubclass使用它来确保Class确实实现Comparator ,但是您不能确保对任何<T>实现Comparator<T> <T> (Note that this method doesn't even know what T is). (请注意,此方法甚至都不知道T是什么)。 Therefore there is still an unchecked conversion of Comparator to Comparator<T> . 因此,仍然存在未经检查的ComparatorComparator<T>转换。

Simply move the cast: 只需移动演员:

Class<Comparator<T>> c = (Class<Comparator<T>>) Class.forName(className); // (1)
return c.newInstance();

Try this solution 试试这个解决方案

@SuppressWarnings("unchecked")
public Comparator<T> getComparator() throws Exception {
    String className = "some.ClassName";
    Class<?> c = Class.forName(className); // (1)
    return (Comparator<T>) c. newInstance();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java使用泛型或类类型创建对象的新实例 - Java Create new Instance of object using Generics OR Class type 使用Java中的反射创建一个新实例,并将引用变量类型设置为新的实例类名称? - Using reflection in Java to create a new instance with the reference variable type set to the new instance class name? 类和新实例的Java泛型 - Java generics for class and new instance 如何使用零参数构造函数创建具有通过Java反射绑定的上下文的Scala类的新实例? - How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor? Java - 使用Reflection创建新实例,而不需要知道构造函数参数 - Java - Create new Instance using Reflection without knowing constructor params 如何使用具有 generics 参数和返回类型方法的 java 中的反射来获取 Class 的实例? - How to get Instance of a Class using reflection in java having generics parameter and return type method? 是否可以使用Java Reflection创建嵌套类的实例? - Is it possible to create an instance of nested class using Java Reflection? Java泛型,创建一个类的实例<T> - Java Generics, Create an instance of Class<T> Java反射创建未知类和构造函数的实例 - Java reflection create instance of unknown class and constructor Java泛型和反射:类加载 - Java generics and reflection: class loading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM