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 Interface method handling #2023

Merged
merged 3 commits into from
Jun 5, 2018
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
7 changes: 5 additions & 2 deletions src/main/java/spoon/reflect/factory/MethodFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@
import spoon.template.Substitution;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.ArrayList;
import java.util.Arrays;

/**
* The {@link CtMethod} sub-factory.
*/
public class MethodFactory extends ExecutableFactory {

public final Set<CtMethod<?>> OBJECT_METHODS = Collections.unmodifiableSet(factory.Class().get(Object.class).getMethods());

/**
* Creates a new method sub-factory.
*
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtExecutableReference;
Expand Down Expand Up @@ -124,6 +124,9 @@ public <R> CtMethod<R> getOverriddenMethod() {
lambdaExecutableMethod = lambdaTypeMethods.iterator().next();
} else {
for (CtMethod<?> method : lambdaTypeMethods) {
if (getFactory().Method().OBJECT_METHODS.stream().anyMatch(method::isOverriding)) {
continue;
}
if (method.isDefaultMethod() || method.hasModifier(ModifierKind.PRIVATE) || method.hasModifier(ModifierKind.STATIC)) {
continue;
}
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/spoon/test/lambda/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
Expand Down Expand Up @@ -403,6 +402,19 @@ public void testLambdaFilter() throws Exception {
assertHasStrings(methodNames, "m2", "m4", "m7", "m8");
}

@Test
public void testInterfaceWithObjectMethods() throws Exception {
// contract Lambda expression works on interfaces with methods inherited from java.lang.Object
CtInterface<?> checkPersons = factory.Interface().get(Foo.CheckPersons.class);
List<CtLambda<?>> lambdas = foo.filterChildren(new LambdaFilter(checkPersons)).list();
assertEquals(2, lambdas.size());
CtLambda<?> lambda = lambdas.get(0);
assertEquals(2, lambda.getParameters().size());
CtMethod<?> method = lambda.getOverriddenMethod();
assertTrue(checkPersons.getMethods().contains(method));
assertEquals("test", method.getSimpleName());
}

private void assertHasStrings(List<String> methodNames, String... strs) {
for (String str : strs) {
assertTrue("List should contain "+str+" but it is missing.", methodNames.remove(str));
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/spoon/test/lambda/testclasses/Foo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spoon.test.lambda.testclasses;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
Expand Down Expand Up @@ -110,5 +111,6 @@ public interface Check {

public interface CheckPersons {
boolean test(Person p1, Person p2);
boolean equals(Object other);
}
}