Search in sources :

Example 6 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class ImportTest method testStaticImportForInvocationInNoClasspath.

@Test
public void testStaticImportForInvocationInNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/resources/import-static", "--output-type", "nooutput", "--noclasspath" });
    final List<CtInvocation<?>> elements = new SortedList(new CtLineElementComparator());
    elements.addAll(Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return !element.getExecutable().isConstructor() && super.matches(element);
        }
    }));
    // Invocation for a static method with the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(true), elements.get(0));
    // Invocation for a static method without the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(true), elements.get(1));
    // Invocation for a static method with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(false), elements.get(2));
    // Invocation for a static method without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(false), elements.get(3));
    // Invocation for a static method in an inner class with the declaring class specified.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(4));
    // Invocation for a static method in an inner class without the declaring class specified.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(true), elements.get(5));
    // Invocation for a static method in an inner class with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(6));
    // Invocation for a static method in an innser class without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(7));
    // Invocation for a static method in an inner class with the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(true), elements.get(8));
    // Invocation for a static method in an inner class without the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(true), elements.get(9));
    // Invocation for a static method in an inner class with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(false), elements.get(10));
    // Invocation for a static method in an inner class without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(false), elements.get(11));
    // Invocation for a static method with the declaring class specified and an import *.
    assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(true), elements.get(12));
    // Invocation for a static method without the declaring class specified and an import *.
    assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(true), elements.get(13));
    // Invocation for a static method with the declaring class specified, a return type and an import *.
    assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(false), elements.get(14));
    // Invocation for a static method without the declaring class specified, a return type and an import *.
    assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(false), elements.get(15));
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) SortedList(spoon.support.util.SortedList) Launcher(spoon.Launcher) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 7 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class TemplateTest method testExtensionDecoupledSubstitutionVisitor.

@Test
public void testExtensionDecoupledSubstitutionVisitor() throws Exception {
    // contract: substitution can be done on model, which is not based on Template
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/template/testclasses/logger/Logger.java");
    launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/LoggerModel.java"));
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> aTemplateModelType = launcher.getFactory().Class().get(LoggerModel.class);
    final CtMethod<?> aTemplateModel = aTemplateModelType.getMethod("block");
    final CtClass<?> aTargetType = launcher.getFactory().Class().get(Logger.class);
    final CtMethod<?> toBeLoggedMethod = aTargetType.getMethodsByName("enter").get(0);
    Map<String, Object> params = new HashMap<>();
    params.put("_classname_", factory.Code().createLiteral(aTargetType.getSimpleName()));
    params.put("_methodName_", factory.Code().createLiteral(toBeLoggedMethod.getSimpleName()));
    params.put("_block_", toBeLoggedMethod.getBody());
    final List<CtMethod<?>> aMethods = new SubstitutionVisitor(factory, params).substitute(aTemplateModel.clone());
    assertEquals(1, aMethods.size());
    final CtMethod<?> aMethod = aMethods.get(0);
    assertTrue(aMethod.getBody().getStatement(0) instanceof CtTry);
    final CtTry aTry = (CtTry) aMethod.getBody().getStatement(0);
    assertTrue(aTry.getFinalizer().getStatement(0) instanceof CtInvocation);
    assertEquals("spoon.test.template.testclasses.logger.Logger.exit(\"enter\")", aTry.getFinalizer().getStatement(0).toString());
    assertTrue(aTry.getBody().getStatement(0) instanceof CtInvocation);
    assertEquals("spoon.test.template.testclasses.logger.Logger.enter(\"Logger\", \"enter\")", aTry.getBody().getStatement(0).toString());
    assertTrue(aTry.getBody().getStatements().size() > 1);
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) CtTry(spoon.reflect.code.CtTry) SubstitutionVisitor(spoon.support.template.SubstitutionVisitor) CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 8 with CtInvocation

use of spoon.reflect.code.CtInvocation 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 9 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class SignatureTest method testNullSignatureInUnboundVariable.

