| 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.Modifier; |
| 25 | import com.github.javaparser.ast.body.*; |
| 26 | import com.github.javaparser.ast.expr.LambdaExpr; |
| 27 | import com.github.javaparser.ast.expr.VariableDeclarationExpr; |
| 28 | import com.github.javaparser.ast.modules.ModuleRequiresDirective; |
| 29 | import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; |
| 30 | import com.github.javaparser.ast.nodeTypes.NodeWithTokenRange; |
| 31 | import com.github.javaparser.ast.stmt.CatchClause; |
| 32 | import com.github.javaparser.ast.validator.ProblemReporter; |
| 33 | import com.github.javaparser.ast.validator.VisitorValidator; |
| 34 | import com.github.javaparser.utils.SeparatedItemStringBuilder; |
| 35 | |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static com.github.javaparser.ast.Modifier.Keyword.*; |
| 40 | import static java.util.Arrays.asList; |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Verifies that only allowed modifiers are used where modifiers are expected. |
| 45 | */ |
| 46 | public class ModifierValidator extends VisitorValidator { |
| 47 | private final Modifier.Keyword[] interfaceWithNothingSpecial = new Modifier.Keyword[]{PUBLIC, PROTECTED, ABSTRACT, FINAL, SYNCHRONIZED, NATIVE, STRICTFP}; |
| 48 | private final Modifier.Keyword[] interfaceWithStaticAndDefault = new Modifier.Keyword[]{PUBLIC, PROTECTED, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP, DEFAULT}; |
| 49 | private final Modifier.Keyword[] interfaceWithStaticAndDefaultAndPrivate = new Modifier.Keyword[]{PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP, DEFAULT}; |
| 50 | |
| 51 | private final boolean hasStrictfp; |
| 52 | private final boolean hasDefaultAndStaticInterfaceMethods; |
| 53 | private final boolean hasPrivateInterfaceMethods; |
| 54 | |
| 55 | public ModifierValidator(boolean hasStrictfp, boolean hasDefaultAndStaticInterfaceMethods, boolean hasPrivateInterfaceMethods) { |
| 56 | this.hasStrictfp = hasStrictfp; |
| 57 | this.hasDefaultAndStaticInterfaceMethods = hasDefaultAndStaticInterfaceMethods; |
| 58 | this.hasPrivateInterfaceMethods = hasPrivateInterfaceMethods; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void visit(ClassOrInterfaceDeclaration n, ProblemReporter reporter) { |
| 63 | if (n.isInterface()) { |
| 64 | validateInterfaceModifiers(n, reporter); |
| 65 | } else { |
| 66 | validateClassModifiers(n, reporter); |
| 67 | } |
| 68 | super.visit(n, reporter); |
| 69 | } |
| 70 | |
| 71 | private void validateClassModifiers(ClassOrInterfaceDeclaration n, ProblemReporter reporter) { |
| 72 | if (n.isTopLevelType()) { |
| 73 | validateModifiers(n, reporter, PUBLIC, ABSTRACT, FINAL, STRICTFP); |
| 74 | } else if (n.isNestedType()) { |
| 75 | validateModifiers(n, reporter, PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, STRICTFP); |
| 76 | } else if (n.isLocalClassDeclaration()) { |
| 77 | validateModifiers(n, reporter, ABSTRACT, FINAL, STRICTFP); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private void validateInterfaceModifiers(TypeDeclaration<?> n, ProblemReporter reporter) { |
| 82 | if (n.isTopLevelType()) { |
| 83 | validateModifiers(n, reporter, PUBLIC, ABSTRACT, STRICTFP); |
| 84 | } else if (n.isNestedType()) { |
| 85 | validateModifiers(n, reporter, PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, STRICTFP); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void visit(EnumDeclaration n, ProblemReporter reporter) { |
| 91 | if (n.isTopLevelType()) { |
| 92 | validateModifiers(n, reporter, PUBLIC, STRICTFP); |
| 93 | } else if (n.isNestedType()) { |
| 94 | validateModifiers(n, reporter, PUBLIC, PROTECTED, PRIVATE, STATIC, STRICTFP); |
| 95 | } |
| 96 | super.visit(n, reporter); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void visit(AnnotationDeclaration n, ProblemReporter reporter) { |
| 101 | validateInterfaceModifiers(n, reporter); |
| 102 | super.visit(n, reporter); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void visit(AnnotationMemberDeclaration n, ProblemReporter reporter) { |
| 107 | validateModifiers(n, reporter, PUBLIC, ABSTRACT); |
| 108 | super.visit(n, reporter); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void visit(ConstructorDeclaration n, ProblemReporter reporter) { |
| 113 | validateModifiers(n, reporter, PUBLIC, PROTECTED, PRIVATE); |
| 114 | n.getParameters().forEach(p -> validateModifiers(p, reporter, FINAL)); |
| 115 | super.visit(n, reporter); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void visit(FieldDeclaration n, ProblemReporter reporter) { |
| 120 | validateModifiers(n, reporter, PUBLIC, PROTECTED, PRIVATE, STATIC, FINAL, TRANSIENT, VOLATILE); |
| 121 | super.visit(n, reporter); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void visit(MethodDeclaration n, ProblemReporter reporter) { |
| 126 | if (n.isAbstract()) { |
| 127 | final SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("Cannot be 'abstract' and also '", "', '", "'."); |
| 128 | for (Modifier.Keyword m : asList(PRIVATE, STATIC, FINAL, NATIVE, STRICTFP, SYNCHRONIZED)) { |
| 129 | if (n.hasModifier(m)) { |
| 130 | builder.append(m.asString()); |
| 131 | } |
| 132 | } |
| 133 | if (builder.hasItems()) { |
| 134 | reporter.report(n, builder.toString()); |
| 135 | } |
| 136 | } |
| 137 | if (n.getParentNode().isPresent()) { |
| 138 | if (n.getParentNode().get() instanceof ClassOrInterfaceDeclaration) { |
| 139 | if (((ClassOrInterfaceDeclaration) n.getParentNode().get()).isInterface()) { |
| 140 | if (hasDefaultAndStaticInterfaceMethods) { |
| 141 | if (hasPrivateInterfaceMethods) { |
| 142 | validateModifiers(n, reporter, interfaceWithStaticAndDefaultAndPrivate); |
| 143 | } else { |
| 144 | validateModifiers(n, reporter, interfaceWithStaticAndDefault); |
| 145 | } |
| 146 | } else { |
| 147 | validateModifiers(n, reporter, interfaceWithNothingSpecial); |
| 148 | } |
| 149 | } else { |
| 150 | validateModifiers(n, reporter, PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | n.getParameters().forEach(p -> validateModifiers(p, reporter, FINAL)); |
| 155 | super.visit(n, reporter); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void visit(LambdaExpr n, ProblemReporter reporter) { |
| 160 | n.getParameters().forEach(p -> { |
| 161 | // Final is not allowed on inferred parameters, but those get caught by the parser. |
| 162 | validateModifiers(p, reporter, FINAL); |
| 163 | }); |
| 164 | super.visit(n, reporter); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void visit(CatchClause n, ProblemReporter reporter) { |
| 169 | validateModifiers(n.getParameter(), reporter, FINAL); |
| 170 | super.visit(n, reporter); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void visit(VariableDeclarationExpr n, ProblemReporter reporter) { |
| 175 | validateModifiers(n, reporter, FINAL); |
| 176 | super.visit(n, reporter); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void visit(ModuleRequiresDirective n, ProblemReporter reporter) { |
| 181 | validateModifiers(n, reporter, TRANSITIVE, STATIC); |
| 182 | super.visit(n, reporter); |
| 183 | } |
| 184 | |
| 185 | private <T extends NodeWithModifiers<?> & NodeWithTokenRange<?>> void validateModifiers(T n, ProblemReporter reporter, Modifier.Keyword... allowedModifiers) { |
| 186 | validateAtMostOneOf(n, reporter, PUBLIC, PROTECTED, PRIVATE); |
| 187 | validateAtMostOneOf(n, reporter, FINAL, ABSTRACT); |
| 188 | if (hasStrictfp) { |
| 189 | validateAtMostOneOf(n, reporter, NATIVE, STRICTFP); |
| 190 | } else { |
| 191 | allowedModifiers = removeModifierFromArray(STRICTFP, allowedModifiers); |
| 192 | } |
| 193 | for (Modifier m : n.getModifiers()) { |
| 194 | if (!arrayContains(allowedModifiers, m.getKeyword())) { |
| 195 | reporter.report(n, "'%s' is not allowed here.", m.getKeyword().asString()); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private Modifier.Keyword[] removeModifierFromArray(Modifier.Keyword m, Modifier.Keyword[] allowedModifiers) { |
| 201 | final List<Modifier.Keyword> newModifiers = new ArrayList<>(asList(allowedModifiers)); |
| 202 | newModifiers.remove(m); |
| 203 | allowedModifiers = newModifiers.toArray(new Modifier.Keyword[0]); |
| 204 | return allowedModifiers; |
| 205 | } |
| 206 | |
| 207 | private boolean arrayContains(Object[] items, Object searchItem) { |
| 208 | for (Object o : items) { |
| 209 | if (o == searchItem) { |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | private <T extends NodeWithModifiers<?> & NodeWithTokenRange<?>> void validateAtMostOneOf(T t, ProblemReporter reporter, Modifier.Keyword... modifiers) { |
| 217 | List<Modifier.Keyword> foundModifiers = new ArrayList<>(); |
| 218 | for (Modifier.Keyword m : modifiers) { |
| 219 | if (t.hasModifier(m)) { |
| 220 | foundModifiers.add(m); |
| 221 | } |
| 222 | } |
| 223 | if (foundModifiers.size() > 1) { |
| 224 | SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("Can have only one of '", "', '", "'."); |
| 225 | for (Modifier.Keyword m : foundModifiers) { |
| 226 | builder.append(m.asString()); |
| 227 | } |
| 228 | reporter.report(t, builder.toString()); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | } |
| 233 |
Members