From c740a0039281cde6312c5f3cce38bb12cf1f878b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 3 Oct 2022 12:13:21 +0100 Subject: [PATCH] Add a check that requires since on `@Deprecated` Closes gh-343 --- .../checkstyle/SpringConfigurationLoader.java | 1 + .../check/SpringDeprecatedCheck.java | 78 +++++++++++++++++++ .../checkstyle/check/messages.properties | 2 + .../resources/check/DeprecatedBadCase.txt | 6 ++ .../test/resources/check/DeprecatedValid.txt | 1 + .../resources/config/DeprecatedBadCase.xml | 9 +++ .../test/resources/config/DeprecatedValid.xml | 9 +++ .../resources/source/DeprecatedBadCase.java | 43 ++++++++++ .../resources/source/DeprecatedValid.java | 43 ++++++++++ 9 files changed, 192 insertions(+) create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringDeprecatedCheck.java create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedBadCase.txt create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedValid.txt create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedBadCase.xml create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedValid.xml create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedBadCase.java create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedValid.java diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringConfigurationLoader.java b/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringConfigurationLoader.java index f801f130..e78bf500 100644 --- a/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringConfigurationLoader.java +++ b/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringConfigurationLoader.java @@ -49,6 +49,7 @@ class SpringConfigurationLoader { } public Collection load(PropertyResolver propertyResolver) { + System.out.println(getClass().getResource("spring-checkstyle.xml")); Configuration config = loadConfiguration(getClass().getResourceAsStream("spring-checkstyle.xml"), propertyResolver); return Arrays.stream(config.getChildren()).filter(this.moduleFactory::nonFiltered).map(this::load) diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringDeprecatedCheck.java b/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringDeprecatedCheck.java new file mode 100644 index 00000000..3e56dd2d --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringDeprecatedCheck.java @@ -0,0 +1,78 @@ +/* + * Copyright 2017-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.javaformat.checkstyle.check; + +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FullIdent; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + * Checks that {@link Deprecated @Deprecated} annotations follow Spring conventions. + * + * @author Andy Wilkinson + * @since 0.0.35 + */ +public class SpringDeprecatedCheck extends AbstractSpringCheck { + + @Override + public int[] getAcceptableTokens() { + return new int[] { TokenTypes.ANNOTATION }; + } + + @Override + public void visitToken(DetailAST ast) { + if (ast.getType() == TokenTypes.ANNOTATION) { + visitAnnotation(ast); + } + } + + private void visitAnnotation(DetailAST annotation) { + String text = FullIdent.createFullIdent(annotation.findFirstToken(TokenTypes.AT).getNextSibling()).getText(); + if ("Deprecated".equals(text) || "java.lang.Deprecated".equals(text)) { + visitDeprecated(annotation); + } + } + + private void visitDeprecated(DetailAST deprecated) { + DetailAST sinceAttribute = findSinceAttribute(deprecated); + if (sinceAttribute == null) { + log(deprecated.getLineNo(), deprecated.getColumnNo(), "deprecated.missingSince"); + } + else { + DetailAST expr = sinceAttribute.findFirstToken(TokenTypes.EXPR); + DetailAST sinceLiteral = expr.findFirstToken(TokenTypes.STRING_LITERAL); + if ("\"\"".equals(sinceLiteral.getText())) { + log(deprecated.getLineNo(), deprecated.getColumnNo(), "deprecated.emptySince"); + } + } + } + + private DetailAST findSinceAttribute(DetailAST deprecated) { + DetailAST child = deprecated.getFirstChild(); + while (child != null) { + if (child.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) { + DetailAST attributeIdent = child.findFirstToken(TokenTypes.IDENT); + if (attributeIdent != null && ("since".equals(attributeIdent.getText()))) { + return child; + } + } + child = child.getNextSibling(); + } + return null; + } + +} diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties b/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties index b32ec8a7..735d7b43 100644 --- a/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties +++ b/spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties @@ -27,3 +27,5 @@ nothis.unexpected=Reference to instance variable ''{0}'' should not use \"this.\ ternary.equalOperator=Ternary operation should use != when testing. ternary.missingParen=Ternary operation missing parentheses. Use the form \"(a != b) ? y : n\" leadingwhitespace.incorrect=Indentation should be performed with {0} only. +deprecated.missingSince=@Deprecated has no since attribute. +deprecated.emptySince=@Deprecated has an empty since attribute. diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedBadCase.txt b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedBadCase.txt new file mode 100644 index 00000000..96f86d18 --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedBadCase.txt @@ -0,0 +1,6 @@ ++DeprecatedBadCase.java:22:1: @Deprecated has no since attribute. [SpringDeprecated] ++DeprecatedBadCase.java:25:9: @Deprecated has no since attribute. [SpringDeprecated] ++DeprecatedBadCase.java:28:9: @Deprecated has an empty since attribute. [SpringDeprecated] ++DeprecatedBadCase.java:33:9: @Deprecated has no since attribute. [SpringDeprecated] ++DeprecatedBadCase.java:36:17: @Deprecated has an empty since attribute. [SpringDeprecated] ++5 errors \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedValid.txt b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedValid.txt new file mode 100644 index 00000000..69174e4c --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedValid.txt @@ -0,0 +1 @@ ++0 errors \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedBadCase.xml b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedBadCase.xml new file mode 100644 index 00000000..0a4b92ee --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedBadCase.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedValid.xml b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedValid.xml new file mode 100644 index 00000000..0a4b92ee --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedValid.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedBadCase.java b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedBadCase.java new file mode 100644 index 00000000..84292469 --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedBadCase.java @@ -0,0 +1,43 @@ +/* + * Copyright 2017-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Bad cases for use of {@link Deprecated @Deprecated}. + * + * @author Andy Wilkinson + */ +@Deprecated +public class DeprecatedBadCase { + + @java.lang.Deprecated + public static final String SOME_CONSTANT; + + @Deprecated(since = "") + public void someMethod() { + + } + + @Deprecated + private class InnerClass { + + @Deprecated(since = "") + private void someInnerMethod() { + + } + + } + +} diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedValid.java b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedValid.java new file mode 100644 index 00000000..f9cf915c --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedValid.java @@ -0,0 +1,43 @@ +/* + * Copyright 2017-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Valid cases for use of {@link Deprecated @Deprecated}. + * + * @author Andy Wilkinson + */ +@Deprecated(since = "2.0.0") +public class DeprecatedValid { + + @Deprecated(since = "1.2.0") + public static final String SOME_CONSTANT; + + @Deprecated(since = "1.3.0") + public void someMethod() { + + } + + @Deprecated(since = "1.2.0") + private class InnerClass { + + @Deprecated(since = "1.1.0") + private void someInnerMethod() { + + } + + } + +}