Search in sources :

Example 1 with IfStmt

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

the class StatementContext method solveSymbolAsValue.

@Override
public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
    // if we're in a multiple Variable declaration line (for ex: double a=0, b=a;)
    SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(wrappedNode, typeSolver);
    Optional<Value> symbolReference = solveWithAsValue(symbolDeclarator, name, typeSolver);
    if (symbolReference.isPresent()) {
        return symbolReference;
    }
    // we should look in all the statements preceding, treating them as SymbolDeclarators
    if (requireParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.MethodDeclaration) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof IfStmt) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    if (!(requireParentNode(wrappedNode) instanceof NodeWithStatements)) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    NodeWithStatements<?> nodeWithStmt = (NodeWithStatements<?>) requireParentNode(wrappedNode);
    int position = -1;
    for (int i = 0; i < nodeWithStmt.getStatements().size(); i++) {
        if (nodeWithStmt.getStatements().get(i).equals(wrappedNode)) {
            position = i;
        }
    }
    if (position == -1) {
        throw new RuntimeException();
    }
    for (int i = position - 1; i >= 0; i--) {
        symbolDeclarator = JavaParserFactory.getSymbolDeclarator(nodeWithStmt.getStatements().get(i), typeSolver);
        symbolReference = solveWithAsValue(symbolDeclarator, name, typeSolver);
        if (symbolReference.isPresent()) {
            return symbolReference;
        }
    }
    // if nothing is found we should ask the parent context
    Context parentContext = getParent();
    return parentContext.solveSymbolAsValue(name, typeSolver);
}
Also used : Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) NodeWithStatements(com.github.javaparser.ast.nodeTypes.NodeWithStatements) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Value(com.github.javaparser.symbolsolver.model.resolution.Value)

Aggregations

LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)1 NodeWithStatements (com.github.javaparser.ast.nodeTypes.NodeWithStatements)1 IfStmt (com.github.javaparser.ast.stmt.IfStmt)1 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)1 Context (com.github.javaparser.symbolsolver.core.resolution.Context)1 Value (com.github.javaparser.symbolsolver.model.resolution.Value)1 SymbolDeclarator (com.github.javaparser.symbolsolver.resolution.SymbolDeclarator)1