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 | |
22 | package com.github.javaparser.ast.validator.chunks; |
23 | |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.body.Parameter; |
26 | import com.github.javaparser.ast.body.VariableDeclarator; |
27 | import com.github.javaparser.ast.expr.ArrayInitializerExpr; |
28 | import com.github.javaparser.ast.expr.LambdaExpr; |
29 | import com.github.javaparser.ast.expr.NullLiteralExpr; |
30 | import com.github.javaparser.ast.expr.VariableDeclarationExpr; |
31 | import com.github.javaparser.ast.stmt.ExpressionStmt; |
32 | import com.github.javaparser.ast.stmt.ForStmt; |
33 | import com.github.javaparser.ast.stmt.ForEachStmt; |
34 | import com.github.javaparser.ast.stmt.TryStmt; |
35 | import com.github.javaparser.ast.type.VarType; |
36 | import com.github.javaparser.ast.validator.ProblemReporter; |
37 | import com.github.javaparser.ast.validator.TypedValidator; |
38 | |
39 | import java.util.Optional; |
40 | |
41 | public 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 node, ProblemReporter reporter) { |
50 | // All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else. |
51 | Optional<VariableDeclarator> variableDeclarator = 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(node, reporter); |
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<Node> variableDeclarationExpr = vd.getParentNode(); |
71 | if (!variableDeclarationExpr.isPresent()) { |
72 | reportIllegalPosition(node, reporter); |
73 | return; |
74 | } |
75 | variableDeclarationExpr.ifPresent(vdeNode -> { |
76 | if (!(vdeNode instanceof VariableDeclarationExpr)) { |
77 | reportIllegalPosition(node, reporter); |
78 | return; |
79 | } |
80 | VariableDeclarationExpr vde = (VariableDeclarationExpr) vdeNode; |
81 | if (vde.getVariables().size() > 1) { |
82 | reporter.report(vde, "\"var\" only takes a single variable."); |
83 | } |
84 | Optional<Node> container = vdeNode.getParentNode(); |
85 | if (!container.isPresent()) { |
86 | reportIllegalPosition(node, reporter); |
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(node, reporter); |
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 n, ProblemReporter reporter) { |
117 | reporter.report(n, "\"var\" is not allowed here."); |
118 | } |
119 | } |
120 |
Members