Search in sources :

Example 56 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class VisibilityTest method testFullyQualifiedNameOfTypeReferenceWithGeneric.

@Test
public void testFullyQualifiedNameOfTypeReferenceWithGeneric() throws Exception {
    // contract: Generics are written when there are specified in the return type of a method.
    final String target = "./target/spooned/spoon/test/visibility_generics/testclasses/";
    final SpoonAPI launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A.java");
    launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/A2.java");
    launcher.addInputResource("./src/test/java/spoon/test/visibility/testclasses/Foo.java");
    launcher.setSourceOutputDirectory(target);
    launcher.run();
    final CtClass<A> aClass = launcher.getFactory().Class().get(A.class);
    CtType<?> nestedB = aClass.getNestedType("B");
    List<CtFieldAccess> elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(1, elements.size());
    assertEquals("(spoon.test.visibility.testclasses.A.B.i)", elements.get(0).toString());
    CtMethod<?> instanceOf = aClass.getMethodsByName("instanceOf").get(0);
    List<CtBinaryOperator> elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
    assertEquals(1, elements1.size());
    assertEquals("spoon.test.visibility.testclasses.A.B", elements1.get(0).getRightHandOperand().toString());
    CtMethod<?> returnType = aClass.getMethodsByName("returnType").get(0);
    assertEquals("spoon.test.visibility.testclasses.A<T>.C<T>", returnType.getType().toString());
    final CtClass<A2> secondClass = launcher.getFactory().Class().get(A2.class);
    nestedB = secondClass.getNestedType("B");
    elements = nestedB.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(1, elements.size());
    assertEquals("(spoon.test.visibility.testclasses.A2.B.i)", elements.get(0).toString());
    instanceOf = secondClass.getMethodsByName("instanceOf").get(0);
    elements1 = instanceOf.getElements(new TypeFilter<>(CtBinaryOperator.class));
    assertEquals(1, elements1.size());
    assertEquals("spoon.test.visibility.testclasses.A2.B", elements1.get(0).getRightHandOperand().toString());
    returnType = secondClass.getMethodsByName("returnType").get(0);
    assertEquals("spoon.test.visibility.testclasses.A2.C<java.lang.String>", returnType.getType().toString());
    returnType = secondClass.getMethodsByName("returnType2").get(0);
    assertEquals("spoon.test.visibility.testclasses.Foo<java.lang.String>.Bar<java.lang.String>", returnType.getType().toString());
    canBeBuilt(target, 8);
}
Also used : A(spoon.test.visibility.testclasses.A) CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) A2(spoon.test.visibility.testclasses.A2) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 57 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class MethodsAssertGenerator method buildTestWithAssert.

/**
 * Adds new assertions to a test from observation points.
 *
 * @param test Test method
 * @param observations Observation points of the test suite
 * @return Test with new assertions
 */
