Search in sources :

Example 1 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project Ex2Amplifier by STAMP-project.

the class MainGenerator method generateMainMethodFromTestMethod.

public static CtMethod<?> generateMainMethodFromTestMethod(CtMethod<?> testMethod, CtType<?> testClass) {
    final Factory factory = testMethod.getFactory();
    final CtBlock<?> blockMain = new AssertionRemover().removeAssertion(testMethod).getBody().clone();
    final List<CtLiteral<?>> originalLiterals = blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER);
    final int[] count = new int[] { 1 };
    final List<CtLocalVariable<?>> localVariables = originalLiterals.stream().map(literal -> factory.createLocalVariable(Utils.getRealTypeOfLiteral(literal), "lit" + count[0]++, factory.createCodeSnippetExpression(createMakeRead(factory, literal)))).collect(Collectors.toList());
    final CtMethod<?> mainMethod = initMainMethod(factory);
    Collections.reverse(localVariables);
    localVariables.forEach(blockMain::insertBegin);
    Collections.reverse(localVariables);
    final Iterator<CtLocalVariable<?>> iterator = localVariables.iterator();
    blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).forEach(literal -> literal.replace(factory.createVariableRead(iterator.next().getReference(), false)));
    if (!testMethod.getThrownTypes().isEmpty()) {
        final CtTry largeTryCatchBlock = createLargeTryCatchBlock(testMethod.getThrownTypes(), factory);
        largeTryCatchBlock.setBody(blockMain);
        mainMethod.setBody(largeTryCatchBlock);
    } else {
        mainMethod.setBody(blockMain);
    }
    removeNonStaticElement(mainMethod, testClass);
    return mainMethod;
}
Also used : Ex2Amplifier(eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtThrow(spoon.reflect.code.CtThrow) Function(java.util.function.Function) CtStatement(spoon.reflect.code.CtStatement) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) CtType(spoon.reflect.declaration.CtType) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Iterator(java.util.Iterator) Utils(eu.stamp.project.ex2amplifier.Utils) CtTargetedExpression(spoon.reflect.code.CtTargetedExpression) Set(java.util.Set) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) ModifierKind(spoon.reflect.declaration.ModifierKind) Optional(java.util.Optional) CtBlock(spoon.reflect.code.CtBlock) CtTry(spoon.reflect.code.CtTry) CtParameter(spoon.reflect.declaration.CtParameter) CtLiteral(spoon.reflect.code.CtLiteral) Collections(java.util.Collections) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) CtTry(spoon.reflect.code.CtTry) CtLocalVariable(spoon.reflect.code.CtLocalVariable) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) CtLiteral(spoon.reflect.code.CtLiteral)

Example 2 with CtLocalVariable

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

the class AssertionRemover method removeAssertion.

/**
 * Replaces an invocation with its arguments.
 *
 * @param invocation Invocation
 */
public void removeAssertion(CtInvocation<?> invocation) {
    final Factory factory = invocation.getFactory();
    invocation.getArguments().forEach(argument -> {
        CtExpression clone = ((CtExpression) argument).clone();
        if (clone instanceof CtUnaryOperator) {
            clone = ((CtUnaryOperator) clone).getOperand();
        }
        if (clone instanceof CtStatement) {
            clone.getTypeCasts().clear();
            invocation.insertBefore((CtStatement) clone);
        } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
            CtTypeReference<?> typeOfParameter = clone.getType();
            if (clone.getType().equals(factory.Type().NULL_TYPE)) {
                typeOfParameter = factory.Type().createReference(Object.class);
            }
            final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
            invocation.insertBefore(localVariable);
        }
    });
    CtElement currentParent = invocation;
    while (!(currentParent.getParent() instanceof CtStatementList)) {
        currentParent = currentParent.getParent();
    }
    ((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtStatementList(spoon.reflect.code.CtStatementList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtVariableRead(spoon.reflect.code.CtVariableRead)

Example 3 with CtLocalVariable

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

the class CommentTest method testBlockComment.

@Test
public void testBlockComment() {
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(BlockComment.class);
    String strType = type.toString();
    List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
    // verify that the number of comment present in the AST is correct
    assertEquals(51, comments.size());
    // verify that all comments present in the AST is printed
    for (CtComment comment : comments) {
        if (comment.getCommentType() == CtComment.CommentType.FILE) {
            // the header of the file is not printed with the toString
            continue;
        }
        assertNotNull(comment.getParent());
        assertTrue(comment.toString() + ":" + comment.getParent() + " is not printed", strType.contains(comment.toString()));
    }
    assertEquals(4, type.getComments().size());
    assertEquals(createFakeBlockComment(f, "comment class"), type.getComments().get(1));
    CtField<?> field = type.getField("field");
    assertEquals(2, field.getComments().size());
    assertEquals(createFakeBlockComment(f, "Comment Field"), field.getComments().get(0));
    assertEquals("/* Comment Field */" + newLine + "/* comment in field */" + newLine + "private int field = 10;", field.toString());
    CtAnonymousExecutable ctAnonymousExecutable = type.getAnonymousExecutables().get(0);
    assertEquals(1, ctAnonymousExecutable.getComments().size());
    assertEquals(createFakeBlockComment(f, "comment static block"), ctAnonymousExecutable.getComments().get(0));
    assertEquals(createFakeBlockComment(f, "comment inside static"), ctAnonymousExecutable.getBody().getStatement(0));
    assertEquals("/* comment static block */" + newLine + "static {" + newLine + "    /* comment inside static */" + newLine + "}", ctAnonymousExecutable.toString());
    CtConstructor constructor = type.getConstructor();
    assertEquals(1, constructor.getComments().size());
    assertEquals(createFakeBlockComment(f, "comment constructor"), constructor.getComments().get(0));
    // index 0 is the implicit super call
    assertEquals(createFakeBlockComment(f, "Comment in constructor"), constructor.getBody().getStatement(1));
    assertEquals("/* comment constructor */" + newLine + "public BlockComment() {" + newLine + "    /* Comment in constructor */" + newLine + "}", constructor.toString());
    CtMethod<Object> m = type.getMethod("m");
    assertEquals(1, m.getComments().size());
    assertEquals(createFakeBlockComment(f, "comment method"), m.getComments().get(0));
    assertEquals(createFakeBlockComment(f, "comment empty method block"), m.getBody().getStatement(0));
    assertEquals("/* comment method */" + newLine + "public void m() {" + newLine + "    /* comment empty method block */" + newLine + "}", m.toString());
    CtMethod<Object> m1 = type.getMethod("m1");
    CtSwitch ctSwitch = m1.getBody().getStatement(0);
    assertEquals(createFakeBlockComment(f, "comment switch"), ctSwitch.getComments().get(0));
    assertEquals("/* comment switch */" + newLine + "switch (1) {" + newLine + "    /* before first case */" + newLine + "    case 0 :" + newLine + "        /* comment case 0: empty case */" + newLine + "    case 1 :" + newLine + "        /* comment case 1 */" + newLine + "        int i = 0;" + newLine + "    default :" + newLine + "        /* comment default */" + newLine + "}", ctSwitch.toString());
    CtFor ctFor = m1.getBody().getStatement(1);
    assertEquals(createFakeBlockComment(f, "comment for"), ctFor.getComments().get(0));
    assertEquals("/* comment for */" + newLine + "for (int i = 0; i < 10; i++) {" + newLine + "    /* comment for block */" + newLine + "}", ctFor.toString());
    CtIf ctIf = m1.getBody().getStatement(2);
    assertEquals(createFakeBlockComment(f, "comment if"), ctIf.getComments().get(0));
    assertEquals("/* comment if */" + newLine + "if ((1 % 2) == 0) {" + newLine + "    /* comment unary operator */" + newLine + "    (field)++;" + newLine + "}", ctIf.toString());
    CtConstructorCall ctConstructorCall = m1.getBody().getStatement(3);
    assertEquals(createFakeBlockComment(f, "comment constructor call"), ctConstructorCall.getComments().get(0));
    assertEquals("/* comment constructor call */" + newLine + "new spoon.test.comment.testclasses.BlockComment()", ctConstructorCall.toString());
    CtInvocation ctInvocation = m1.getBody().getStatement(4);
    assertEquals(createFakeBlockComment(f, "comment invocation"), ctInvocation.getComments().get(0));
    assertEquals("/* comment invocation */" + newLine + "this.m()", ctInvocation.toString());
    CtLocalVariable ctLocalVariable = m1.getBody().getStatement(5);
    assertEquals(createFakeBlockComment(f, "comment local variable"), ctLocalVariable.getComments().get(0));
    assertEquals("/* comment local variable */" + newLine + "int i = 0", ctLocalVariable.toString());
    CtLocalVariable ctLocalVariable2 = m1.getBody().getStatement(6);
    assertEquals(createFakeBlockComment(f, "comment multi assignments"), ctLocalVariable2.getComments().get(0));
    assertEquals("/* comment multi assignments */" + newLine + "int j = 2", ctLocalVariable2.toString());
    CtDo ctDo = m1.getBody().getStatement(7);
    assertEquals(createFakeBlockComment(f, "comment dowhile"), ctDo.getComments().get(0));
    assertEquals("/* comment dowhile */" + newLine + "do {" + newLine + "    /* comment in do while */" + newLine + "    i++;" + newLine + "    /* comment end do while */" + newLine + "} while (i < 10 )", ctDo.toString());
    CtTry ctTry = m1.getBody().getStatement(8);
    assertEquals(createFakeBlockComment(f, "comment try"), ctTry.getComments().get(0));
    assertEquals("/* comment try */" + newLine + "try {" + newLine + "    /* comment in try */" + newLine + "    i++;" + newLine + "} catch (java.lang.Exception e) {" + newLine + "    /* comment in catch */" + newLine + "}", ctTry.toString());
    CtSynchronized ctSynchronized = m1.getBody().getStatement(9);
    assertEquals(createFakeBlockComment(f, "comment synchronized"), ctSynchronized.getComments().get(0));
    assertEquals("/* comment synchronized */" + newLine + "synchronized(this) {" + newLine + "    /* comment in synchronized */" + newLine + "}", ctSynchronized.toString());
    CtReturn ctReturn = m1.getBody().getStatement(10);
    assertEquals(createFakeBlockComment(f, "comment return"), ctReturn.getComments().get(0));
    assertEquals("/* comment return */" + newLine + "return", ctReturn.toString());
    CtMethod m2 = type.getMethodsByName("m2").get(0);
    assertEquals(6, m2.getComments().size());
    CtParameter ctParameter = (CtParameter) m2.getParameters().get(0);
    assertEquals(4, ctParameter.getComments().size());
    assertEquals("/* comment before type */" + newLine + "/* comment after parameter */" + newLine + "/* comment before throws */" + newLine + "/* comment before exception 1 */" + newLine + "/* comment before exception 2 */" + newLine + "/* comment before block */" + newLine + "public void m2(/* comment before name */" + newLine + "/* comment before parameters */" + newLine + "/* comment before type parameter */" + newLine + "/* comment before name parameter */" + newLine + "int i) throws java.lang.Error, java.lang.Exception {" + newLine + "}", m2.toString());
}
Also used : CtComment(spoon.reflect.code.CtComment) CtSwitch(spoon.reflect.code.CtSwitch) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtTry(spoon.reflect.code.CtTry) CtSynchronized(spoon.reflect.code.CtSynchronized) CtInvocation(spoon.reflect.code.CtInvocation) CtReturn(spoon.reflect.code.CtReturn) CtDo(spoon.reflect.code.CtDo) CtFor(spoon.reflect.code.CtFor) BlockComment(spoon.test.comment.testclasses.BlockComment) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) CtConstructor(spoon.reflect.declaration.CtConstructor) CtClass(spoon.reflect.declaration.CtClass) CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 4 with CtLocalVariable

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

the class CtTypeTest method testIsSubTypeOfonTypeReferences.

@Test
public void testIsSubTypeOfonTypeReferences() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-c" });
    launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
    CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
    O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
        nameToTypeRef.put(var.getSimpleName(), var.getType());
    });
    int[] count = new int[1];
    O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
        for (CtComment comment : ass.getComments()) {
            checkIsNotSubtype(comment, nameToTypeRef);
            count[0]++;
        }
        ;
        count[0]++;
        checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
    });
    assertTrue(count[0] > (9 * 8));
}
Also used : CtComment(spoon.reflect.code.CtComment) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtAssignment(spoon.reflect.code.CtAssignment) HashMap(java.util.HashMap) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 5 with CtLocalVariable

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

the class EqualTest method testEqualsActualTypeRef.

@Test
public void testEqualsActualTypeRef() throws Exception {
    // contract: actual type refs are part of the identity
    Factory factory = new Launcher().createFactory();
    CtLocalVariable var = factory.Code().createCodeSnippetStatement("java.util.List<String> l ").compile();
    CtLocalVariable var2 = factory.Code().createCodeSnippetStatement("java.util.List<Object> l ").compile();
    assertNotEquals(var2, var);
}
Also used : Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Aggregations

CtLocalVariable (spoon.reflect.code.CtLocalVariable)29 Test (org.junit.Test)21 Factory (spoon.reflect.factory.Factory)15 Launcher (spoon.Launcher)10 CtParameter (spoon.reflect.declaration.CtParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)7 CtStatement (spoon.reflect.code.CtStatement)6 CtClass (spoon.reflect.declaration.CtClass)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtElement (spoon.reflect.declaration.CtElement)5 CtLocalVariableReference (spoon.reflect.reference.CtLocalVariableReference)5 List (java.util.List)4 CtComment (spoon.reflect.code.CtComment)4 CtTry (spoon.reflect.code.CtTry)4 CtField (spoon.reflect.declaration.CtField)4 CtType (spoon.reflect.declaration.CtType)4 CtAssignment (spoon.reflect.code.CtAssignment)3 CtCatchVariable (spoon.reflect.code.CtCatchVariable)3 CtExpression (spoon.reflect.code.CtExpression)3