use of spoon.reflect.visitor.filter.OverridingMethodFilter in project spoon by INRIA.
the class FilterTest method testOverridingMethodFromSubClassOfAbstractClass.
@Test
public void testOverridingMethodFromSubClassOfAbstractClass() throws Exception {
// contract: When we ask all overriding methods from an overriding method, we must returns all methods
// below and not above (including the declaration).
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
TreeSet<CtMethod<?>> ts = new TreeSet<CtMethod<?>>(new DeepRepresentationComparator());
List<CtMethod<?>> elements = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aTostada.getMethodsByName("prepare").get(0)));
ts.addAll(elements);
final List<CtMethod<?>> overridingMethods = Arrays.asList(ts.toArray(new CtMethod[0]));
assertEquals(3, overridingMethods.size());
assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(2).getParent(CtClass.class).getQualifiedName());
assertEquals(SubTostada.class, overridingMethods.get(1).getParent(CtClass.class).getActualClass());
assertEquals("spoon.test.filters.testclasses.Tostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
final CtClass<SubTostada> aSubTostada = launcher.getFactory().Class().get(SubTostada.class);
assertEquals(0, Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aSubTostada.getMethodsByName("prepare").get(0))).size());
}
use of spoon.reflect.visitor.filter.OverridingMethodFilter in project spoon by INRIA.
the class FilterTest method testOverridingMethodFromSubClassOfInterface.
@Test
public void testOverridingMethodFromSubClassOfInterface() throws Exception {
// contract: When we ask all overriding methods from an overriding method, we must returns all methods
// below and not above (including the declaration).
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<AbstractTostada> anAbstractTostada = launcher.getFactory().Class().get(AbstractTostada.class);
List<CtMethod<?>> overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(anAbstractTostada.getMethodsByName("make").get(0)));
assertEquals(2, overridingMethods.size());
assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
assertEquals(Tostada.class, overridingMethods.get(1).getParent(CtClass.class).getActualClass());
final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aTostada.getMethodsByName("make").get(0)));
assertEquals(1, overridingMethods.size());
assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
}
use of spoon.reflect.visitor.filter.OverridingMethodFilter in project spoon by INRIA.
the class FilterTest method testOverridingMethodFromAbstractClass.
@Test
public void testOverridingMethodFromAbstractClass() throws Exception {
// contract: When we declare an abstract method on an abstract class, we must return all overriding
// methods in sub classes and anonymous classes.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<AbstractTostada> aClass = launcher.getFactory().Class().get(AbstractTostada.class);
TreeSet<CtMethod<?>> ts = new TreeSet<CtMethod<?>>(new DeepRepresentationComparator());
List<CtMethod<?>> elements = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aClass.getMethodsByName("prepare").get(0)));
ts.addAll(elements);
assertEquals(5, elements.size());
final List<CtMethod<?>> overridingMethods = Arrays.asList(ts.toArray(new CtMethod[0]));
assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(3).getParent(CtClass.class).getQualifiedName());
assertEquals(Antojito.class, overridingMethods.get(1).getParent(CtClass.class).getActualClass());
assertEquals(SubTostada.class, overridingMethods.get(2).getParent(CtClass.class).getActualClass());
assertEquals("spoon.test.filters.testclasses.Tostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
assertEquals(Tostada.class, overridingMethods.get(4).getParent(CtClass.class).getActualClass());
}
use of spoon.reflect.visitor.filter.OverridingMethodFilter in project spoon by INRIA.
the class FilterTest method testOverridingMethodFromInterface.
@Test
public void testOverridingMethodFromInterface() throws Exception {
// contract: When we declare a method in an interface, we must return all overriding
// methods in sub classes and anonymous classes.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtInterface<ITostada> aITostada = launcher.getFactory().Interface().get(ITostada.class);
TreeSet<CtMethod<?>> ts = new TreeSet<CtMethod<?>>(new DeepRepresentationComparator());
List<CtMethod<?>> elements = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aITostada.getMethodsByName("make").get(0)));
ts.addAll(elements);
final List<CtMethod<?>> overridingMethods = Arrays.asList(ts.toArray(new CtMethod[0]));
assertEquals(4, overridingMethods.size());
assertEquals(AbstractTostada.class, overridingMethods.get(3).getParent(CtType.class).getParent(CtClass.class).getActualClass());
assertEquals("spoon.test.filters.testclasses.AbstractTostada", overridingMethods.get(1).getParent(CtClass.class).getQualifiedName());
assertEquals(Tostada.class, overridingMethods.get(0).getParent(CtClass.class).getActualClass());
assertEquals(Tacos.class, overridingMethods.get(2).getParent(CtClass.class).getActualClass());
}
use of spoon.reflect.visitor.filter.OverridingMethodFilter in project spoon by INRIA.
the class SpoonTestHelpers method getSetterOf.
/**
* returns the corresponding setter, if several are possible returns the lowest one in the hierarchy
*/
public static CtMethod<?> getSetterOf(CtType<?> baseType, CtMethod<?> getter) {
String setterName = getter.getSimpleName().replaceFirst("^get", "set");
Object[] tentativeSetters = baseType.getAllMethods().stream().filter(x -> x.getSimpleName().equals(setterName)).toArray();
if (tentativeSetters.length == 0) {
return null;
}
// return one that is as low as possible in the hierarchy
for (Object o : tentativeSetters) {
if (baseType.getPackage().getElements(new OverridingMethodFilter((CtMethod<?>) o)).size() == 0) {
return (CtMethod<?>) o;
}
}
// System.out.println(setterName+" "+tentativeSetters.length);
return (CtMethod<?>) tentativeSetters[0];
}
Aggregations