diff --git a/src/main/java/spoon/reflect/factory/MethodFactory.java b/src/main/java/spoon/reflect/factory/MethodFactory.java index d7695519223..400fa7f4b1d 100644 --- a/src/main/java/spoon/reflect/factory/MethodFactory.java +++ b/src/main/java/spoon/reflect/factory/MethodFactory.java @@ -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> OBJECT_METHODS = Collections.unmodifiableSet(factory.Class().get(Object.class).getMethods()); + /** * Creates a new method sub-factory. * diff --git a/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java b/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java index bbf0336aa35..9515ea8cdac 100644 --- a/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java @@ -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; @@ -124,6 +124,9 @@ public CtMethod 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; } diff --git a/src/test/java/spoon/test/lambda/LambdaTest.java b/src/test/java/spoon/test/lambda/LambdaTest.java index b98979cb0e9..5a2d1127fb9 100644 --- a/src/test/java/spoon/test/lambda/LambdaTest.java +++ b/src/test/java/spoon/test/lambda/LambdaTest.java @@ -405,14 +405,15 @@ public void testLambdaFilter() throws Exception { } @Test - public void testLambdaComparator() throws Exception { - CtInterface comparatorInterface = factory.Interface().get(Comparator.class); - List> comparators = foo.filterChildren(new LambdaFilter(comparatorInterface)).list(); - assertEquals(1, comparators.size()); - CtLambda comparator = comparators.get(0); - assertEquals(2, comparator.getParameters().size()); - CtMethod method = comparator.getOverriddenMethod(); - assertTrue(comparatorInterface.getMethods().contains(method)); + public void testInterfaceWithObjectMethods() throws Exception { + CtInterface checkPersons = factory.Interface().get(Foo.CheckPersons.class); + List> 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 methodNames, String... strs) { diff --git a/src/test/java/spoon/test/lambda/testclasses/Foo.java b/src/test/java/spoon/test/lambda/testclasses/Foo.java index 148c4e05aa4..30efdfb0d40 100644 --- a/src/test/java/spoon/test/lambda/testclasses/Foo.java +++ b/src/test/java/spoon/test/lambda/testclasses/Foo.java @@ -56,10 +56,6 @@ public void m9() { }; } - public void m10() { - sortPersonsWithComparator(persons, (p1, p2) -> p1.age - p2.age); - } - public static void printPersonsWithPredicate(List roster, Predicate tester) { for (Person p : roster) { if (tester.test(p)) { @@ -90,10 +86,6 @@ public static void printPersonsWithCheckPersons(List roster, CheckPerson } } - public static void sortPersonsWithComparator(List roster, Comparator comparator) { - roster.sort(comparator); - } - public class Person { public final int age; @@ -119,5 +111,6 @@ public interface Check { public interface CheckPersons { boolean test(Person p1, Person p2); + boolean equals(Object other); } } \ No newline at end of file