google / error-prone

Catch common Java mistakes as compile-time errors
https://errorprone.info
Apache License 2.0
6.85k stars 744 forks source link

Using class information from the whole codebase, not just "close context" #3692

Open fprochazka opened 1 year ago

fprochazka commented 1 year ago

Hi, I want to write a check that needs to know a lot of information about the codebase and knowing just the context of the current class/file is not enough... I need first to scan the whole codebase, collect some metadata and then use that metadata in the error-prone check... That's because (if I understand it correctly) the check always has only the context of at most the current file and maybe of what it directly references...

To be more specific, I want to write a check for hibernate validator, which will tell me that I'm missing @Valid on the inner structure. But to do that, I first must know if the class I'm referencing is inner structure/value object/other and based on that require the annotation. There are some very complex edge cases which force me to be able to inspect other classes and knowing just direct neighbours/parents/etc. is not enough, I need to be able to inspect this several layers (classes) deep.

The only solution I've come up with so far is to force error-prone to run twice over the whole module - the first pass would only collect metadata and the second pass would then use that to decide if the check is violated. But that's not nice at all...

I would prefer the ability to be natively available in error-prone, so I don't have to hack around it. Right now, I'm thinking that maybe it's better to abandon this idea and use, e.g. SpotBugs to write the check.

Any ideas? Thanks!

Stephan202 commented 1 year ago

Depending on what exactly needs to happen it may be possible to do what you want by inspecting referenced types (not sure, since I don't fully understand the use case), but for the general use case of making assertions about the full classpath it may be more pragmatic/feasible use ArchUnit instead.