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 failing CtExecutableReference#getActualMethod #1277

Merged
merged 1 commit into from
May 4, 2017
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 @@ -19,9 +19,11 @@
import spoon.SpoonException;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtFormalTypeDeclarer;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.reference.CtActualTypeContainer;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtIntersectionTypeReference;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
Expand Down Expand Up @@ -173,15 +175,28 @@ public CtTypeParameter getDeclaration() {
return null;
}

CtElement e = this;
CtElement parent = getParent();
if (parent instanceof CtExecutableReference) {
CtElement parent2 = parent.getParent();
if (parent2 instanceof CtMethod) {
e = parent2;
} else {
e = ((CtExecutableReference<?>) parent).getDeclaringType().getTypeDeclaration();
}
} else {
e = e.getParent(CtFormalTypeDeclarer.class);
}

// case #1: we're a type of a method parameter, a local variable, ...
// the strategy is to look in the parents
// collecting all formal type declarers of the hierarchy
CtElement e = this;
while ((e = e.getParent(CtFormalTypeDeclarer.class)) != null) {
while (e != null) {
CtTypeParameter result = findTypeParamDeclaration((CtFormalTypeDeclarer) e, this.getSimpleName());
if (result != null) {
return result;
}
e = e.getParent(CtFormalTypeDeclarer.class);
}
return null;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/spoon/test/methodreference/MethodReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,33 @@
import spoon.reflect.code.CtExecutableReferenceExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.visitor.filter.NameFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.methodreference.testclasses.Cloud;
import spoon.test.methodreference.testclasses.Foo;
import spoon.testing.utils.ModelUtils;

import java.io.File;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.function.Supplier;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static spoon.testing.utils.ModelUtils.canBeBuilt;

public class MethodReferenceTest {
Expand Down Expand Up @@ -197,6 +203,28 @@ public void testNoClasspathSuperExecutable() {
assertNotNull(toStringMethod.getAnnotation(overrideRef));
assertNotNull(toStringMethod.getReference().getOverridingExecutable());
}

@Test
public void testGetGenericMethodFromReferene() throws Exception {
CtType<?> classCloud = ModelUtils.buildClass(Cloud.class);
CtMethod<?> ctMethod = classCloud.getMethodsByName("method").get(0);
CtExecutableReference<?> execRef = ctMethod.getReference();
Method method = execRef.getActualMethod();
assertNotNull(method);
assertEquals("method", method.getName());

CtClass<?> classSun = classCloud.getFactory().Class().get("spoon.test.methodreference.testclasses.Sun");
// CtExecutableReference<?> execRef2 = classSun.filterChildren(new TypeFilter<>(CtExecutableReference.class)).select(new NameFilter<>("method")).first();
CtExecutableReference<?> execRef2 = classSun.filterChildren(new TypeFilter<>(CtInvocation.class))
.select(((CtInvocation i)->i.getExecutable().getSimpleName().equals("method")))
.map((CtInvocation i)->i.getExecutable())
.first();
assertNotNull(execRef2);
Method method2 = execRef2.getActualMethod();
assertNotNull(method2);
assertEquals("method", method2.getName());
}


private void assertTypedBy(Class<?> expected, CtTypeReference<?> type) {
assertEquals("Method reference must be typed.", expected, type.getActualClass());
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/methodreference/testclasses/Cloud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package spoon.test.methodreference.testclasses;

public class Cloud<T> {

void method(T param) {}
}

class Sun {
void foo() {
Cloud<String> cc = new Cloud<>();
cc.method("x");
}
}