Search in sources :

Example 26 with CtInvocation

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

the class ExecutableReferenceTest method testCallMethodOfClassNotPresent.

@Test
public void testCallMethodOfClassNotPresent() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/resources/executable-reference", "--output-type", "nooutput", "--noclasspath" });
    final List<CtExecutableReference<?>> references = Query.getReferences(launcher.getFactory(), new ReferenceTypeFilter<CtExecutableReference<?>>(CtExecutableReference.class) {

        @Override
        public boolean matches(CtExecutableReference<?> reference) {
            return !reference.isConstructor() && super.matches(reference);
        }
    });
    final List<CtInvocation<?>> invocations = Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return !element.getExecutable().isConstructor() && super.matches(element);
        }
    });
    assertEquals(4, references.size());
    assertEquals(4, invocations.size());
    // Executable reference with 0 parameter.
    final CtExecutableReference<?> executableZeroParameter = references.get(0);
    assertNotNull(executableZeroParameter.getDeclaringType());
    assertNull(executableZeroParameter.getType());
    assertEquals(0, executableZeroParameter.getParameters().size());
    assertEquals("m()", executableZeroParameter.toString());
    assertEquals("new Bar().m()", invocations.get(0).toString());
    // Executable reference with 1 parameter and return type.
    final CtExecutableReference<?> executableOneParameter = references.get(1);
    assertNotNull(executableOneParameter.getDeclaringType());
    assertNotNull(executableOneParameter.getType());
    assertEquals(1, executableOneParameter.getParameters().size());
    assertNotEquals(executableZeroParameter, executableOneParameter);
    assertEquals("m(int)", executableOneParameter.toString());
    assertEquals("bar.m(1)", invocations.get(1).toString());
    // Executable reference with 2 parameters.
    final CtExecutableReference<?> executableTwoParameters = references.get(2);
    assertNotNull(executableTwoParameters.getDeclaringType());
    assertNull(executableTwoParameters.getType());
    assertEquals(2, executableTwoParameters.getParameters().size());
    assertNotEquals(executableTwoParameters, executableZeroParameter);
    assertNotEquals(executableTwoParameters, executableOneParameter);
    assertEquals("m(int,java.lang.String)", executableTwoParameters.toString());
    assertEquals("new Bar().m(1, \"5\")", invocations.get(2).toString());
    // Static Executable reference.
    final CtExecutableReference<?> staticExecutable = references.get(3);
    assertNotNull(staticExecutable.getDeclaringType());
    assertNull(staticExecutable.getType());
    assertEquals(1, staticExecutable.getParameters().size());
    assertNotEquals(staticExecutable, executableZeroParameter);
    assertNotEquals(staticExecutable, executableOneParameter);
    assertEquals("m(java.lang.String)", staticExecutable.toString());
    assertEquals("Bar.m(\"42\")", invocations.get(3).toString());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) Test(org.junit.Test)

Example 27 with CtInvocation

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

the class ExecutableReferenceTest method testInvokeEnumMethod.

@Test
public void testInvokeEnumMethod() {
    final spoon.Launcher launcher = new spoon.Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/reference/Enum.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setComplianceLevel(8);
    launcher.buildModel();
    CtInvocation invocation = launcher.getModel().getElements(new TypeFilter<CtInvocation>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation element) {
            return super.matches(element) && element.getExecutable().getSimpleName().equals("valueOf");
        }
    }).get(0);
    assertNotNull(invocation.getExecutable().getExecutableDeclaration());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 28 with CtInvocation

use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.

the class TestMethodCallAdder method apply.

private CtMethod apply(CtMethod method, int invocation_index) {
    CtMethod<?> cloned_method = AmplificationHelper.cloneTestMethodForAmp(method, "_add");
    // add the cloned method in the same class as the original method
    // get the lit_indexth literal of the cloned method
    CtInvocation stmt = Query.getElements(cloned_method, new TypeFilter<>(CtInvocation.class)).get(invocation_index);
    CtInvocation cloneStmt = stmt.clone();
    final CtStatement parent = getParent(stmt);
    parent.insertBefore(cloneStmt);
    cloneStmt.setParent(parent.getParent(CtBlock.class));
    Counter.updateInputOf(cloned_method, 1);
    // DSpotUtils.addComment(cloneStmt, "MethodCallAdder", CtComment.CommentType.INLINE);
    return cloned_method;
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) TypeFilter(spoon.reflect.visitor.filter.TypeFilter)

Example 29 with CtInvocation

use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.

the class StatementAddTest method testOnClassWithJavaObjects.

@Test
public void testOnClassWithJavaObjects() throws Exception {
    /*
            Test that the StatementAdd amplifier is able to generate, and manage Collection and Map from java.util
         */
    final String packageName = "fr.inria.statementadd";
    InputProgram inputProgram = Utils.getInputProgram();
    final Factory factory = inputProgram.getFactory();
    inputProgram.setFactory(factory);
    AmplificationHelper.setSeedRandom(32L);
    StatementAdd amplifier = new StatementAdd(packageName);
    amplifier.reset(factory.Class().get(packageName + ".ClassTarget"));
    CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTarget"), "test");
    List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
    assertEquals(7, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("getList", "getSizeOf", "getSizeOfTypedCollection", "getSizeOfTypedMap");
    assertTrue(amplifiedMethods.stream().allMatch(amplifiedMethod -> amplifiedMethod.filterChildren(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return expectedCalledMethod.contains(element.getExecutable().getSimpleName());
        }
    }).first() != null));
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Arrays(java.util.Arrays) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) List(java.util.List) Utils(fr.inria.Utils) CtClass(spoon.reflect.declaration.CtClass) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractTest(fr.inria.AbstractTest) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) CtInvocation(spoon.reflect.code.CtInvocation) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 30 with CtInvocation

use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.

the class StatementAddTest method testStatementAddOnArrayObjects.

@Test
public void testStatementAddOnArrayObjects() throws Exception {
    final String packageName = "fr.inria.statementaddarray";
    InputProgram inputProgram = Utils.getInputProgram();
    final Factory factory = inputProgram.getFactory();
    inputProgram.setFactory(factory);
    AmplificationHelper.setSeedRandom(32L);
    StatementAdd amplifier = new StatementAdd(packageName);
    amplifier.reset(factory.Class().get(packageName + ".ClassTargetAmplify"));
    CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
    List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
    System.out.println(amplifiedMethods);
    assertEquals(5, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("methodWithArrayParatemeter", "methodWithArrayParatemeterFromDomain", "methodWithDomainParameter", "methodWithReturn", "method1");
    assertTrue(amplifiedMethods.stream().allMatch(amplifiedMethod -> amplifiedMethod.filterChildren(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return expectedCalledMethod.contains(element.getExecutable().getSimpleName());
        }
    }).first() != null));
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Arrays(java.util.Arrays) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) List(java.util.List) Utils(fr.inria.Utils) CtClass(spoon.reflect.declaration.CtClass) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractTest(fr.inria.AbstractTest) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) CtInvocation(spoon.reflect.code.CtInvocation) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

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