为什么此转换不会生成编译器警告?
为什么以下 java 代码不会生成编译器警告,提示“从 SuperClass 到 SomeBaseClass 的不安全转换”之类的内容?
public abstract SuperClass
{
static SuperClass create()
{
return new AnotherBaseClass();
}
private static class SomeBaseClass extends SuperClass
{
void print()
{
System.out.println("Hello World");
}
}
private static class AnotherBaseClass extends SuperClass
{
}
public static void main(String[] args)
{
SomeBaseClass actuallyAnotherClass = (SomeBaseClass)SuperClass.create();
actuallyAnotherClass.print();
}
}
我在Windows机器上使用了jdk1.6.0_25/bin/javac。 Eclipse Helios 也没有对此发出警告。
相反,它会导致运行时异常:
线程“main”中的异常 java.lang.ClassCastException:SuperClass$AnotherBaseClass 无法转换为 SuperClass$SomeBaseClass
Why doesn't the following java code generate a compiler warning saying something like "Unsafe cast from SuperClass to SomeBaseClass"?
public abstract SuperClass
{
static SuperClass create()
{
return new AnotherBaseClass();
}
private static class SomeBaseClass extends SuperClass
{
void print()
{
System.out.println("Hello World");
}
}
private static class AnotherBaseClass extends SuperClass
{
}
public static void main(String[] args)
{
SomeBaseClass actuallyAnotherClass = (SomeBaseClass)SuperClass.create();
actuallyAnotherClass.print();
}
}
I used jdk1.6.0_25/bin/javac on a Windows machine. Eclipse Helios doesn't warn about it either.
Instead it results in a runtime exception:
Exception in thread "main" java.lang.ClassCastException: SuperClass$AnotherBaseClass cannot be cast to SuperClass$SomeBaseClass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Javac 仅在使用泛型时警告不安全的强制转换。在这里,编译器相信你知道你在做什么:)
Javac only warns for unsafe casts when using generics. Here the compiler trusts you to know what you're doing :)
实际上,如果根本不可能进行强制转换,编译器会抛出错误,例如,如果
create()
方法返回类型为AnotherBaseClass
而不是SuperClass< /代码>。
由于它返回一个
SuperClass
,编译器不知道实际返回什么——它也可能返回一个SomeBaseClass
。因此,它必须相信您知道如何处理该演员。编辑:
要在转换时收到警告,您可以尝试使用代码分析工具,例如 Checkstyle。
但请注意,这些工具很可能无法或不检查类层次结构,因此可能只能警告一般使用的(非原始)强制转换。因此,如果您使用需要强制转换的库(例如,如果您使用尚不支持泛型的 apache commons 集合),您将收到大量警告。
毕竟,编程仍然是一门艺术,您仍然需要知道自己在做什么。
Actually, the compiler would throw an error if the cast was not possible at all, e.g. if the
create()
methods return type would beAnotherBaseClass
instead ofSuperClass
.Since it returns a
SuperClass
the compiler doesn't know what will actually be returned - it could as well return aSomeBaseClass
. Thus it has to trust that you know what you do with that cast.Edit:
To get a warning when casting you might try and employ a code analysis tool like Checkstyle.
Note, however, that those tools most likely can't or don't check the class hierarchy and thus might only be able to warn about (non-primitive) casts being used in general. Thus if you use a library that requires casts (e.g. if you're using apache commons collections which doesn't support generics yet) you'd get a lot of warnings.
After all, programming is still an art and you still need to know what you're doing.
这不是编译器警告。当尝试将
AnotherBaseClass
对象转换为SomeBaseClass
时,它在运行时失败。That's not a compiler warning. It has failed at runtime while trying to cast
AnotherBaseClass
object toSomeBaseClass
.