Search in sources :

Example 21 with Statement

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

the class ParsingSteps method thenLambdaInStatementInMethodInClassBlockStatement.

@Then("lambda in statement $statementPosition in method $methodPosition in class $classPosition block statement is \"$expectedBody\"")
public void thenLambdaInStatementInMethodInClassBlockStatement(int statementPosition, int methodPosition, int classPosition, String expectedBody) {
    LambdaExpr lambdaExpr = getLambdaExprInStatementInMethodInClass(statementPosition, methodPosition, classPosition);
    BlockStmt blockStmt = lambdaExpr.getBody().asBlockStmt();
    Statement lambdaStmt = blockStmt.getStatement(0);
    assertThat(lambdaStmt.toString(), is(expectedBody));
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Then(org.jbehave.core.annotations.Then)

Example 22 with Statement

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

the class NodeRemovalTest method testRemoveStatementFromMethodBody.

@Test
public void testRemoveStatementFromMethodBody() {
    ClassOrInterfaceDeclaration testClass = cu.addClass("testC");
    MethodDeclaration addMethod = testClass.addMethod("testM");
    BlockStmt methodBody = addMethod.createBody();
    Statement addStatement = methodBody.addAndGetStatement("test");
    assertEquals(1, methodBody.getStatements().size());
    boolean remove = addStatement.remove();
    assertEquals(true, remove);
    assertEquals(0, methodBody.getStatements().size());
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Test(org.junit.Test)

Example 23 with Statement

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

the class LexicalDifferenceCalculatorTest method csmModelAfterAddingStatementToEmptyBlock.

@Test
public void csmModelAfterAddingStatementToEmptyBlock() 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));
    LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = ldc.calculatedSyntaxModelAfterListAddition(ConcreteSyntaxModel.forClass(BlockStmt.class), ObservableProperty.STATEMENTS, setter.getBody().get().getStatements(), 0, assignStatement);
    int index = 0;
    assertEquals(CsmElement.token(GeneratedJavaParserConstants.LBRACE), calculatedSyntaxModel.elements.get(index++));
    assertEquals(CsmElement.newline(), calculatedSyntaxModel.elements.get(index++));
    assertEquals(CsmElement.indent(), calculatedSyntaxModel.elements.get(index++));
    assertTrue(isChild(calculatedSyntaxModel.elements.get(index++), ExpressionStmt.class));
    assertEquals(CsmElement.newline(), calculatedSyntaxModel.elements.get(index++));
    assertEquals(CsmElement.unindent(), calculatedSyntaxModel.elements.get(index++));
    assertEquals(CsmElement.token(GeneratedJavaParserConstants.RBRACE), calculatedSyntaxModel.elements.get(index++));
    assertEquals(index, calculatedSyntaxModel.elements.size());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) 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