Search in sources :

Example 6 with Statement

use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.

the class DifferenceTest method addingStatementToEmptyMethodBody.

@Test
public void addingStatementToEmptyMethodBody() {
    String code = "class A { void foo(char p1, int p2) {} }";
    considerCode(code);
    Statement s = new ExpressionStmt(new BinaryExpr(new IntegerLiteralExpr("10"), new IntegerLiteralExpr("2"), BinaryExpr.Operator.PLUS));
    MethodDeclaration m = cu.getClassByName("A").get().getMethodsByName("foo").get(0);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(m.getBody().get());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterListAddition(m.getBody().get(), ObservableProperty.STATEMENTS, 0, s);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    int i = 0;
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmIndent()), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmChild(s)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmUnindent()), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CsmIndent(com.github.javaparser.printer.concretesyntaxmodel.CsmIndent) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmUnindent(com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent) Test(org.junit.Test)

Example 7 with Statement

use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.

the class LexicalDifferenceCalculatorTest method differenceAfterddingStatementToEmptyBlock.

@Test
public void differenceAfterddingStatementToEmptyBlock() throws IOException {
    LexicalDifferenceCalculator ldc = new LexicalDifferenceCalculator();
    considerExample("ASimpleClassWithMoreFormatting_step3");
    MethodDeclaration setter = cu.getClassByName("MyRenamedClass").get().getMethodsByName("setAField").get(0);
    Statement assignStatement = new ExpressionStmt(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "aField"), new NameExpr("aField"), AssignExpr.Operator.ASSIGN));
    Difference diff = ldc.calculateListAdditionDifference(ObservableProperty.STATEMENTS, setter.getBody().get().getStatements(), 0, assignStatement);
    int index = 0;
    assertEquals(Difference.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.kept(CsmElement.newline()), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.added(CsmElement.indent()), diff.getElements().get(index++));
    assertTrue(isAddedChild(diff.getElements().get(index++), ExpressionStmt.class));
    assertEquals(Difference.DifferenceElement.added(CsmElement.newline()), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.added(CsmElement.unindent()), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(index++));
    assertEquals(index, diff.getElements().size());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Test(org.junit.Test)

Example 8 with Statement

use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.

the class NodeWithStatements method addStatement.

public default T addStatement(int index, final Expression expr) {
    Statement stmt = new ExpressionStmt(expr);
    expr.setParentNode(stmt);
    return addStatement(index, stmt);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 9 with Statement

use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.

the class ParseErrorRecoveryTest method bodystatementClosingBraceRecovery.

@Test
public void bodystatementClosingBraceRecovery() {
    MethodDeclaration cu = parser.parse(ParseStart.CLASS_BODY, provider("int x(){X X X}")).getResult().get().asMethodDeclaration();
    Statement xxx = cu.getBody().get();
    assertEquals(UNPARSABLE, xxx.getParsed());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) Test(org.junit.Test)

Example 10 with Statement

use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.

the class ParseErrorRecoveryTest method bodystatementSemicolonRecovery.

@Test
public void bodystatementSemicolonRecovery() {
    MethodDeclaration cu = parser.parse(ParseStart.CLASS_BODY, provider("int x(){X X X;}")).getResult().get().asMethodDeclaration();
    Statement xxx = cu.getBody().get().getStatements().get(0);
    assertEquals(UNPARSABLE, xxx.getParsed());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) Test(org.junit.Test)

Aggregations

Statement (com.github.javaparser.ast.stmt.Statement)23 Test (org.junit.Test)7 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)6 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)6 Then (org.jbehave.core.annotations.Then)5 Expression (com.github.javaparser.ast.expr.Expression)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 Parameter (com.github.javaparser.ast.body.Parameter)2 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 ASTParser (com.github.javaparser.ASTParser)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)1 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)1 TypeParameter (com.github.javaparser.ast.TypeParameter)1 MultiTypeParameter (com.github.javaparser.ast.body.MultiTypeParameter)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)1 SwitchEntryStmt (com.github.javaparser.ast.stmt.SwitchEntryStmt)1 SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)1