Skip to content

Commit

Permalink
Add a check that requires since on @Deprecated
Browse files Browse the repository at this point in the history
Closes gh-343
  • Loading branch information
wilkinsona committed Oct 5, 2022
1 parent 9610342 commit c740a00
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SpringConfigurationLoader {
}

public Collection<FileSetCheck> 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)
Expand Down
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+0 errors
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>
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>
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() {

}

}

}
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() {

}

}

}

0 comments on commit c740a00

Please sign in to comment.