Class HideUtilityClassConstructorVisitor<P>

java.lang.Object
org.openrewrite.TreeVisitor<J,P>
org.openrewrite.java.JavaVisitor<P>
org.openrewrite.java.JavaIsoVisitor<P>
org.openrewrite.java.cleanup.HideUtilityClassConstructorVisitor<P>

@Incubating(since="7.0.0") public class HideUtilityClassConstructorVisitor<P> extends JavaIsoVisitor<P>
HideUtilityClassConstructorVisitor will perform the following operations on a Utility Class:
  • Change any Public constructors to Private
  • Change any Package-Private ("Default", no modifiers) to Private
  • If the Implicit Default Constructor is used (as in, no explicit constructors defined), add a Private constructor

HideUtilityClassConstructorVisitor will NOT perform operations on a Utility Class under these circumstances:

  • Will NOT change any Protected constructors to Private
  • HideUtilityClassConstructorVisitor will ignore classes with a Main method signature (public static void main(String[] args). This prevents HideUtilityClassConstructorVisitor from generating a Private constructor on classes which only serve as application entry points, though they are technically a Utility Class.
  • HideUtilityClassConstructorVisitor can be configured with a list of fully-qualified "ignorable Annotations" strings. These are used with AnnotationMatcher to check for the presence of annotations on the class. HideUtilityClassConstructorVisitor will ignore classes which have any of the configured Annotations. This is valuable for situations such as Lombok Utility classes, which generate Private constructors in bytecode.