IllegalType Not Working For Annotation Using FQN
tntim96 opened this issue · comments
java -jar checkstyle-10.15.0-all.jar -c config.xml IllegalAnnotationClass.java
Starting audit...
Audit done.
cat config.xml
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<module name="TreeWalker">
<!--
-->
<module name="IllegalImport">
<property name="illegalPkgs" value="testing"/>
<property name="illegalClasses" value="testing.AnnotationClass"/>
</module>
<module name="IllegalType">
<property name="illegalClassNames" value="testing.AnnotationClass"/>
</module>
</module>
</module>
cat IllegalAnnotationClass.java
public class IllegalAnnotationClass {
@testing.AnnotationClass
public void foo() {}
}
cat testing/AnnotationClass.java
package testing;
public @interface AnnotationClass {}
RUN_LOCALE="-Duser.language=en -Duser.country=US"
java $RUN_LOCALE -jar checkstyle-10.15.0-all.jar -c config.xml IllegalAnnotationClass.java
Starting audit...
Audit done.
Expected output
Starting audit...
[ERROR] /var/tmp/checkstyle-bug/IllegalAnnotationClass.java:3:4: Usage of type 'testing.AnnotationClass' is not allowed. [IllegalType]
Audit done.
Checkstyle ends with 1 errors.
If you followed issue template , you might look at doc https://checkstyle.org/checks/coding/illegaltype.html#IllegalType
For additional restriction of type usage see also: IllegalInstantiation, IllegalImport
But I agree some clarity would be good to check description to define its targets.
Can other Checks help you with what you want to archive? or you need exactly this Check to handle it ?
Can other Checks help you
Not as far as I can tell. I think IllegalType
is the most appropriate place for this check, however I noticed the documentation says:
Specify classes that should not be used as types in variable declarations, return values or parameters.
...so strictly speaking, perhaps this is a feature request to expand the scope to include annotations.
It might be related problem #6477