0

I have two situations I need a checkstyle check for. Let's say I have a bunch of objects with the annotation @BusinessLogic. I want to do a first pass through all *.java files creating a Set with the full classnames of these objects. Let's say ONE of the classes here is MyBusinessLogic. NEXT, and as part of a custom checkstyle checker, I want to go through and fail the build if there is any lines of code that say "new MyBusinessLogic()" in any of the code. We want to force DI when objects are annotated with @BusinessLogic. Is this possible with checkstyle? I am not sure checkstyle does a dual pass.

Another option I am considering is some gradle plugin perhaps that scans all java files and writes to a file the list of classes annotated with @BusinessLogic and then running checkstyle after that where my checker reads in the file?

My next situation is I have a library delivered as a jar so in that jar, I also have classes annotated with @BusinessLogic and I need to make sure those are also added to my list of classes that should not be newed up manually and only created with dependency injection.

Follow up question from the previous question here after reading through checkstyle docs:

How to enforce this pattern via gradle plugins?

thanks, Dean

1 Answer 1

2

Is it possible to write a dual pass checkstyle check?

Possible, yes, but not officially supported. Support would come at https://github.com/checkstyle/checkstyle/issues/3540 but it hasn't been agreed on.

Multi-file validation is possible with FileSets (still not officially supported), but it becomes harder with TreeWalker checks. This is because TreeWalker doesn't chain finishProcessing to the checks. You can implement your own TreeWalker that will chain this finishProcessing to implementation of AbstractChecks.

You will have to do everything in 1 pass with this method. Log all new XXX and classes with annotation @YYY. In the finishProcessing method, correlate the information obtained between the 2 and print a violation when you have a match.

I have a library delivered as a jar

Checkstyle does not support reading JARs or bytecode. You can always create a hard coded list as an alternative. The only other way is build your own reader into Checkstyle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.