通过反射将子类类型传递给Java泛型方法不会引发异常
因此,我在Spring中遇到了一个奇怪的bug,其中调用了一个抽象控制器方法,并传递了一个类型系统不允许的对象 这是一个简单的例子,演示了这个bug通过反射将子类类型传递给Java泛型方法不会引发异常,java,generics,reflection,java-8,Java,Generics,Reflection,Java 8,因此,我在Spring中遇到了一个奇怪的bug,其中调用了一个抽象控制器方法,并传递了一个类型系统不允许的对象 这是一个简单的例子,演示了这个bug import org.junit.Test; import java.lang.reflect.InvocationTargetException; public class RandomJavaTesting { public static class Animal { } public static class C
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
public class RandomJavaTesting
{
public static class Animal {
}
public static class Cat extends Animal {
}
public static class AnimalManager<T extends Animal> {
// This gets called and doesn't blow up
public void add(T animal){
// This will throw a class cast exception
// Why does this blow up here for CatManager?
this.callAdd(animal);
}
public void callAdd(T animal){
}
}
public static class CatManager extends AnimalManager<Cat> {
@Override
public void callAdd(Cat animal)
{
// This will never run
}
}
@Test
public void callingGenericMethodWithSubClassType() throws InvocationTargetException, IllegalAccessException
{
CatManager manager = new CatManager();
Animal animal = new Animal();
AnimalManager.class.getDeclaredMethod("add", Animal.class).invoke(manager, animal);
}
}
基本上,我的问题是,为什么调用add
方法不会导致ClassCastException。
为什么调用callAdd
会导致异常?
我知道泛型信息不会在运行时保留,但是我假设扩展类会导致保留泛型类的类型参数。
这就是这场战争背后的机制。或者,至少我是这么想的。基本上它打破了利斯科夫的替代原则
对于Add方法,可以将T类型视为动物,但对于AdDALL方法,该类型被指定为CAT,因此不能用类型动物替换CAT类型。
您是正确的,因为泛型超类型被保留,但是您过高估计了它的含义。它只是暗示像这样的反射方法将为您提供信息,这确实是Guava TypeToken背后的机制
继承的方法仍要进行类型擦除。顺便说一下,如果将错误类型的对象传递给方法.invoke
,则不会得到ClassCastException
,而是IllegalArgumentException
。但是,当然,如果您对方法AnimalManager.class.getDeclaredMethod(“add”,Animal.class)
执行一个反射查询,并最终得到一个签名不是add(Animal)
的方法,这会让人感到困惑
这甚至适用于重写方法的场景,例如,当您使用
AnimalManager.class.getDeclaredMethod(“callAdd”,Animal.class).invoke(manager,Animal)时代码>,您仍然不会得到IllegalArgumentException
,而是调用了一个方法addAll(Animal)
,当尝试委托给显式定义的方法addAll(Cat)
时,该方法会生成一个ClassCastException
。此过程中涉及的addAll(Animal)
方法称为bridge方法。类型擦除后,add
方法的签名为public void add(Animal)
。CatManager
继承了该方法,而不是公共无效添加(猫-动物)
。因此,<>代码> AddLoad < /C>是唯一一个要求<代码> CAT/COD>的方法,因为它被这个签名所覆盖。不,你不能“考虑T类型为动物”。对于泛型类型系统,类CatManager
是AnimalManager
的子类型,AnimalManager
没有方法add(Animal)
。它只有一个方法add(Cat)
,由CatManager
继承。问题在于,对于查询其add
方法的AnimalManager
,没有反射操作。相反,AnimalManager.class
反映了原始类型AnimalManager
,它颠覆了泛型类型系统。
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at RandomJavaTesting.callingGenericMethodWithSubClassType(RandomJavaTesting.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassCastException: RandomJavaTesting$Animal cannot be cast to RandomJavaTesting$Cat
at RandomJavaTesting$CatManager.callAdd(RandomJavaTesting.java:28)
at RandomJavaTesting$AnimalManager.add(RandomJavaTesting.java:20)
... 32 more