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

feat: add method CtMethod#getTopDefinitions #1844

Merged
merged 4 commits into from
Feb 16, 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
9 changes: 9 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import spoon.reflect.annotations.PropertyGetter;
import spoon.reflect.annotations.PropertySetter;

import java.util.Collection;

import static spoon.reflect.path.CtRole.IS_DEFAULT;


Expand Down Expand Up @@ -50,4 +52,11 @@ public interface CtMethod<T> extends CtExecutable<T>, CtTypeMember, CtFormalType

@Override
CtMethod<T> clone();

/**
* Returns the top-most methods in the hierarchy defining this method
* (in super class and super interfaces).
* Returns the empty collection if defined here for the first time.
*/
Collection<CtMethod<?>> getTopDefinitions();
}
30 changes: 30 additions & 0 deletions src/main/java/spoon/support/reflect/declaration/CtMethodImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.visitor.filter.AllTypeMembersFunction;
import spoon.support.reflect.CtExtendedModifier;
import spoon.support.reflect.CtModifierHandler;
import spoon.support.visitor.ClassTypingContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -217,6 +219,34 @@ public CtMethod<T> clone() {
return (CtMethod<T>) super.clone();
}

@Override
public Collection<CtMethod<?>> getTopDefinitions() {
List<CtMethod<?>> s = new ArrayList<>();

// first collect potential declarations of this method in the type hierarchy
ClassTypingContext context = new ClassTypingContext(this.getDeclaringType());
getDeclaringType().map(new AllTypeMembersFunction(CtMethod.class)).forEach((CtMethod<?> m) -> {
if (m != this && context.isOverriding(this, m)) {
s.add(m);
}
});

// now removing the intermediate methods for which there exists a definition upper in the hierarchy
List<CtMethod<?>> finalMeths = new ArrayList<>(s);
for (CtMethod m1 : s) {
boolean m1IsIntermediate = false;
for (CtMethod m2 : s) {
if (context.isOverriding(m1, m2)) {
m1IsIntermediate = true;
}
}
if (!m1IsIntermediate) {
finalMeths.add(m1);
}
}
return finalMeths;
}

@Override
public boolean isPublic() {
return this.modifierHandler.isPublic();
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/spoon/test/filters/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -74,6 +77,7 @@
import spoon.reflect.visitor.filter.ReturnOrThrowFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.comparator.DeepRepresentationComparator;
import spoon.support.comparator.QualifiedNameComparator;
import spoon.support.reflect.declaration.CtMethodImpl;
import spoon.support.visitor.SubInheritanceHierarchyResolver;
import spoon.test.filters.testclasses.AbstractTostada;
Expand Down Expand Up @@ -382,6 +386,51 @@ public void testOverriddenMethodsFromSubClassOfAbstractClass() throws Exception
assertEquals(Tostada.class, overridenMethodsFromSub.get(1).getParent(CtClass.class).getActualClass());
}

@Test
public void testgetTopDefinitions() throws Exception {
// contract: getTopDefinitions returns the correct number of definitions
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();

// start with ToStatda
final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
List<CtMethod<?>> methods;

methods = orderByName(aTostada.getMethodsByName("make").get(0).getTopDefinitions());
assertEquals(2, methods.size());
assertEquals("AbstractTostada", methods.get(0).getDeclaringType().getSimpleName());
assertEquals("ITostada", methods.get(1).getDeclaringType().getSimpleName());

methods = orderByName(aTostada.getMethodsByName("prepare").get(0).getTopDefinitions());
assertEquals(1, methods.size());
assertEquals("AbstractTostada", methods.get(0).getDeclaringType().getSimpleName());

methods = orderByName(aTostada.getMethodsByName("toString").get(0).getTopDefinitions());
assertEquals(1, methods.size());
assertEquals("Object", methods.get(0).getDeclaringType().getSimpleName());

methods = orderByName(aTostada.getMethodsByName("honey").get(0).getTopDefinitions());
assertEquals(2, methods.size());
assertEquals("AbstractTostada", methods.get(0).getDeclaringType().getSimpleName());
assertEquals("Honey", methods.get(1).getDeclaringType().getSimpleName());

methods = orderByName(aTostada.getMethodsByName("foo").get(0).getTopDefinitions());
assertEquals(0, methods.size());
}

private List<CtMethod<?>> orderByName(Collection<CtMethod<?>> meths) {
List<CtMethod<?>> ordered = new ArrayList<>(meths);
ordered.sort(new Comparator<CtMethod<?>>() {
@Override
public int compare(CtMethod<?> o1, CtMethod<?> o2) {
return o1.getParent(CtType.class).getQualifiedName().compareTo(o2.getParent(CtType.class).getQualifiedName());
}
});
return ordered;
}

@Test
public void testOverriddenMethodFromInterface() throws Exception {
// contract: When we declare a method in an interface, we must return an empty list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public ITostada make() {
}

public abstract void prepare();

public void honey() {

}
}
4 changes: 3 additions & 1 deletion src/test/java/spoon/test/filters/testclasses/ITostada.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package spoon.test.filters.testclasses;

public interface ITostada {
public interface ITostada extends Foo {
ITostada make();
}
interface Foo {
}
19 changes: 18 additions & 1 deletion src/test/java/spoon/test/filters/testclasses/Tostada.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package spoon.test.filters.testclasses;

public class Tostada extends AbstractTostada {

public class Tostada extends AbstractTostada implements Honey {
@Override
public ITostada make() {
return new Tostada() {
Expand All @@ -32,4 +33,20 @@ public void prepare() {
@Override
public void prepare() {
}

@Override
public String toString() {
return "";
}

@Override
public void honey() {

}

public void foo() {}
}

interface Honey {
void honey();
}