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);
}
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);
}
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);
}
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);
}
Aggregations