|
| 1 | +/* |
| 2 | + * Copyright 2017-2022 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.FullIdent; |
| 21 | +import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks that {@link Deprecated @Deprecated} annotations follow Spring conventions. |
| 25 | + * |
| 26 | + * @author Andy Wilkinson |
| 27 | + * @since 0.0.35 |
| 28 | + */ |
| 29 | +public class SpringDeprecatedCheck extends AbstractSpringCheck { |
| 30 | + |
| 31 | + @Override |
| 32 | + public int[] getAcceptableTokens() { |
| 33 | + return new int[] { TokenTypes.ANNOTATION }; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void visitToken(DetailAST ast) { |
| 38 | + if (ast.getType() == TokenTypes.ANNOTATION) { |
| 39 | + visitAnnotation(ast); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private void visitAnnotation(DetailAST annotation) { |
| 44 | + String text = FullIdent.createFullIdent(annotation.findFirstToken(TokenTypes.AT).getNextSibling()).getText(); |
| 45 | + if ("Deprecated".equals(text) || "java.lang.Deprecated".equals(text)) { |
| 46 | + visitDeprecated(annotation); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void visitDeprecated(DetailAST deprecated) { |
| 51 | + DetailAST sinceAttribute = findSinceAttribute(deprecated); |
| 52 | + if (sinceAttribute == null) { |
| 53 | + log(deprecated.getLineNo(), deprecated.getColumnNo(), "deprecated.missingSince"); |
| 54 | + } |
| 55 | + else { |
| 56 | + DetailAST expr = sinceAttribute.findFirstToken(TokenTypes.EXPR); |
| 57 | + DetailAST sinceLiteral = expr.findFirstToken(TokenTypes.STRING_LITERAL); |
| 58 | + if ("\"\"".equals(sinceLiteral.getText())) { |
| 59 | + log(deprecated.getLineNo(), deprecated.getColumnNo(), "deprecated.emptySince"); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private DetailAST findSinceAttribute(DetailAST deprecated) { |
| 65 | + DetailAST child = deprecated.getFirstChild(); |
| 66 | + while (child != null) { |
| 67 | + if (child.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) { |
| 68 | + DetailAST attributeIdent = child.findFirstToken(TokenTypes.IDENT); |
| 69 | + if (attributeIdent != null && ("since".equals(attributeIdent.getText()))) { |
| 70 | + return child; |
| 71 | + } |
| 72 | + } |
| 73 | + child = child.getNextSibling(); |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments