Skip to content

Commit

Permalink
use original return type for method calls of signature polymorphic me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
SirYwell committed Jan 3, 2023
1 parent 490b9dd commit 40a850b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
10 changes: 7 additions & 3 deletions src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,13 @@ <T> CtExecutableReference<T> getExecutableReference(MethodBinding exec) {
}

// original() method returns a result not null when the current method is generic.
// if polymorphic, the original parameter list differs from the actual parameter list
// see https://github.com/INRIA/spoon/issues/4863
if (exec.original() != null && !exec.isPolymorphic()) {
if (exec.original() != null) {
// if polymorphic, the original return type differs from the actual return type
// therefore we use the original one here
// see https://github.com/INRIA/spoon/issues/4863
if (exec.isPolymorphic()) {
ref.setType(getTypeReference(exec.original().returnType, true));
}
final List<CtTypeReference<?>> parameters = new ArrayList<>(exec.original().parameters.length);
for (TypeBinding b : exec.original().parameters) {
parameters.add(getTypeReference(b, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ private CtExecutable<T> getCtExecutable(CtType<?> typeDecl) {
return null;
}
return elements.get(0);
} else if (method == null) {
// deal with signature polymorphic methods here
String qualifiedName = typeDecl.getQualifiedName();
if (qualifiedName.equals("java.lang.invoke.MethodHandle") || qualifiedName.equals("java.lang.invoke.VarHandle")) {
return typeDecl.getMethod(getSimpleName(), getFactory().createArrayReference(getFactory().Type().objectType()));
}
}
return method;
}
Expand Down
23 changes: 3 additions & 20 deletions src/test/java/spoon/test/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package spoon.test.method;

import org.hamcrest.CoreMatchers;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.factory.Factory;
import spoon.reflect.declaration.CtParameter;
Expand All @@ -37,7 +35,6 @@
import org.junit.jupiter.api.Test;
import spoon.testing.utils.ModelTest;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.ConcurrentModificationException;
Expand Down Expand Up @@ -254,26 +251,12 @@ void test_getTopDefinitions_findsTopOnly(Factory factory) {
void testSignaturePolymorphicMethodInvocations(Factory factory) {
CtType<?> type = factory.Type().get("spoon.test.method.testclasses.SignaturePolymorphicMethods");
for (CtMethod<?> method : type.getMethods()) {
CtTypeReference<?> returnType = method.getType();
CtInvocation<?> invocation = method.getBody().getElements(new TypeFilter<>(CtInvocation.class)).get(0);
System.out.println(invocation.getExecutable().getExecutableDeclaration());
assertThat(invocation, instanceOf(CtInvocation.class));
assertThat(invocation.getType(), equalTo(returnType));
assertThat(invocation.getType(), equalTo(factory.Type().objectType()));
List<CtTypeReference<?>> parameters = invocation.getExecutable().getParameters();
List<CtExpression<?>> arguments = invocation.getArguments();
assertThat(parameters.size(), equalTo(arguments.size()));
for (int i = 0; i < arguments.size(); i++) {
CtTypeReference<?> parameter = parameters.get(i);
CtExpression<?> argument = arguments.get(i);
assertThat(parameter, equalTo(typeAfterCasting(argument)));
}
}
}

private CtTypeReference<?> typeAfterCasting(CtExpression<?> expression) {
if (expression.getTypeCasts().isEmpty()) {
return expression.getType();
assertThat(parameters.size(), equalTo(1));
assertThat(parameters.get(0), equalTo(factory.Type().createArrayReference(factory.Type().objectType())));
}
return expression.getTypeCasts().get(0);
}
}

0 comments on commit 40a850b

Please sign in to comment.