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

review: fix: set correct executable reference type for signature polymorphic methods #4915

Merged
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 @@ -453,6 +453,12 @@ <T> CtExecutableReference<T> getExecutableReference(MethodBinding exec, int sour

// original() method returns a result not null when the current method is generic.
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));
}
final List<CtTypeReference<?>> parameters = new ArrayList<>(exec.original().parameters.length);
for (TypeBinding b : exec.original().parameters) {
parameters.add(getTypeReference(b, true));
Expand Down
21 changes: 19 additions & 2 deletions src/test/java/spoon/test/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/
package spoon.test.method;

import org.hamcrest.CoreMatchers;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.factory.Factory;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.method.testclasses.Hierarchy;
import spoon.test.method.testclasses.Tacos;
import spoon.reflect.reference.CtTypeReference;
Expand All @@ -34,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 All @@ -43,6 +43,7 @@
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static spoon.testing.utils.ModelUtils.buildClass;
Expand Down Expand Up @@ -245,4 +246,20 @@ void test_getTopDefinitions_findsTopOnly(Factory factory) {
assertThat(m0.getTopDefinitions().size(), equalTo(0));
assertThat(m1.getTopDefinitions().size(), equalTo(0));
}

@ModelTest("src/test/resources/signature-polymorphic-methods/SignaturePolymorphicMethods.java")
void testSignaturePolymorphicMethodInvocations(Factory factory) {
// contract: calls to signature-polymorphic methods should be equal to their declaration signature
CtType<?> type = factory.Type().get("SignaturePolymorphicMethods");
Set<CtMethod<?>> methods = type.getMethods();
assertThat(methods.size(), equalTo(4));
for (CtMethod<?> method : methods) {
// MethodHandle#invoke and MethodHandle#invokeExact have the declaration signature (Object[])Object
CtInvocation<?> invocation = method.getBody().getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertThat(invocation.getType(), equalTo(factory.Type().objectType()));
List<CtTypeReference<?>> parameters = invocation.getExecutable().getParameters();
assertThat(parameters.size(), equalTo(1));
assertThat(parameters.get(0), equalTo(factory.Type().createArrayReference(factory.Type().objectType())));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class SignaturePolymorphicMethods {
private static final MethodHandle STRING_IDENTITY = MethodHandles.identity(String.class);
private static final MethodHandle HELLO_WORLD = MethodHandles.constant(String.class, "Hello World");
private static final MethodHandle NOP = MethodHandles.empty(MethodType.methodType(void.class, CharSequence.class, int.class));
public String a() throws Throwable {
return (String) STRING_IDENTITY.invokeExact("Hello World");
}
public String b() throws Throwable {
return (String) HELLO_WORLD.invokeExact();
}
public Object c() throws Throwable {
return HELLO_WORLD.invoke();
}
public void d() throws Throwable {
NOP.invokeExact((CharSequence) "Hello World", 123);
}
}