Search in sources :

Example 56 with CtInvocation

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

the class TemplateTest method testExtensionBlock.

@Test
public void testExtensionBlock() throws Exception {
    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/logger/LoggerTemplate.java"));
    launcher.addProcessor(new LoggerTemplateProcessor());
    launcher.getEnvironment().setSourceClasspath(System.getProperty("java.class.path").split(File.pathSeparator));
    try {
        launcher.run();
    } catch (ClassCastException ignored) {
        fail();
    }
    final CtClass<Logger> aLogger = launcher.getFactory().Class().get(Logger.class);
    final CtMethod aMethod = aLogger.getMethodsByName("enter").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 : CtInvocation(spoon.reflect.code.CtInvocation) LoggerTemplateProcessor(spoon.test.template.testclasses.logger.LoggerTemplateProcessor) Launcher(spoon.Launcher) FileSystemFile(spoon.support.compiler.FileSystemFile) Logger(spoon.test.template.testclasses.logger.Logger) CtTry(spoon.reflect.code.CtTry) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 57 with CtInvocation

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

the class TargetedExpressionTest method testTargetsOfInvInAnonymousClass.

@Test
public void testTargetsOfInvInAnonymousClass() throws Exception {
    // contract: Specify declaring type of the executable of an invocation, the target of the invocation, and its result. All this in an anonymous class.
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtClass<?> anonymousClass = type.getElements(new TypeFilter<CtClass>(CtClass.class) {

        @Override
        public boolean matches(CtClass element) {
            return element.isAnonymous() && super.matches(element);
        }
    }).get(0);
    final CtTypeReference<?> expectedAnonymousType = anonymousClass.getReference();
    final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtThisAccess expectedAnonymousThisAccess = factory.Code().createThisAccess(expectedAnonymousType);
    final CtMethod<?> method = anonymousClass.getMethodsByName("m").get(0);
    final List<CtInvocation> elements = method.getElements(new TypeFilter<>(CtInvocation.class));
    assertEquals(2, elements.size());
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.invStatic()"), elements.get(0));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("this.invStatic()"), elements.get(1));
}
Also used : Foo(spoon.test.targeted.testclasses.Foo) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) Test(org.junit.Test)

Example 58 with CtInvocation

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

the class TargetedExpressionTest method testTargetsOfInvInInnerClass.

@Test
public void testTargetsOfInvInInnerClass() throws Exception {
    // contract: Specify declaring type of the executable of an invocation, the target of the invocation and its result. All this in an innerclass.
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtTypeReference<SuperClass> expectedSuperClassType = factory.Class().<SuperClass>get(SuperClass.class).getReference();
    final CtType<?> innerClass = type.getNestedType("InnerClass");
    final CtTypeReference<?> expectedInnerClass = innerClass.getReference();
    final CtType<?> nestedTypeScanner = type.getNestedType("1NestedTypeScanner");
    final CtTypeReference<?> expectedNested = nestedTypeScanner.getReference();
    final CtTypeAccess<Foo> fooTypeAccess = factory.Code().createTypeAccess(expectedType);
    final CtThisAccess expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtThisAccess expectedSuperThisAccess = factory.Code().createThisAccess(expectedSuperClassType);
    final CtThisAccess<?> expectedInnerClassAccess = factory.Code().createThisAccess(expectedInnerClass);
    final CtThisAccess expectedNestedAccess = factory.Code().createThisAccess(expectedNested);
    final CtMethod<?> innerInvMethod = innerClass.getMethodsByName("innerInv").get(0);
    final List<CtInvocation<?>> elements = innerInvMethod.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
    assertEquals(8, elements.size());
    expectedThisAccess.setType(expectedInnerClass);
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("inv()"), elements.get(0));
    expectedThisAccess.setType(expectedType);
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.inv()"), elements.get(1));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(2));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(3));
    expectedSuperThisAccess.setType(expectedInnerClass);
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedSuperThisAccess).result("superMethod()"), elements.get(4));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("this.superMethod()"), elements.get(5));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("method()"), elements.get(6));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("this.method()"), elements.get(7));
    final List<CtInvocation> newElements = nestedTypeScanner.getMethodsByName("checkType").get(0).getElements(new TypeFilter<>(CtInvocation.class));
    assertEquals(1, newElements.size());
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedNested).target(expectedNestedAccess).result("this.checkType(type)"), newElements.get(0));
}
Also used : Foo(spoon.test.targeted.testclasses.Foo) SuperClass(spoon.test.targeted.testclasses.SuperClass) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) CtInvocation(spoon.reflect.code.CtInvocation) Test(org.junit.Test)

Example 59 with CtInvocation

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

the class VisibilityTest method testInvocationVisibilityInFieldDeclaration.

@Test
public void testInvocationVisibilityInFieldDeclaration() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/Solver.java");
    launcher.setSourceOutputDirectory("./target/spooned");
    launcher.buildModel();
    final CtType<Object> aSolver = launcher.getFactory().Type().get("org.sat4j.minisat.core.Solver");
    final CtField<?> lbdTimerField = aSolver.getField("lbdTimer");
    final CtInvocation<?> ctInvocation = lbdTimerField.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return "bound".equals(element.getExecutable().getSimpleName()) && super.matches(element);
        }
    }).get(0);
    assertNotNull(ctInvocation.getTarget());
    assertTrue(ctInvocation.getTarget().isImplicit());
    assertEquals("bound()", ctInvocation.toString());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 60 with CtInvocation

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

the class ImportTest method testAnotherMissingImport.

@Test
public void testAnotherMissingImport() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setLevel("OFF");
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/AnotherMissingImport.java"));
    compiler.build();
    List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "doSomething"));
    List<CtParameter> parameters = methods.get(0).getParameters();
    CtTypeReference<?> type = parameters.get(0).getType();
    assertEquals("SomeType", type.getSimpleName());
    assertEquals("externallib", type.getPackage().getSimpleName());
    CtMethod<?> mainMethod = factory.Class().getAll().get(0).getMethodsByName("main").get(0);
    List<CtStatement> statements = mainMethod.getBody().getStatements();
    CtStatement invocationStatement = statements.get(1);
    assertTrue(invocationStatement instanceof CtInvocation);
    CtInvocation invocation = (CtInvocation) invocationStatement;
    CtExecutableReference executableReference = invocation.getExecutable();
    assertEquals("doSomething(externallib.SomeType)", executableReference.getSignature());
    assertSame(methods.get(0), executableReference.getDeclaration());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtInvocation(spoon.reflect.code.CtInvocation) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) 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