Search in sources :

Example 1 with OverridingMethodFilter

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());
}
Also used : Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) SubTostada(spoon.test.filters.testclasses.SubTostada) CtMethod(spoon.reflect.declaration.CtMethod) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) Test(org.junit.Test)

Example 2 with OverridingMethodFilter

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());
}
Also used : AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) Test(org.junit.Test)

Example 3 with OverridingMethodFilter

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());
}
Also used : AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) Test(org.junit.Test)

Example 4 with OverridingMethodFilter

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());
}
Also used : DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) CtType(spoon.reflect.declaration.CtType) TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) ITostada(spoon.test.filters.testclasses.ITostada) CtMethod(spoon.reflect.declaration.CtMethod) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) Test(org.junit.Test)

Example 5 with OverridingMethodFilter

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];
}
Also used : SpoonMetaModel(spoon.test.metamodel.SpoonMetaModel) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DerivedProperty(spoon.support.DerivedProperty) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) ArrayList(java.util.ArrayList) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)6 OverridingMethodFilter (spoon.reflect.visitor.filter.OverridingMethodFilter)6 Test (org.junit.Test)4 Launcher (spoon.Launcher)4 TreeSet (java.util.TreeSet)3 CtType (spoon.reflect.declaration.CtType)3 DeepRepresentationComparator (spoon.support.comparator.DeepRepresentationComparator)3 AbstractTostada (spoon.test.filters.testclasses.AbstractTostada)3 ITostada (spoon.test.filters.testclasses.ITostada)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CtElement (spoon.reflect.declaration.CtElement)2 CtTypeReference (spoon.reflect.reference.CtTypeReference)2 SubTostada (spoon.test.filters.testclasses.SubTostada)2 Tostada (spoon.test.filters.testclasses.Tostada)2 File (java.io.File)1 Collection (java.util.Collection)1 Set (java.util.Set)1 SpoonException (spoon.SpoonException)1 CtAssignment (spoon.reflect.code.CtAssignment)1