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; |
23 | |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.NodeList; |
26 | import com.github.javaparser.ast.expr.IntegerLiteralExpr; |
27 | import com.github.javaparser.ast.expr.VariableDeclarationExpr; |
28 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
29 | import com.github.javaparser.ast.stmt.ForEachStmt; |
30 | import com.github.javaparser.ast.stmt.SwitchEntry; |
31 | import com.github.javaparser.ast.type.PrimitiveType; |
32 | import com.github.javaparser.ast.type.Type; |
33 | |
34 | import java.util.Optional; |
35 | |
36 | /** |
37 | * This validator validates according to Java 5 syntax rules. |
38 | */ |
39 | public class Java5Validator extends Java1_4Validator { |
40 | final Validator genericsWithoutDiamondOperator = new TreeVisitorValidator((node, reporter) -> { |
41 | if (node instanceof NodeWithTypeArguments) { |
42 | Optional<NodeList<Type>> typeArguments = ((NodeWithTypeArguments<? extends Node>) node).getTypeArguments(); |
43 | if (typeArguments.isPresent() && typeArguments.get().isEmpty()) { |
44 | reporter.report(node, "The diamond operator is not supported."); |
45 | } |
46 | } |
47 | }); |
48 | |
49 | protected final Validator noPrimitiveGenericArguments = new TreeVisitorValidator((node, reporter) -> { |
50 | if (node instanceof NodeWithTypeArguments) { |
51 | Optional<NodeList<Type>> typeArguments = ((NodeWithTypeArguments<? extends Node>) node).getTypeArguments(); |
52 | typeArguments.ifPresent(types -> types.forEach(ty -> { |
53 | if (ty instanceof PrimitiveType) { |
54 | reporter.report(node, "Type arguments may not be primitive."); |
55 | } |
56 | })); |
57 | } |
58 | }); |
59 | |
60 | // Enhanced for statements were introduced in Java 5. There must be exactly one declared variable, and the only |
61 | // allowed modifier is FINAL. |
62 | final Validator forEachStmt = new SingleNodeTypeValidator<>(ForEachStmt.class, (node, reporter) -> { |
63 | VariableDeclarationExpr declaration = node.getVariable(); |
64 | // assert that the variable declaration expression has exactly one variable declarator |
65 | if (declaration.getVariables().size() != 1) { |
66 | reporter.report(node, "A foreach statement's variable declaration must have exactly one variable " + |
67 | "declarator. Given: " + declaration.getVariables().size() + "."); |
68 | } |
69 | }); |
70 | |
71 | final Validator enumNotAllowed = new ReservedKeywordValidator("enum"); |
72 | |
73 | public Java5Validator() { |
74 | super(); |
75 | replace(noGenerics, genericsWithoutDiamondOperator); |
76 | add(noPrimitiveGenericArguments); |
77 | add(enumNotAllowed); |
78 | add(forEachStmt); |
79 | |
80 | // TODO validate annotations on classes, fields and methods but nowhere else |
81 | // The following is probably too simple. |
82 | remove(noAnnotations); |
83 | |
84 | remove(noEnums); |
85 | remove(noVarargs); |
86 | remove(noForEach); |
87 | remove(noStaticImports); |
88 | } |
89 | } |
90 |
Members