|
| 1 | +/* |
| 2 | + * Copyright 2017-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.javaformat.checkstyle.check; |
| 18 | + |
| 19 | +import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 20 | +import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 21 | + |
| 22 | +/** |
| 23 | + * Checks that protected, package-private and private classes to not have public methods |
| 24 | + * unless they are also annotated with {@link Override @Override}. |
| 25 | + * |
| 26 | + * @author Phillip Webb |
| 27 | + */ |
| 28 | +public class SpringMethodVisibilityCheck extends AbstractSpringCheck { |
| 29 | + |
| 30 | + @Override |
| 31 | + public int[] getAcceptableTokens() { |
| 32 | + return new int[] { TokenTypes.METHOD_DEF }; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void visitToken(DetailAST ast) { |
| 37 | + DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS); |
| 38 | + if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null) { |
| 39 | + visitPublicMethod(modifiers, ast); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private void visitPublicMethod(DetailAST modifiers, DetailAST method) { |
| 44 | + if (hasOverrideAnnotation(modifiers)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + DetailAST classDef = getClassDef(method.getParent()); |
| 48 | + if (classDef == null) { |
| 49 | + return; |
| 50 | + } |
| 51 | + DetailAST classModifiers = classDef.findFirstToken(TokenTypes.MODIFIERS); |
| 52 | + if (classModifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null |
| 53 | + || classModifiers.findFirstToken(TokenTypes.LITERAL_PROTECTED) != null) { |
| 54 | + return; |
| 55 | + } |
| 56 | + DetailAST ident = method.findFirstToken(TokenTypes.IDENT); |
| 57 | + log(ident.getLineNo(), ident.getColumnNo(), "methodvisibility.publicMethod", ident.getText()); |
| 58 | + } |
| 59 | + |
| 60 | + private boolean hasOverrideAnnotation(DetailAST modifiers) { |
| 61 | + DetailAST candidate = modifiers.getFirstChild(); |
| 62 | + while (candidate != null) { |
| 63 | + if (candidate.getType() == TokenTypes.ANNOTATION) { |
| 64 | + DetailAST dot = candidate.findFirstToken(TokenTypes.DOT); |
| 65 | + String name = (dot != null ? dot : candidate).findFirstToken(TokenTypes.IDENT).getText(); |
| 66 | + if ("Override".equals(name)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + candidate = candidate.getNextSibling(); |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + private DetailAST getClassDef(DetailAST ast) { |
| 76 | + while (ast != null) { |
| 77 | + if (ast.getType() == TokenTypes.CLASS_DEF) { |
| 78 | + return ast; |
| 79 | + } |
| 80 | + ast = ast.getParent(); |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments