Search in sources :

Example 31 with CtInvocation

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

the class StatementAddTest method testStatementAddOnUnderTest.

@Test
public void testStatementAddOnUnderTest() throws Exception {
    Factory factory = Utils.getFactory();
    CtClass<Object> ctClass = factory.Class().get("fr.inria.mutation.ClassUnderTestTest");
    AmplificationHelper.setSeedRandom(23L);
    StatementAdd amplificator = new StatementAdd();
    amplificator.reset(ctClass);
    CtMethod originalMethod = Utils.findMethod(ctClass, "testLit");
    List<CtMethod> amplifiedMethods = amplificator.apply(originalMethod);
    System.out.println(amplifiedMethods);
    assertEquals(2, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("plusOne", "minusOne");
    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) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 32 with CtInvocation

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

the class ChangeMinimizer method minimize.

@Override
public CtMethod<?> minimize(CtMethod<?> amplifiedTestToBeMinimized) {
    final CtMethod<?> generalMinimize = super.minimize(amplifiedTestToBeMinimized);
    final CtMethod<?> changeMinimize = generalMinimize.clone();
    final long time = System.currentTimeMillis();
    final Failure failureToKeep = this.failurePerAmplifiedTest.get(amplifiedTestToBeMinimized);
    final List<CtInvocation> assertions = changeMinimize.filterChildren(AmplificationHelper.ASSERTIONS_FILTER).list();
    LOGGER.info("Minimizing {} assertions.", assertions.size());
    assertions.forEach(invocation -> {
        DSpotUtils.printProgress(assertions.indexOf(invocation), assertions.size());
        tryToRemoveAssertion(changeMinimize, invocation, failureToKeep);
    });
    LOGGER.info("Reduce {}, {} statements to {} statements in {} ms.", amplifiedTestToBeMinimized.getSimpleName(), generalMinimize.getBody().getStatements().size(), changeMinimize.getBody().getStatements().size(), System.currentTimeMillis() - time);
    // now that we reduce the amplified test, we must update the stack trace
    updateStackTrace(amplifiedTestToBeMinimized, changeMinimize);
    return changeMinimize;
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Failure(org.junit.runner.notification.Failure)

Example 33 with CtInvocation

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

the class TestMethodCallAdder method apply.

public List<CtMethod> apply(CtMethod method) {
    List<CtMethod> methods = new ArrayList<>();
    if (method.getDeclaringType() != null) {
        // get the list of method calls
        List<CtInvocation> invocations = Query.getElements(method, new TypeFilter(CtInvocation.class));
        // this index serves to replace ith literal is replaced by zero in the ith clone of the method
        int invocation_index = 0;
        for (CtInvocation invocation : invocations) {
            try {
                if (AmplificationChecker.canBeAdded(invocation) && !AmplificationChecker.isAssert(invocation)) {
                    methods.add(apply(method, invocation_index));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            invocation_index++;
        }
    }
    return methods;
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) ArrayList(java.util.ArrayList) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod)

Example 34 with CtInvocation

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

the class CtParameterRemoveRefactoring method computeAllInvocations.

/**
 * search for all methods and lambdas which has to be refactored together with target method
 */
private void computeAllInvocations() {
    ExecutableReferenceFilter execRefFilter = new ExecutableReferenceFilter();
    for (CtExecutable<?> exec : getTargetExecutables()) {
        execRefFilter.addExecutable(exec);
    }
    // all the invocations, which belongs to same inheritance tree
    final List<CtInvocation<?>> invocations = new ArrayList<>();
    target.getFactory().getModel().filterChildren(execRefFilter).forEach(new CtConsumer<CtExecutableReference<?>>() {

        @Override
        public void accept(CtExecutableReference<?> t) {
            CtElement parent = t.getParent();
            if (parent instanceof CtInvocation<?>) {
                invocations.add((CtInvocation<?>) parent);
            }
        // else ignore other hits, which are not in context of invocation
        }
    });
    targetInvocations = Collections.unmodifiableList(invocations);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter)

Example 35 with CtInvocation

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

the class GenericsTest method testGetExecDeclarationOfEnumSetOf.

@Test
public void testGetExecDeclarationOfEnumSetOf() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/EnumSetOf.java");
    launcher.buildModel();
    CtClass<?> ctClass = launcher.getFactory().Class().get(EnumSetOf.class);
    CtInvocation invocation = ctClass.getMethodsByName("m").get(0).getBody().getStatement(0);
    CtExecutable<?> decl = invocation.getExecutable().getDeclaration();
    assertNull(decl);
    CtClass<?> enumClass = launcher.getFactory().Class().get(EnumSet.class);
    List<CtMethod<?>> methods = enumClass.getMethodsByName("of");
    CtMethod rightOfMethod = null;
    for (CtMethod method : methods) {
        if (method.getParameters().size() == 1) {
            rightOfMethod = method;
        }
    }
    assertNotNull(rightOfMethod);
    decl = invocation.getExecutable().getExecutableDeclaration();
    assertEquals(rightOfMethod, decl);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) 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