Skip to content

Commit

Permalink
Fix counting java.lang.Object methods as abstract methods in interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
krakowski committed Jun 5, 2018
1 parent 7494a39 commit 083bd5c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
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
17 changes: 9 additions & 8 deletions src/test/java/spoon/test/lambda/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,15 @@ public void testLambdaFilter() throws Exception {
}

@Test
public void testLambdaComparator() throws Exception {
CtInterface<?> comparatorInterface = factory.Interface().get(Comparator.class);
List<CtLambda<?>> 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<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) {
Expand Down
9 changes: 1 addition & 8 deletions src/test/java/spoon/test/lambda/testclasses/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ public void m9() {
};
}

public void m10() {
sortPersonsWithComparator(persons, (p1, p2) -> p1.age - p2.age);
}

public static void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester) {
for (Person p : roster) {
if (tester.test(p)) {
Expand Down Expand Up @@ -90,10 +86,6 @@ public static void printPersonsWithCheckPersons(List<Person> roster, CheckPerson
}
}

public static void sortPersonsWithComparator(List<Person> roster, Comparator<Person> comparator) {
roster.sort(comparator);
}

public class Person {
public final int age;

Expand All @@ -119,5 +111,6 @@ public interface Check {

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

0 comments on commit 083bd5c

Please sign in to comment.