use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class NodeWithStatements method addStatement.
public default T addStatement(int index, final Expression expr) {
Statement stmt = new ExpressionStmt(expr);
expr.setParentNode(stmt);
return addStatement(index, stmt);
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class VarValidator method accept.
@Override
public void accept(VarType node, ProblemReporter reporter) {
// All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
Optional<VariableDeclarator> variableDeclarator = node.findParent(VariableDeclarator.class);
if (!variableDeclarator.isPresent()) {
// Java 11's var in lambda's
if (varAllowedInLambdaParameters) {
boolean valid = node.findParent(Parameter.class).flatMap(Node::getParentNode).map((Node p) -> p instanceof LambdaExpr).orElse(false);
if (valid) {
return;
}
}
reportIllegalPosition(node, reporter);
return;
}
variableDeclarator.ifPresent(vd -> {
Optional<Node> variableDeclarationExpr = vd.getParentNode();
if (!variableDeclarationExpr.isPresent()) {
reportIllegalPosition(node, reporter);
return;
}
variableDeclarationExpr.ifPresent(vdeNode -> {
if (!(vdeNode instanceof VariableDeclarationExpr)) {
reportIllegalPosition(node, reporter);
return;
}
VariableDeclarationExpr vde = (VariableDeclarationExpr) vdeNode;
if (vde.getVariables().size() > 1) {
reporter.report(vde, "\"var\" only takes a single variable.");
}
Optional<Node> container = vdeNode.getParentNode();
if (!container.isPresent()) {
reportIllegalPosition(node, reporter);
return;
}
container.ifPresent(c -> {
boolean positionIsFine = c instanceof ForStmt || c instanceof ForeachStmt || c instanceof ExpressionStmt;
if (!positionIsFine) {
reportIllegalPosition(node, reporter);
}
// A local variable declaration ends up inside an ExpressionStmt.
if (c instanceof ExpressionStmt) {
if (!vd.getInitializer().isPresent()) {
reporter.report(node, "\"var\" needs an initializer.");
}
vd.getInitializer().ifPresent(initializer -> {
if (initializer instanceof NullLiteralExpr) {
reporter.report(node, "\"var\" cannot infer type from just null.");
}
if (initializer instanceof ArrayCreationExpr) {
reporter.report(node, "\"var\" cannot infer array types.");
}
});
}
});
});
});
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class ArrayTypeTest method setVariableDeclarationWithArrays.
@Test
public void setVariableDeclarationWithArrays() {
ExpressionStmt variableDeclarationStatement = parseStatement("@C int @A[] @B[] a @X[] @Y[];").asExpressionStmt();
VariableDeclarationExpr variableDeclarationExpr = variableDeclarationStatement.getExpression().asVariableDeclarationExpr();
variableDeclarationExpr.getVariable(0).setType(new ArrayType(new ArrayType(PrimitiveType.intType())));
assertEquals("@C int[][] a;", variableDeclarationStatement.toString());
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class ContextTest method resolveDeclaredFieldReference.
@Test
public void resolveDeclaredFieldReference() {
CompilationUnit cu = parseSample("ReferencesToField");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "ReferencesToField");
MethodDeclaration method1 = Navigator.demandMethod(referencesToField, "method1");
ExpressionStmt stmt = (ExpressionStmt) method1.getBody().get().getStatements().get(0);
AssignExpr assignExpr = (AssignExpr) stmt.getExpression();
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference symbolReference = symbolSolver.solveSymbol("i", assignExpr.getTarget());
assertEquals(true, symbolReference.isSolved());
assertEquals("i", symbolReference.getCorrespondingDeclaration().getName());
assertEquals(true, symbolReference.getCorrespondingDeclaration().isField());
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class ContextTest method resolveInheritedFieldReference.
@Test
public void resolveInheritedFieldReference() {
CompilationUnit cu = parseSample("ReferencesToField");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "ReferencesToFieldExtendingClass");
MethodDeclaration method1 = Navigator.demandMethod(referencesToField, "method2");
ExpressionStmt stmt = (ExpressionStmt) method1.getBody().get().getStatements().get(0);
AssignExpr assignExpr = (AssignExpr) stmt.getExpression();
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference symbolReference = symbolSolver.solveSymbol("i", assignExpr.getTarget());
assertEquals(true, symbolReference.isSolved());
assertEquals("i", symbolReference.getCorrespondingDeclaration().getName());
assertEquals(true, symbolReference.getCorrespondingDeclaration().isField());
}
Aggregations