Search in sources :

Example 11 with ExpressionStmt

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);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 12 with ExpressionStmt

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.");
                        }
                    });
                }
            });
        });
    });
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Node(com.github.javaparser.ast.Node) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ForeachStmt(com.github.javaparser.ast.stmt.ForeachStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) ForStmt(com.github.javaparser.ast.stmt.ForStmt) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr)

Example 13 with ExpressionStmt

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());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Test(org.junit.Test)

Example 14 with ExpressionStmt

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 15 with ExpressionStmt

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Aggregations

ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)27 Test (org.junit.Test)19 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)11 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)11 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)10 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)8 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)8 Expression (com.github.javaparser.ast.expr.Expression)7 Statement (com.github.javaparser.ast.stmt.Statement)6 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)5 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)5 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)5 Parameter (com.github.javaparser.ast.body.Parameter)3 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)3 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)3 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)3 Then (org.jbehave.core.annotations.Then)3 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)2