@Test
public void testNullSignatureInUnboundVariable() throws Exception {
    // Unbound variable access bug fix:
    // Bug description: The signature printer ignored the element Unbound variable reference
    // (as well all Visitor that extend CtVisitor)
    // Fix description: modify CtVisitor (including SignaturePrinter) for visiting unbound variable access.
    Factory factory = new Launcher().createFactory();
    // We want to compile a class with an reference to a class that is not
    // in the classpath
    // As consequence, we set the option NoClasspath as true.
    factory.getEnvironment().setNoClasspath(true);
    String unboundVarAccess = "Complex.I";
    String content = "" + "class X {" + "public Object foo(java.util.List<String> l) {" + " Integer.toString(" + unboundVarAccess + ");" + " return null;" + "}};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    try {
        builder.build();
        Assert.fail();
    } catch (Exception e) {
    // Must fail due to the unbound element "Complex.I"
    }
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    Set<CtMethod<?>> methods = clazz1.getMethods();
    CtMethod<?> method = (CtMethod<?>) methods.toArray()[0];
    assertEquals("foo(java.util.List)", method.getSignature());
    CtInvocation<?> invo = (CtInvocation<?>) method.getBody().getStatement(0);
    CtExpression<?> argument1 = invo.getArguments().get(0);
    assertEquals(unboundVarAccess, argument1.toString());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 10 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class TargetedExpressionTest method testStaticTargetsOfInv.

@Test
public void testStaticTargetsOfInv() throws Exception {
    // contract: Specify declaring type of the executable of an static invocation, the target of the static invocation and its result.
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtClass<Foo.Fii.Fuu> fuu = factory.Class().<Foo.Fii.Fuu>get(Foo.Fii.Fuu.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtTypeReference<Bar> expectedBarType = factory.Class().<Bar>get(Bar.class).getReference();
    final CtTypeReference<Foo.Fii.Fuu> expectedFuuType = fuu.getReference();
    final CtThisAccess<Foo> expectedThisAccess = type.getFactory().Code().createThisAccess(expectedType);
    final CtTypeAccess<Foo> expectedTypeAccess = type.getFactory().Code().createTypeAccess(expectedType);
    final CtTypeAccess<Bar> expectedBarTypeAccess = type.getFactory().Code().createTypeAccess(expectedBarType);
    final CtMethod<?> invMethod = type.getMethodsByName("invStatic").get(0);
    final List<CtInvocation<?>> elements = invMethod.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
    assertEquals(8, elements.size());
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtConstructorCallImpl.class).result("new spoon.test.targeted.testclasses.Foo(0, 0).staticMethod()"), elements.get(0));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtFieldReadImpl.class).result("foo.staticMethod()"), elements.get(1));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.staticMethod()"), elements.get(2));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(3));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(4));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.staticMethodBar()"), elements.get(5));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.staticMethodBar()"), elements.get(6));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedFuuType).target(factory.Code().createTypeAccess(expectedFuuType)).result("spoon.test.targeted.testclasses.Foo.Fii.Fuu.m()"), elements.get(7));
}
Also used : Foo(spoon.test.targeted.testclasses.Foo) Factory(spoon.reflect.factory.Factory) Bar(spoon.test.targeted.testclasses.Bar) CtInvocation(spoon.reflect.code.CtInvocation) Test(org.junit.Test)

Aggregations

CtInvocation (spoon.reflect.code.CtInvocation)64 Test (org.junit.Test)49 Factory (spoon.reflect.factory.Factory)30 Launcher (spoon.Launcher)27 CtMethod (spoon.reflect.declaration.CtMethod)25 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)25 CtClass (spoon.reflect.declaration.CtClass)18 CtTypeReference (spoon.reflect.reference.CtTypeReference)11 List (java.util.List)9 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)9 CtStatement (spoon.reflect.code.CtStatement)8 CtBlock (spoon.reflect.code.CtBlock)7 CtElement (spoon.reflect.declaration.CtElement)7 CtIf (spoon.reflect.code.CtIf)6 CtLiteral (spoon.reflect.code.CtLiteral)6 AbstractTest (fr.inria.AbstractTest)5 InputProgram (fr.inria.diversify.utils.sosiefier.InputProgram)5 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)5