Search in sources :

Example 1 with DeepRepresentationComparator

use of spoon.support.comparator.DeepRepresentationComparator 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 DeepRepresentationComparator

use of spoon.support.comparator.DeepRepresentationComparator in project spoon by INRIA.

the class SignatureTest method testMethodInvocationSignatureWithVariableAccess.

@Test
public void testMethodInvocationSignatureWithVariableAccess() throws Exception {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    String content = "" + "class PR {" + "static String PRS = null;" + "public Object foo(String p) {" + " int s = 0; 	" + " this.foo(s);" + "this.foo(p);" + " return null;" + "}" + " public Object foo(int p) {" + " String s = null;" + " this.foo(s);" + "this.foo(p);" + "return null;" + "}" + "};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    builder.build();
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    assertNotNull(clazz1);
    // **FIRST PART: passing local variable access.
    // /--------From the first method we take the method invocations
    TreeSet<CtMethod<?>> ts = new TreeSet<CtMethod<?>>(new DeepRepresentationComparator());
    ts.addAll(clazz1.getMethods());
    CtMethod[] methodArray = ts.toArray(new CtMethod[0]);
    CtMethod<?> methodInteger = methodArray[0];
    assertEquals("foo(int)", methodInteger.getSignature());
    CtInvocation<?> invoToInt1 = (CtInvocation<?>) methodInteger.getBody().getStatement(1);
    CtExpression<?> argumentToInt1 = invoToInt1.getArguments().get(0);
    // ----------From the second method we take the Method Inv
    CtMethod<?> methodString = (CtMethod<?>) methodArray[1];
    assertEquals("foo(java.lang.String)", methodString.getSignature());
    CtInvocation<?> invoToString = (CtInvocation<?>) methodString.getBody().getStatement(1);
    CtExpression<?> argumentToString = invoToString.getArguments().get(0);
    // we compare the signatures of " this.foo(s);"	from both methods
    assertNotEquals(invoToInt1, invoToString);
    // Now we check that we have two invocation to "foo(s)",
    // but one invocation is with var 's' type integer, the other var 's' type int
    assertNotEquals(argumentToInt1, argumentToString);
    // / ***SECOND PART, passing Parameters
    CtInvocation<?> invoToString2 = (CtInvocation<?>) methodInteger.getBody().getStatement(2);
    CtExpression<?> argumentToString2 = invoToString2.getArguments().get(0);
    CtInvocation<?> invoToInt2 = (CtInvocation<?>) methodString.getBody().getStatement(2);
    CtExpression<?> argumentToInt2 = invoToInt2.getArguments().get(0);
    // /
    // Compare the method invo signature (same real argument's name, different type)
    assertNotEquals(invoToString2, invoToInt2);
    // Compare signature of parameters (same var name, different type)
    assertNotEquals(argumentToString2, argumentToInt2);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) DefaultCoreFactory(spoon.support.DefaultCoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtClass(spoon.reflect.declaration.CtClass) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) CtInvocation(spoon.reflect.code.CtInvocation) TreeSet(java.util.TreeSet) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 3 with DeepRepresentationComparator

use of spoon.support.comparator.DeepRepresentationComparator in project spoon by INRIA.

the class LiteralTest method testCharLiteralInNoClasspath.

@Test
public void testCharLiteralInNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/SecondaryIndexManager.java");
    launcher.setSourceOutputDirectory("./target/literal");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("org.apache.cassandra.index.SecondaryIndexManager");
    TreeSet<CtLiteral<?>> ts = new TreeSet<CtLiteral<?>>(new DeepRepresentationComparator());
    ts.addAll(aClass.getElements(new TypeFilter<CtLiteral<Character>>(CtLiteral.class) {

        @Override
        public boolean matches(CtLiteral element) {
            return element.getValue() instanceof Character && super.matches(element);
        }
    }));
    assertTrue(ts.last().getType().isPrimitive());
    assertEquals(':', ts.last().getValue());
    canBeBuilt("./target/literal", 8, true);
}
Also used : DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) CtLiteral(spoon.reflect.code.CtLiteral) TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 4 with DeepRepresentationComparator

use of spoon.support.comparator.DeepRepresentationComparator 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 5 with DeepRepresentationComparator

use of spoon.support.comparator.DeepRepresentationComparator 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)

Aggregations

TreeSet (java.util.TreeSet)6 DeepRepresentationComparator (spoon.support.comparator.DeepRepresentationComparator)6 Test (org.junit.Test)5 Launcher (spoon.Launcher)5 CtMethod (spoon.reflect.declaration.CtMethod)4 OverridingMethodFilter (spoon.reflect.visitor.filter.OverridingMethodFilter)3 CtClass (spoon.reflect.declaration.CtClass)2 Factory (spoon.reflect.factory.Factory)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 AbstractTostada (spoon.test.filters.testclasses.AbstractTostada)2 ITostada (spoon.test.filters.testclasses.ITostada)2 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 SpoonModelBuilder (spoon.SpoonModelBuilder)1 CtConstructorCall (spoon.reflect.code.CtConstructorCall)1 CtInvocation (spoon.reflect.code.CtInvocation)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtType (spoon.reflect.declaration.CtType)1 FactoryImpl (spoon.reflect.factory.FactoryImpl)1 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)1