Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalType Not Working For Annotation Using FQN #14798

Open
tntim96 opened this issue Apr 16, 2024 · 3 comments
Open

IllegalType Not Working For Annotation Using FQN #14798

tntim96 opened this issue Apr 16, 2024 · 3 comments

Comments

@tntim96
Copy link

tntim96 commented Apr 16, 2024

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.
@romani
Copy link
Member

romani commented Apr 17, 2024

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 ?

@tntim96
Copy link
Author

tntim96 commented Apr 17, 2024

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.

@romani
Copy link
Member

romani commented Apr 17, 2024

It might be related problem #6477

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants