Search in sources :

Example 1 with CtExpression

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

the class ParentExiter method visitCtCase.

@Override
public <E> void visitCtCase(CtCase<E> caseStatement) {
    final ASTNode node = jdtTreeBuilder.getContextBuilder().stack.peek().node;
    if (node instanceof CaseStatement && ((CaseStatement) node).constantExpression != null && caseStatement.getCaseExpression() == null && child instanceof CtExpression) {
        caseStatement.setCaseExpression((CtExpression<E>) child);
        return;
    } else if (child instanceof CtStatement) {
        caseStatement.addStatement((CtStatement) child);
        return;
    }
    super.visitCtCase(caseStatement);
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode)

Example 2 with CtExpression

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

the class ParentExiter method visitCtBinaryOperator.

@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
    if (child instanceof CtExpression) {
        if (operator.getLeftHandOperand() == null) {
            operator.setLeftHandOperand((CtExpression<?>) child);
            return;
        } else if (operator.getRightHandOperand() == null) {
            operator.setRightHandOperand((CtExpression<?>) child);
            return;
        } else if (jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof StringLiteralConcatenation) {
            CtBinaryOperator<?> op = operator.getFactory().Core().createBinaryOperator();
            op.setKind(BinaryOperatorKind.PLUS);
            op.setLeftHandOperand(operator.getRightHandOperand());
            op.setRightHandOperand((CtExpression<?>) child);
            operator.setRightHandOperand(op);
            int[] lineSeparatorPositions = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult.lineSeparatorPositions;
            SourcePosition leftPosition = op.getLeftHandOperand().getPosition();
            SourcePosition rightPosition = op.getRightHandOperand().getPosition();
            op.setPosition(op.getFactory().createSourcePosition(leftPosition.getCompilationUnit(), leftPosition.getSourceStart(), rightPosition.getSourceEnd(), lineSeparatorPositions));
            return;
        }
    }
    super.visitCtBinaryOperator(operator);
}
Also used : CtExpression(spoon.reflect.code.CtExpression) SourcePosition(spoon.reflect.cu.SourcePosition) StringLiteralConcatenation(org.eclipse.jdt.internal.compiler.ast.StringLiteralConcatenation)

Example 3 with CtExpression

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

the class SnippetCompilationHelper method createWrapperContent.

private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) {
    CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME);
    CtBlock body = f.Core().createBlock();
    if (element instanceof CtStatement) {
        body.addStatement((CtStatement) element);
    } else if (element instanceof CtExpression) {
        CtReturn ret = f.Core().createReturn();
        ret.setReturnedExpression((CtExpression) element);
        body.addStatement(ret);
    }
    Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC);
    Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>();
    thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference());
    f.Method().create(w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body);
    String contents = w.toString();
    // Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly
    w.getPackage().removeType(w);
    return contents;
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) CtReturn(spoon.reflect.code.CtReturn) CtTypeReference(spoon.reflect.reference.CtTypeReference) ModifierKind(spoon.reflect.declaration.ModifierKind) HashSet(java.util.HashSet)

Example 4 with CtExpression

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

the class ContextBuilder method enter.

@SuppressWarnings("unchecked")
void enter(CtElement e, ASTNode node) {
    stack.push(new ASTPair(e, node));
    if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon.getFile().getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) {
        if (compilationunitdeclaration != null && !e.isImplicit()) {
            e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node));
        }
    }
    ASTPair pair = stack.peek();
    CtElement current = pair.element;
    if (current instanceof CtExpression) {
        while (!casts.isEmpty()) {
            ((CtExpression<?>) current).addTypeCast(casts.remove(0));
        }
    }
    if (current instanceof CtStatement && !this.label.isEmpty()) {
        ((CtStatement) current).setLabel(this.label.pop());
    }
    try {
        if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && !(e instanceof CtCatchVariable) && node instanceof Expression) {
            if (((CtTypedElement<?>) e).getType() == null) {
                ((CtTypedElement<Object>) e).setType(this.jdtTreeBuilder.getReferencesBuilder().getTypeReference(((Expression) node).resolvedType));
            }
        }
    } catch (UnsupportedOperationException ignore) {
    // For some element, we throw an UnsupportedOperationException when we call setType().
    }
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CtExpression(spoon.reflect.code.CtExpression) CtElement(spoon.reflect.declaration.CtElement) CtPackage(spoon.reflect.declaration.CtPackage) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

Example 5 with CtExpression

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

the class CommentTest method testAddCommentsToSnippet.

@Test
public void testAddCommentsToSnippet() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setCommentEnabled(true);
    CtStatement statement = factory.Code().createCodeSnippetStatement("System.out.println(\"Caenorhabditis\")");
    CtComment comment = factory.createComment("My comment on my statement", CtComment.CommentType.INLINE);
    statement.addComment(comment);
    CtExpression expression = factory.Code().createCodeSnippetExpression("\"Caenorhabditis\" + \"Caenorhabditis\"");
    CtComment commentExpression = factory.createComment("My comment on my expression", CtComment.CommentType.INLINE);
    expression.addComment(commentExpression);
    assertEquals("// My comment on my statement" + newLine + "System.out.println(\"Caenorhabditis\")", statement.toString());
    assertEquals("// My comment on my expression" + newLine + "\"Caenorhabditis\" + \"Caenorhabditis\"", expression.toString());
}
Also used : CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Aggregations

CtExpression (spoon.reflect.code.CtExpression)33 Factory (spoon.reflect.factory.Factory)19 Test (org.junit.Test)16 CtTypeReference (spoon.reflect.reference.CtTypeReference)11 CtStatement (spoon.reflect.code.CtStatement)10 CtInvocation (spoon.reflect.code.CtInvocation)6 CtMethod (spoon.reflect.declaration.CtMethod)6 AbstractTest (fr.inria.AbstractTest)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CtAssignment (spoon.reflect.code.CtAssignment)5 CtLiteral (spoon.reflect.code.CtLiteral)5 CtNewArray (spoon.reflect.code.CtNewArray)5 Launcher (spoon.Launcher)4 CtConstructorCall (spoon.reflect.code.CtConstructorCall)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 CtClass (spoon.reflect.declaration.CtClass)4 CtElement (spoon.reflect.declaration.CtElement)4 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4