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

fix: ClassCastException in getTypeParameterDeclaration #3042

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,8 @@ public CtTypeParameter getTypeParameterDeclaration() {
if (parent instanceof CtExecutableReference) {
CtExecutable<?> exec = ((CtExecutableReference<?>) parent).getExecutableDeclaration();
if (exec instanceof CtMethod || exec instanceof CtConstructor) {
return findTypeParamDeclarationByPosition((CtFormalTypeDeclarer) exec, ((CtTypeReference) parent).getActualTypeArguments().indexOf(this));
int idx = ((CtExecutableReference) parent).getActualTypeArguments().indexOf(this);
return idx >= 0 ? findTypeParamDeclarationByPosition((CtFormalTypeDeclarer) exec, idx) : null;
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/generics/GenericsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1547,4 +1547,17 @@ public void testIsParameterized() {
assertFalse(param2.getType().getActualTypeArguments().get(0).isParameterized());
assertTrue(param3.getType().getActualTypeArguments().get(0).isParameterized());
}

@Test
public void testExecutableTypeParameter() {
// contract: getTypeParameterDeclaration() should not produce class cast exception
// https://github.com/INRIA/spoon/issues/3040
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses6/A.java");
CtModel model = launcher.buildModel();
CtInvocation m1 = model.getElements(new TypeFilter<>(CtInvocation.class)).get(1);
CtTypeParameter formalType = ((CtMethod) m1.getExecutable().getDeclaration()).getFormalCtTypeParameters().get(0);
assertEquals(formalType, ((CtTypeReference) m1.getActualTypeArguments().get(0)).getTypeParameterDeclaration());
assertNull(m1.getType().getTypeParameterDeclaration());
}
}
11 changes: 11 additions & 0 deletions src/test/java/spoon/test/generics/testclasses6/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package spoon.test.generics.testclasses6;

public class A<X> {

<T> void m1(T a) {
}

void f() {
this.<Integer>m1(42);
}
}