JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/validator/chunks/VarValidator.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.ast.validator.chunks;
23
24import com.github.javaparser.ast.Node;
25import com.github.javaparser.ast.body.Parameter;
26import com.github.javaparser.ast.body.VariableDeclarator;
27import com.github.javaparser.ast.expr.ArrayInitializerExpr;
28import com.github.javaparser.ast.expr.LambdaExpr;
29import com.github.javaparser.ast.expr.NullLiteralExpr;
30import com.github.javaparser.ast.expr.VariableDeclarationExpr;
31import com.github.javaparser.ast.stmt.ExpressionStmt;
32import com.github.javaparser.ast.stmt.ForStmt;
33import com.github.javaparser.ast.stmt.ForEachStmt;
34import com.github.javaparser.ast.stmt.TryStmt;
35import com.github.javaparser.ast.type.VarType;
36import com.github.javaparser.ast.validator.ProblemReporter;
37import com.github.javaparser.ast.validator.TypedValidator;
38
39import java.util.Optional;
40
41public class VarValidator implements TypedValidator<VarType> {
42    private boolean varAllowedInLambdaParameters;
43
44    public VarValidator(boolean varAllowedInLambdaParameters) {
45        this.varAllowedInLambdaParameters = varAllowedInLambdaParameters;
46    }
47
48    @Override
49    public void accept(VarType nodeProblemReporter reporter) {
50        // All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
51        Optional<VariableDeclaratorvariableDeclarator = node.findAncestor(VariableDeclarator.class);
52        if (!variableDeclarator.isPresent()) {
53            // Java 11's var in lambda's
54            if (varAllowedInLambdaParameters) {
55                boolean valid = node
56                        .findAncestor(Parameter.class)
57                        .flatMap(Node::getParentNode)
58                        .map((Node p) -> p instanceof LambdaExpr).orElse(false);
59                if (valid) {
60                    return;
61                }
62            }
63            reportIllegalPosition(nodereporter);
64            return;
65        }
66        variableDeclarator.ifPresent(vd -> {
67            if (vd.getType().isArrayType()) {
68                reporter.report(vd"\"var\" cannot have extra array brackets.");
69            }
70            Optional<NodevariableDeclarationExpr = vd.getParentNode();
71            if (!variableDeclarationExpr.isPresent()) {
72                reportIllegalPosition(nodereporter);
73                return;
74            }
75            variableDeclarationExpr.ifPresent(vdeNode -> {
76                if (!(vdeNode instanceof VariableDeclarationExpr)) {
77                    reportIllegalPosition(nodereporter);
78                    return;
79                }
80                VariableDeclarationExpr vde = (VariableDeclarationExprvdeNode;
81                if (vde.getVariables().size() > 1) {
82                    reporter.report(vde"\"var\" only takes a single variable.");
83                }
84                Optional<Nodecontainer = vdeNode.getParentNode();
85                if (!container.isPresent()) {
86                    reportIllegalPosition(nodereporter);
87                    return;
88                }
89                container.ifPresent(c -> {
90                    boolean positionIsFine = c instanceof ForStmt || c instanceof ForEachStmt ||
91                            c instanceof ExpressionStmt || c instanceof TryStmt;
92                    if (!positionIsFine) {
93                        reportIllegalPosition(nodereporter);
94                    }
95                    // A local variable declaration ends up inside an ExpressionStmt.
96                    if (c instanceof ExpressionStmt) {
97                        if (!vd.getInitializer().isPresent()) {
98                            reporter.report(node"\"var\" needs an initializer.");
99                        }
100                        vd.getInitializer().ifPresent(initializer -> {
101                            if (initializer instanceof NullLiteralExpr) {
102                                reporter.report(node"\"var\" cannot infer type from just null.");
103                            }
104                            if (initializer instanceof ArrayInitializerExpr) {
105                                reporter.report(node"\"var\" cannot infer array types.");
106                            }
107                        });
108
109                    }
110                });
111            });
112        });
113
114    }
115
116    private void reportIllegalPosition(VarType nProblemReporter reporter) {
117        reporter.report(n"\"var\" is not allowed here.");
118    }
119}
120
MembersX
VarValidator:accept:Block:Block:Block:container
VarValidator:accept
VarValidator:accept:Block:Block:variableDeclarationExpr
VarValidator:varAllowedInLambdaParameters
VarValidator:accept:Block:Block:Block:vde
VarValidator:VarValidator
VarValidator:accept:Block:variableDeclarator
VarValidator:accept:Block:Block:Block:valid
VarValidator:accept:Block:Block:Block:Block:positionIsFine
VarValidator:reportIllegalPosition
Members
X