0

There is a class abc.something.MyComponent that is used by some legacy code, but we want to avoid using it in newly created code.

I'm thinking about something like maven-enforcer-plugin with additional option to create a list of "allowed violations", because it is not possible to remove all usages instantly.

What is appropriate approach to add rules of a certain class usage in Java project?

diziaq
  • 6,881
  • 16
  • 54
  • 96
  • 3
    declare them deprecated, provide an alternative and remove it in a future version. I wouldn't, however, go and look for a way beyond that. Seems a bit overkill. Just let the developer know that using it is on his own responsibility – Stultuske Feb 02 '23 at 08:39
  • Activate the appropriate warnings via java compiler (-Xlint:all, -Xlint:-processing) and you will see all deprecations. Mark the code as deprecated as already suggested ... cleanest way... – khmarbaise Feb 02 '23 at 08:54
  • What I missed that if you mark things deprecated your IDE will give already hints... during the time you are writing the code..other solution might be an add-on but from point of view too late... – khmarbaise Feb 03 '23 at 07:46

1 Answers1

2

One way is to use Checkstyle's IllegalType check. Checkstyle can easily be integrated in a Maven build using the Maven Checkstyle plugin. It can be configured to simply create a report or to run its checks during the normal build and even fail the build if any violation is found.

Here's how the general configuration would look like:

<module name="IllegalType">
  <property name="illegalClassNames" value="abc.something.MyComponent"/>
</module>

If you want to suppress violations about specific uses in legacy code, you could further configure "suppressions", as shown here. You can use file patterns and also restrict the suppressions to specific checks.

rolve
  • 10,083
  • 4
  • 55
  • 75