简体   繁体   English

如何使用通用接口和 Class.forName 避免类型安全警告

[英]How to avoid type safety warning with generic interfaces and Class.forName

I have the following code which is forking fine:我有以下代码很好:

private <X extends SomeInterface<MyImplementation>> void test(String fqcn)
{
    Class<X> c = (Class<X>)Class.forName(fqcn).asSubclass(SomeInterface.class);
    ...
}

However, I get a type safety warning但是,我收到类型安全警告

Type safety: Unchecked cast from Class<capture#2-of ? extends SomeInterface> to Class<X>

and wondering how to fix it?并想知道如何解决它?

There is no way to prove that this casting is safe and would succeed.没有办法证明这种铸造是安全的并且会成功。 You're performing casting of Class<?> (of unknown type ) returned by Class.forName() into the Class<X> (of something), the compiler can't verify if what you're doing is correct.您正在将Class.forName() ) 返回的Class<?>未知类型)转换为Class<X> (某物),编译器无法验证您所做的是否正确。

The only thing you can do is to suppress the warning by placing annotation @SuppressWarnings("unchecked") on the method on the problematic statement:您唯一能做的就是通过在有问题的语句的方法上放置注释@SuppressWarnings("unchecked")来抑制警告:

@SuppressWarnings("unchecked")
private <X extends SomeInterface<MyImplementation>> void test(String fqcn) {
    Class<X> c = (Class<X>)Class.forName(fqcn).asSubclass(SomeInterface.class);
    ...
}

Or或者

private <X extends SomeInterface<MyImplementation>> void test(String fqcn) {
    @SuppressWarnings("unchecked")
    Class<X> c = (Class<X>)Class.forName(fqcn).asSubclass(SomeInterface.class);
    ...
}

Read the reflection java.lang.Class "api docs" for forName method, it should be wrapped in java.lang.ClassNotFoundException and java.lang.ClassCastException. Read the reflection java.lang.Class "api docs" for forName method, it should be wrapped in java.lang.ClassNotFoundException and java.lang.ClassCastException. The correct Exception(s) should remove that warning.正确的异常应该删除该警告。

use isInstance(theCreatedClassReference) from java.lang.Class使用 java.lang.Class 中的 isInstance(theCreatedClassReference)

don't want to try without code and compiler, take too long for me.不想在没有代码和编译器的情况下尝试,对我来说需要太长时间。

isInstance() returns a boolean and is the correct to use for Class.forName(stringnametoload) check alike the instanceof operator when creating a Class check its constructed reference in an if test the multi argument Class.forName allows class loading usage designation of whether it is "initialized". isInstance() returns a boolean and is the correct to use for Class.forName(stringnametoload) check alike the instanceof operator when creating a Class check its constructed reference in an if test the multi argument Class.forName allows class loading usage designation of whether it已“初始化”。

sorry about the botched code attempt.对拙劣的代码尝试感到抱歉。

If you are actually making a usable reference then maybe check with instanceof but the actions DO require the exception wrappers (not a bad idea when they can be interfaces and other types in one).如果您实际上正在制作一个可用的引用,那么可以使用 instanceof 进行检查,但这些操作确实需要异常包装器(当它们可以是接口和其他类型时,这不是一个坏主意)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM