Search in sources :

Example 1 with NodeWithStatements

use of com.github.javaparser.ast.nodeTypes.NodeWithStatements in project javaparser by javaparser.

the class StatementContext method solveInBlockAsValue.

public static Optional<Value> solveInBlockAsValue(String name, TypeSolver typeSolver, Statement stmt) {
    if (!(requireParentNode(stmt) instanceof NodeWithStatements)) {
        throw new IllegalArgumentException();
    }
    NodeWithStatements<?> blockStmt = (NodeWithStatements<?>) requireParentNode(stmt);
    int position = -1;
    for (int i = 0; i < blockStmt.getStatements().size(); i++) {
        if (blockStmt.getStatements().get(i).equals(stmt)) {
            position = i;
        }
    }
    if (position == -1) {
        throw new RuntimeException();
    }
    for (int i = position - 1; i >= 0; i--) {
        SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(blockStmt.getStatements().get(i), typeSolver);
        SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
        if (symbolReference.isSolved()) {
            return Optional.of(Value.from(symbolReference.getCorrespondingDeclaration()));
        }
    }
    // if nothing is found we should ask the parent context
    return JavaParserFactory.getContext(requireParentNode(stmt), typeSolver).solveSymbolAsValue(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) NodeWithStatements(com.github.javaparser.ast.nodeTypes.NodeWithStatements)

Example 2 with NodeWithStatements

use of com.github.javaparser.ast.nodeTypes.NodeWithStatements in project javaparser by javaparser.

the class StatementContext method solveInBlock.

public static SymbolReference<? extends ResolvedValueDeclaration> solveInBlock(String name, TypeSolver typeSolver, Statement stmt) {
    if (!(requireParentNode(stmt) instanceof NodeWithStatements)) {
        throw new IllegalArgumentException();
    }
    NodeWithStatements<?> blockStmt = (NodeWithStatements<?>) requireParentNode(stmt);
    int position = -1;
    for (int i = 0; i < blockStmt.getStatements().size(); i++) {
        if (blockStmt.getStatements().get(i).equals(stmt)) {
            position = i;
        }
    }
    if (position == -1) {
        throw new RuntimeException();
    }
    for (int i = position - 1; i >= 0; i--) {
        SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(blockStmt.getStatements().get(i), typeSolver);
        SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
        if (symbolReference.isSolved()) {
            return symbolReference;
        }
    }
    // if nothing is found we should ask the parent context
    return JavaParserFactory.getContext(requireParentNode(stmt), typeSolver).solveSymbol(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) NodeWithStatements(com.github.javaparser.ast.nodeTypes.NodeWithStatements)

Example 3 with NodeWithStatements

use of com.github.javaparser.ast.nodeTypes.NodeWithStatements 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)

Example 4 with NodeWithStatements

use of com.github.javaparser.ast.nodeTypes.NodeWithStatements in project javaparser by javaparser.

the class StatementContext method solveSymbol.

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(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);
    SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
    if (symbolReference.isSolved()) {
        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().solveSymbol(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.ConstructorDeclaration) {
        return getParent().solveSymbol(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
        return getParent().solveSymbol(name, typeSolver);
    }
    if (!(requireParentNode(wrappedNode) instanceof NodeWithStatements)) {
        return getParent().solveSymbol(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 = solveWith(symbolDeclarator, name);
        if (symbolReference.isSolved()) {
            return symbolReference;
        }
    }
    // if nothing is found we should ask the parent context
    return getParent().solveSymbol(name, typeSolver);
}
Also used : 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)

Aggregations

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