Search in sources :

Example 11 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 DoStmt n, final A arg) {
    visitComment(n, arg);
    final Statement body = (Statement) n.getBody().accept(this, arg);
    if (body == null) {
        return null;
    }
    n.setBody(body);
    final Expression condition = (Expression) n.getCondition().accept(this, arg);
    if (condition == null) {
        return null;
    }
    n.setCondition(condition);
    return n;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Statement(com.github.javaparser.ast.stmt.Statement)

Example 12 with Statement

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

the class DumpVisitor method visit.

@Override
public void visit(final BlockStmt n, final Object arg) {
    printOrphanCommentsBeforeThisChildNode(n);
    printJavaComment(n.getComment(), arg);
    printer.printLn("{");
    if (n.getStmts() != null) {
        printer.indent();
        for (final Statement s : n.getStmts()) {
            s.accept(this, arg);
            printer.printLn();
        }
        printer.unindent();
    }
    printOrphanCommentsEnding(n);
    printer.print("}");
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement)

Example 13 with Statement

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

the class DumpVisitor method visit.

@Override
public void visit(final SwitchEntryStmt n, final Object arg) {
    printJavaComment(n.getComment(), arg);
    if (n.getLabel() != null) {
        printer.print("case ");
        n.getLabel().accept(this, arg);
        printer.print(":");
    } else {
        printer.print("default:");
    }
    printer.printLn();
    printer.indent();
    if (n.getStmts() != null) {
        for (final Statement s : n.getStmts()) {
            s.accept(this, arg);
            printer.printLn();
        }
    }
    printer.unindent();
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement)

Example 14 with Statement

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

the class JavaParser method parseStatement.

/**
 * Parses the Java statement contained in a {@link String} and returns a
 * {@link Statement} that represents it.
 *
 * @param statement
 *            {@link String} containing Java statement code
 * @return Statement representing the Java statement
 * @throws ParseException
 *             if the source code has parser errors
 */
public static Statement parseStatement(final String statement) {
    StringReader sr = new StringReader(statement);
    Statement stmt = new ASTParser(sr).Statement();
    sr.close();
    return stmt;
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) StringReader(java.io.StringReader) ASTParser(com.github.javaparser.ASTParser)

Example 15 with Statement

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

the class SwitchEntryContext method solveSymbol.

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
    SwitchStmt switchStmt = (SwitchStmt) requireParentNode(wrappedNode);
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(switchStmt.getSelector());
    if (type.isReferenceType() && type.asReferenceType().getTypeDeclaration().isEnum()) {
        if (type instanceof ReferenceTypeImpl) {
            ReferenceTypeImpl typeUsageOfTypeDeclaration = (ReferenceTypeImpl) type;
            if (typeUsageOfTypeDeclaration.getTypeDeclaration().hasField(name)) {
                return SymbolReference.solved(typeUsageOfTypeDeclaration.getTypeDeclaration().getField(name));
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }
    // look for declaration in other switch statements
    for (SwitchEntryStmt seStmt : switchStmt.getEntries()) {
        if (!seStmt.equals(wrappedNode)) {
            for (Statement stmt : seStmt.getStatements()) {
                SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(stmt, typeSolver);
                SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
                if (symbolReference.isSolved()) {
                    return symbolReference;
                }
            }
        }
    }
    return getParent().solveSymbol(name, typeSolver);
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) Statement(com.github.javaparser.ast.stmt.Statement) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

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