-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a check that requires since on
@Deprecated
Closes gh-343
- Loading branch information
1 parent
9610342
commit c740a00
Showing
9 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringDeprecatedCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...ng-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedBadCase.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
1 change: 1 addition & 0 deletions
1
spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/DeprecatedValid.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
+0 errors |
9 changes: 9 additions & 0 deletions
9
...g-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedBadCase.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0"?> | ||
<!DOCTYPE module PUBLIC | ||
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" | ||
"https://checkstyle.org/dtds/configuration_1_3.dtd"> | ||
<module name="com.puppycrawl.tools.checkstyle.Checker"> | ||
<module name="com.puppycrawl.tools.checkstyle.TreeWalker"> | ||
<module name="io.spring.javaformat.checkstyle.check.SpringDeprecatedCheck"/> | ||
</module> | ||
</module> |
9 changes: 9 additions & 0 deletions
9
spring-javaformat/spring-javaformat-checkstyle/src/test/resources/config/DeprecatedValid.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0"?> | ||
<!DOCTYPE module PUBLIC | ||
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" | ||
"https://checkstyle.org/dtds/configuration_1_3.dtd"> | ||
<module name="com.puppycrawl.tools.checkstyle.Checker"> | ||
<module name="com.puppycrawl.tools.checkstyle.TreeWalker"> | ||
<module name="io.spring.javaformat.checkstyle.check.SpringDeprecatedCheck"/> | ||
</module> | ||
</module> |
43 changes: 43 additions & 0 deletions
43
...-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedBadCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
|
||
} | ||
|
||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...ng-javaformat/spring-javaformat-checkstyle/src/test/resources/source/DeprecatedValid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
|
||
} | ||
|
||
} | ||
|
||
} |