泛型和Class.forName [英] Generics and Class.forName

查看:441
本文介绍了泛型和Class.forName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用它的名字创建一个指定类的实例。我的代码如下所示。



我得到一个编译器警告。我是否以正确的方式做这件事?甚至可以使用类的名称并获取该类型的实例,因为我认为编译器不知道类型应该是什么?

  public static< T> T create(final String className){
try {
final Class<?> clazz = Class.forName(className);

//警告:类型安全:从捕获#2中未选中投射? to T
return(T)create(clazz);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}

public static< T> T create(final Class< T> classToCreate){
final构造函数< T>构造函数;
尝试{
constructor = classToCreate.getDeclaredConstructor();
final T result = constructor.newInstance();
返回结果;
}
catch(Exception e){
e.printStackTrace();


谢谢

解决方案

我认为第一种方法应该是这样的:

  public static< T> T create(final String className,Class< T> ifaceClass)
throws ClassNotFoundException {
final Class< T> clazz = Class.forName(className).asSubclass(ifaceClass);
return create(clazz);

$ / code>

不能使用类型参数执行上传的类型转换... without那些讨厌的类型安全警告。

顺便说一下,如果你忽略这些警告,create方法可能会创建一些与实际不兼容的类的实例类型由调用者使用。这很可能会导致以后发生意外的ClassCastException;例如当分配实例时。




编辑:@Pascal指出我们需要添加一个类型转换来进行编译;即

  Class< T> clazz =(Class< T>)Class.forName(className).asSubclass(ifaceClass); 

不幸的是,我们还需要添加一个@SuppressWarnings注释。 p>

I would like to create an instance of a specified class using its name. My code is shown below.

I get a compiler warning. Am I doing this the right way? Is it even possible to use the name of a class and get an instance of that type back, as I don't think there is any way of the compiler knowing what the type should be?

public static <T> T create(final String className) {
    try {
        final Class<?> clazz = Class.forName(className);

        //WARNING: Type safety: Unchecked cast from capture#2-of ? to T
        return (T) create(clazz); 
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static <T> T create(final Class<T> classToCreate) {
    final Constructor<T> constructor;
    try {
        constructor = classToCreate.getDeclaredConstructor();
        final T result = constructor.newInstance();
        return result;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Thanks

解决方案

I think that the first method should look something like this:

public static <T> T create(final String className, Class<T> ifaceClass) 
throws ClassNotFoundException {
    final Class<T> clazz = Class.forName(className).asSubclass(ifaceClass);
    return create(clazz); 
}

You cannot do an up-cast typecast using a type parameter ... without those pesky type-safety warnings.

By the way, if you ignore those warnings, the create method may create an instance of some class that isn't compatible with the actual type used by the caller. This is likely to lead to an unexpected ClassCastException later on; e.g. when the instance is assigned.


EDIT: @Pascal points out that we need to add a typecast to make this compile; i.e.

Class<T> clazz = (Class<T>) Class.forName(className).asSubclass(ifaceClass);

Unfortunately, we also need to add a @SuppressWarnings annotation.

这篇关于泛型和Class.forName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