Search in sources :

Example 1 with Statement

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

the class ParsingSteps method getLambdaExprInStatementInMethodInClass.

private LambdaExpr getLambdaExprInStatementInMethodInClass(int statementPosition, int methodPosition, int classPosition) {
    Statement statement = getStatementInMethodInClass(statementPosition, methodPosition, classPosition);
    VariableDeclarationExpr expression = ((ExpressionStmt) statement).getExpression().asVariableDeclarationExpr();
    VariableDeclarator variableDeclarator = expression.getVariable(0);
    return (LambdaExpr) variableDeclarator.getInitializer().orElse(null);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement)

Example 2 with Statement

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

the class DumpVisitor method visit.

@Override
public void visit(LambdaExpr n, Object arg) {
    printJavaComment(n.getComment(), arg);
    List<Parameter> parameters = n.getParameters();
    boolean printPar = false;
    printPar = n.isParametersEnclosed();
    if (printPar) {
        printer.print("(");
    }
    if (parameters != null) {
        for (Iterator<Parameter> i = parameters.iterator(); i.hasNext(); ) {
            Parameter p = i.next();
            p.accept(this, arg);
            if (i.hasNext()) {
                printer.print(", ");
            }
        }
    }
    if (printPar) {
        printer.print(")");
    }
    printer.print(" -> ");
    Statement body = n.getBody();
    if (body instanceof ExpressionStmt) {
        // Print the expression directly
        ((ExpressionStmt) body).getExpression().accept(this, arg);
    } else {
        body.accept(this, arg);
    }
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) TypeParameter(com.github.javaparser.ast.TypeParameter) Parameter(com.github.javaparser.ast.body.Parameter) MultiTypeParameter(com.github.javaparser.ast.body.MultiTypeParameter) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 3 with Statement

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

the class ModifierVisitorAdapter method visit.

@Override
public Node visit(final IfStmt n, final A arg) {
    visitComment(n, arg);
    final Expression condition = (Expression) n.getCondition().accept(this, arg);
    if (condition == null) {
        return null;
    }
    n.setCondition(condition);
    final Statement thenStmt = (Statement) n.getThenStmt().accept(this, arg);
    if (thenStmt == null) {
        // then-clause.
        return null;
    }
    n.setThenStmt(thenStmt);
    if (n.getElseStmt() != null) {
        n.setElseStmt((Statement) n.getElseStmt().accept(this, arg));
    }
    return n;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Statement(com.github.javaparser.ast.stmt.Statement)

Example 4 with Statement

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

the class CloneVisitor method visit.

@Override
public Node visit(LambdaExpr _n, Object _arg) {
    List<Parameter> lambdaParameters = visit(_n.getParameters(), _arg);
    Statement body = cloneNodes(_n.getBody(), _arg);
    return new LambdaExpr(_n.getRange(), lambdaParameters, body, _n.isParametersEnclosed());
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter)

Example 5 with Statement

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

the class Issue257 method issue257.

@Test
public void issue257() throws FileNotFoundException {
    String pathToSourceFile = adaptPath("src/test/resources/issue257/A.java.txt");
    CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile));
    Statement statement = cu.getClassByName("A").get().getMethodsByName("run").get(0).getBody().get().getStatement(0);
    ExpressionStmt expressionStmt = (ExpressionStmt) statement;
    Expression expression = expressionStmt.getExpression();
    JavaParserFacade.get(typeSolver).getType(expression);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) Statement(com.github.javaparser.ast.stmt.Statement) File(java.io.File) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) 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