如何将通配符 (?) 转换为泛型类型 T?

ehb*_*ian 5 java generics casting wildcard

我有一个静态方法,我想用它来加载类并在运行时实例化我的对象,但是当我编译时,我收到了这个警告:

warning: [unchecked] unchecked cast
            T t = (T) ctor.newInstance();
required: T
found:    CAP#1
where T is a type-variable:
    T extends Object declared in method <T>forName(String,Set<String>)
    where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
1 warning
Run Code Online (Sandbox Code Playgroud)

这是代码:

public static <T> Set<T> forName(String modulePath, Set<String> classes) throws InvalidModuleException{
    try {
        ClassLoader cl = new URLClassLoader(new URL[]{new URL(modulePath)});

        Set<T> list = new HashSet<>(classes.size());
        for (String className : classes) {
            Class<?> clazz = (Class<?>) Class.forName(className, true, cl);
            Constructor<?> ctor = clazz.getConstructor();
            T t = (T) ctor.newInstance();
            list.add(t);
        }
        return list;    
    } catch (MalformedURLException | ReflectiveOperationException ex) {
        throw new InvalidModuleException(ex.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释我吗?

[更新] 这是方法调用的示例:

HashSet<String> set = new HashSet<>();
h.add("fully_classfied_classname_readed_from_file"); //Class that extends AbstractApplication
Set<AbstractApplication> abs = Apps.forName("plugins/module.jar", set);
Run Code Online (Sandbox Code Playgroud)

Jat*_*tin 1

有一点缺失。首先,您尝试将每个对象类型转换为T. 如果您T预先知道,我看不出当类对象可以传递一组字符串时您需要由谁传递的原因

假设如果存在子类的可能性,您是否仍然需要:

public static <T> Set<? extends T> forName(String modulePath, Set<String> classes, Class<T> claz) throws InvalidModuleException{
    try {
    ClassLoader cl = new URLClassLoader(new URL[]{new URL(modulePath)});

    Set<T> list = new HashSet<>(classes.size());
    for (String className : classes) {
        Class<?> clazz = Class.forName(className, true, cl);
        Constructor<?> ctor = clazz.getConstructor();
        Object obj = ctor.newInstance();
        list.add(claz.cast(obj));
    }
    return list;
} catch (MalformedURLException | ReflectiveOperationException | ClassCastException ex) {
    throw new InvalidModuleException(ex.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)

  • 或者,您可以事先更改类的类型 `Class&lt;? 扩展 T&gt; clazz = Class.forName(...).asSubclass(claz);` (3认同)