@SuppressWarnings("unchecked")
private CtMethod<?> buildTestWithAssert(CtMethod test, Map<String, Observation> observations) {
    CtMethod testWithAssert = AmplificationHelper.cloneTestMethodForAmp(test, "");
    int numberOfAddedAssertion = 0;
    List<CtStatement> statements = Query.getElements(testWithAssert, new TypeFilter(CtStatement.class));
    for (String id : observations.keySet()) {
        if (!id.split("__")[0].equals(testWithAssert.getSimpleName())) {
            continue;
        }
        final List<CtStatement> assertStatements = AssertBuilder.buildAssert(factory, observations.get(id).getNotDeterministValues(), observations.get(id).getObservationValues(), Double.parseDouble(configuration.getProperties().getProperty("delta", "0.1")));
        if (assertStatements.stream().map(Object::toString).map("// AssertGenerator add assertion\n"::concat).anyMatch(testWithAssert.getBody().getLastStatement().toString()::equals)) {
            continue;
        }
        int line = Integer.parseInt(id.split("__")[1]);
        CtStatement lastStmt = null;
        for (CtStatement statement : assertStatements) {
            DSpotUtils.addComment(statement, "AssertGenerator add assertion", CtComment.CommentType.INLINE);
            try {
                CtStatement stmt = statements.get(line);
                if (lastStmt == null) {
                    lastStmt = stmt;
                }
                if (stmt instanceof CtBlock) {
                    break;
                }
                if (stmt instanceof CtInvocation && !AssertGeneratorHelper.isVoidReturn((CtInvocation) stmt) && stmt.getParent() instanceof CtBlock) {
                    CtInvocation invocationToBeReplaced = (CtInvocation) stmt.clone();
                    final CtLocalVariable localVariable = factory.createLocalVariable(invocationToBeReplaced.getType(), "o_" + id.split("___")[0], invocationToBeReplaced);
                    stmt.replace(localVariable);
                    DSpotUtils.addComment(localVariable, "AssertGenerator create local variable with return value of invocation", CtComment.CommentType.INLINE);
                    localVariable.setParent(stmt.getParent());
                    if (id.endsWith("end")) {
                        testWithAssert.getBody().insertEnd(statement);
                    } else {
                        localVariable.insertAfter(statement);
                    }
                    statements.remove(line);
                    statements.add(line, localVariable);
                } else {
                    if (id.endsWith("end")) {
                        stmt.getParent(CtBlock.class).insertEnd(statement);
                    } else {
                        lastStmt.insertAfter(statement);
                    }
                }
                lastStmt = statement;
                numberOfAddedAssertion++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    Counter.updateAssertionOf(testWithAssert, numberOfAddedAssertion);
    if (!testWithAssert.equals(test)) {
        return testWithAssert;
    } else {
        AmplificationHelper.removeAmpTestParent(testWithAssert);
        return null;
    }
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) TestTimedOutException(org.junit.runners.model.TestTimedOutException) IOException(java.io.IOException)

Example 58 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter 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 59 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class TestMethodCallRemover 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 (toRemove(invocation) && !AmplificationChecker.isAssert(invocation) && !inWhileLoop(invocation) && !containsIteratorNext(invocation)) {
                    methods.add(apply(method, invocation_index));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            invocation_index++;
        }
    }
    return methods;
}
Also used : ArrayList(java.util.ArrayList) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod)

Example 60 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class AssertGeneratorHelper method createTestWithLog.

static CtMethod<?> createTestWithLog(CtMethod test, final String filter) {
    CtMethod clone = AmplificationHelper.cloneTestMethodNoAmp(test);
    clone.setSimpleName(test.getSimpleName() + "_withlog");
    final List<CtStatement> allStatement = clone.getElements(new TypeFilter<>(CtStatement.class));
    allStatement.stream().filter(statement -> isStmtToLog(filter, statement)).forEach(statement -> addLogStmt(statement, test.getSimpleName() + "__" + indexOfByRef(allStatement, statement)));
    return clone;
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) CtTypedElement(spoon.reflect.declaration.CtTypedElement) ObjectLog(fr.inria.diversify.compare.ObjectLog) Predicate(java.util.function.Predicate) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtNamedElement(spoon.reflect.declaration.CtNamedElement) spoon.reflect.code(spoon.reflect.code) CtMethod(spoon.reflect.declaration.CtMethod) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

TypeFilter (spoon.reflect.visitor.filter.TypeFilter)132 Test (org.junit.Test)119 Launcher (spoon.Launcher)54 Factory (spoon.reflect.factory.Factory)52 CtMethod (spoon.reflect.declaration.CtMethod)43 CtInvocation (spoon.reflect.code.CtInvocation)27 CtClass (spoon.reflect.declaration.CtClass)24 CtLiteral (spoon.reflect.code.CtLiteral)18 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)18 AbstractTest (fr.inria.AbstractTest)17 List (java.util.List)17 CtTypeReference (spoon.reflect.reference.CtTypeReference)17 ArrayList (java.util.ArrayList)14 CtBlock (spoon.reflect.code.CtBlock)10 CtConstructorCall (spoon.reflect.code.CtConstructorCall)10 CtIf (spoon.reflect.code.CtIf)10 CtStatement (spoon.reflect.code.CtStatement)10 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)10 File (java.io.File)9 CtElement (spoon.reflect.declaration.CtElement)8