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());
}
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);
}
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);
}
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());
}
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());
}
Aggregations