Search in sources :

Example 6 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class NodeWithVariablesTest method getElementTypeWorksForNormalVariables.

@Test
public void getElementTypeWorksForNormalVariables() {
    VariableDeclarationExpr declaration = parseVariableDeclarationExpr("int a,b");
    assertEquals(PrimitiveType.intType(), declaration.getElementType());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Test(org.junit.Test)

Example 7 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class NodeWithVariablesTest method getCommonTypeWorksForNormalVariables.

@Test
public void getCommonTypeWorksForNormalVariables() {
    VariableDeclarationExpr declaration = parseVariableDeclarationExpr("int a,b");
    assertEquals(PrimitiveType.intType(), declaration.getCommonType());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Test(org.junit.Test)

Example 8 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr 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 9 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr 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 10 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class TypeTest method primitiveTypeArgumentLenientValidator.

@Test
public void primitiveTypeArgumentLenientValidator() {
    ParserConfiguration config = new ParserConfiguration().setLanguageLevel(RAW);
    config.getPostProcessors().add(new Java5Validator() {

        {
            remove(noPrimitiveGenericArguments);
        }
    }.postProcessor());
    ParseResult<VariableDeclarationExpr> result = new JavaParser(config).parse(VARIABLE_DECLARATION_EXPR, provider("List<long> x"));
    assertTrue(result.isSuccessful());
    VariableDeclarationExpr decl = result.getResult().get();
    assertEquals("List<long>", decl.getVariable(0).getType().asString());
}
Also used : JavaParser(com.github.javaparser.JavaParser) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) JavaParser.parseVariableDeclarationExpr(com.github.javaparser.JavaParser.parseVariableDeclarationExpr) Java5Validator(com.github.javaparser.ast.validator.Java5Validator) ParserConfiguration(com.github.javaparser.ParserConfiguration) Test(org.junit.Test)

Aggregations

VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)15 Test (org.junit.Test)12 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)3 Parameter (com.github.javaparser.ast.body.Parameter)2 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 JavaParser (com.github.javaparser.JavaParser)1 JavaParser.parseVariableDeclarationExpr (com.github.javaparser.JavaParser.parseVariableDeclarationExpr)1 ParserConfiguration (com.github.javaparser.ParserConfiguration)1 Node (com.github.javaparser.ast.Node)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclaratorId (com.github.javaparser.ast.body.VariableDeclaratorId)1 ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 ForStmt (com.github.javaparser.ast.stmt.ForStmt)1 ForeachStmt (com.github.javaparser.ast.stmt.ForeachStmt)1 Java5Validator (com.github.javaparser.ast.validator.Java5Validator)1 ResolvedArrayType (com.github.javaparser.resolution.types.ResolvedArrayType)1 ResolvedPrimitiveType (com.github.javaparser.resolution.types.ResolvedPrimitiveType)1