Search in sources :

Example 1 with ArrayCreationExpr

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

the class ArrayCreationLevelTransformationsTest method consider.

protected ArrayCreationLevel consider(String code) {
    considerExpression("new int" + code);
    ArrayCreationExpr arrayCreationExpr = expression.asArrayCreationExpr();
    return arrayCreationExpr.getLevels().get(0);
}
Also used : ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr)

Example 2 with ArrayCreationExpr

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

the class JavaParserTest method rangeOfArrayCreationLevelWithoutExpressionIsCorrect.

@Test
public void rangeOfArrayCreationLevelWithoutExpressionIsCorrect() {
    String code = "new int[][]";
    ArrayCreationExpr expression = JavaParser.parseExpression(code);
    Optional<Range> range;
    range = expression.getLevels().get(0).getRange();
    assertEquals(true, range.isPresent());
    assertEquals(new Range(new Position(1, 8), new Position(1, 9)), range.get());
    range = expression.getLevels().get(1).getRange();
    assertEquals(true, range.isPresent());
    assertEquals(new Range(new Position(1, 10), new Position(1, 11)), range.get());
}
Also used : ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) Test(org.junit.Test)

Example 3 with ArrayCreationExpr

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

the class JavaParserTest method rangeOfArrayCreationLevelWithExpressionIsCorrect.

@Test
public void rangeOfArrayCreationLevelWithExpressionIsCorrect() {
    String code = "new int[123][456]";
    ArrayCreationExpr expression = JavaParser.parseExpression(code);
    Optional<Range> range;
    range = expression.getLevels().get(0).getRange();
    assertEquals(true, range.isPresent());
    assertEquals(new Range(new Position(1, 8), new Position(1, 12)), range.get());
    range = expression.getLevels().get(1).getRange();
    assertEquals(true, range.isPresent());
    assertEquals(new Range(new Position(1, 13), new Position(1, 17)), range.get());
}
Also used : ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) Test(org.junit.Test)

Example 4 with ArrayCreationExpr

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

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

the class ArrayTypeTest method getArrayCreationType.

@Test
public void getArrayCreationType() {
    ArrayCreationExpr expr = parseExpression("new int[]");
    ArrayType outerType = expr.createdType().asArrayType();
    Type innerType = outerType.getComponentType();
    assertThat(innerType).isEqualTo(expr.getElementType());
}
Also used : ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) Test(org.junit.Test)

Aggregations

ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)12 Expression (com.github.javaparser.ast.expr.Expression)5 NodeList (com.github.javaparser.ast.NodeList)4 ArrayInitializerExpr (com.github.javaparser.ast.expr.ArrayInitializerExpr)4 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 Test (org.junit.Test)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)3 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)3 List (java.util.List)3 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)2 Node (com.github.javaparser.ast.Node)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 ArrayAccessExpr (com.github.javaparser.ast.expr.ArrayAccessExpr)2 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)2