gpt4 book ai didi

java - 使用反射将子类类型传递给 Java 泛型方法不会抛出异常

转载 作者:行者123 更新时间:2023-11-30 06:52:13 26 4
gpt4 key购买 nike

所以我在 Spring 中遇到了一个奇怪的错误,其中调用了一个抽象 Controller 方法并传递了一个类型系统不应该允许的对象。

这是一个演示错误的简单示例。

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);

}
}

此测试给出以下异常:

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

基本上,我的问题是,为什么调用 add 方法不会导致 ClassCastException。为什么调用 callAdd 会导致异常?我知道在运行时不会保留通用信息,但是我假设扩展类会导致保留通用类的类型参数。这是 Guava TypeToken 背后的机制.或者,至少我是这么想的。

最佳答案

你是对的,通用父类(super class)型被保留了,但你高估了它的含义。它只是暗示像 Class.getGenericSuperclass() 这样的反射方法将为您提供信息,这确实是 Guava TypeToken 背后的机制。

继承的方法仍然需要进行类型删除。顺便说一下,如果您将错误类型的对象传递给 Method.invoke,您不会得到 ClassCastException,而是 IllegalArgumentException。但是,当然,如果您对方法 AnimalManager.class.getDeclaredMethod("add", Animal.class) 执行反射查询并最终得到一个签名不是添加(动物)

这甚至适用于您覆盖方法的场景,例如当您使用 AnimalManager.class.getDeclaredMethod("callAdd", Animal.class).invoke(manager, animal); 时,您仍然没有得到 IllegalArgumentException,而是,当尝试委托(delegate)给明确定义的方法 addAll(Cat) 时,调用方法 addAll(Animal) 会产生 ClassCastException。这个过程中涉及的addAll(Animal)方法称为bridge方法

关于java - 使用反射将子类类型传递给 Java 泛型方法不会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com