Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Generic methods using isoverride method will throw an exception #5226

Closed
honghao12 opened this issue May 23, 2023 · 1 comment · Fixed by #5228
Closed

[Bug]: Generic methods using isoverride method will throw an exception #5226

honghao12 opened this issue May 23, 2023 · 1 comment · Fixed by #5228
Labels

Comments

@honghao12
Copy link

honghao12 commented May 23, 2023

Describe the bug

Generic methods that use the isOverriding method throw exceptions

spoon.SpoonException: Cannot resolve type erasure of the type parameter reference, which is not able to found it's declaration.
	at spoon.support.reflect.reference.CtTypeParameterReferenceImpl.getTypeErasure(CtTypeParameterReferenceImpl.java:176)
	at spoon.support.adaption.TypeAdaptor.isOverriding(TypeAdaptor.java:418)
	at spoon.support.reflect.declaration.CtMethodImpl.isOverriding(CtMethodImpl.java:191)
	at com.huawei.tenet.console.SpoonCommandTest.test(SpoonCommandTest.java:75)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Source code you are trying to analyze/transform

p12.test1

package p12;

public class test1 {
    public static class Enclosing<T> {
        public class Enclosed<S> {
            public class ReallyDeepNow<R> {
                void someMethod(S s, T t, R r) {
                }
            }
        }
    }
}

p12.test2

package p12;

public class test2 {
    public static class ExtendsEnclosing extends test1.Enclosing<String> {
        public class ExtendsEnclosed extends Enclosed<Integer> {
            public class ExtendsReallyDeepNow extends ReallyDeepNow<Long> {
                @Override
                void someMethod(Integer s, String t, Long r) {
                    throw new UnsupportedOperationException();
                }
            }
        }
    }
}

Source code for your Spoon processing

Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(11);
launcher.addInputResource("D:\\IdeaDirectory\\javademo4\\test1\\src\\main\\java\\p12");
CtModel model = launcher.buildModel();
List<CtMethod> elements = model.getElements(new TypeFilter<>(CtMethod.class));
CtMethod test2Method = elements.stream()
.filter(ctMethod -> ctMethod.getPath().toString()
.equals(          "#subPackage[name=p12]#containedType[name=test2]#nestedType[name=ExtendsEnclosing]#nestedType[name=ExtendsEnclosed]#nestedType[name=ExtendsReallyDeepNow]#method[signature=someMethod(java.lang.Integer,java.lang.String,java.lang.Long)]")).collect(Collectors.toList()).get(0);
CtMethod test1Method = elements.stream().filter(ctMethod -> ctMethod.getPath().toString().equals(            "#subPackage[name=p12]#containedType[name=test1]#nestedType[name=Enclosing]#nestedType[name=Enclosed]#nestedType[name=ReallyDeepNow]#method[signature=someMethod(java.lang.Object,java.lang.Object,java.lang.Object)]")).collect(Collectors.toList()).get(0);

if(test2Method.isOverriding(test1Method)){// throw SpoonException
   System.out.println("is overriding");
}

Actual output

spoon.SpoonException: Cannot resolve type erasure of the type parameter reference, which is not able to found it's declaration.

Expected output

is overriding

Spoon Version

10.3.0

JVM Version

11

What operating system are you using?

window

@honghao12 honghao12 added the bug label May 23, 2023
@honghao12 honghao12 changed the title [Bug]: Generic methods that use the isOverriding method throw exceptions [Bug]: Generic methods using isoverride method will throw an exception May 23, 2023
@I-Al-Istannen
Copy link
Collaborator

Thanks, this is an oversight with generics inherited from enclosing classes. I've opened a PR with a fix, but I will need to polish it a bit, hopefully today.

If you want you could try out the PR and see if it fixes your original issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants