Class AddImport<P>

java.lang.Object
org.openrewrite.TreeVisitor<org.openrewrite.java.tree.J,P>

public class AddImport<P> extends JavaIsoVisitor<P>
A Java refactoring visitor that can be used to add an import (or static import) to a given compilation unit. This visitor can also be configured to only add the import if the imported class/method are referenced within the compilation unit.

The typeName must be supplied and together with the optional packageName represents the fully qualified class name.

The member is an optional method within the imported type. The staticMethod can be set to "*" to represent a static wildcard import.

The onlyIfReferenced is a flag (defaulted to true) to indicate if the import should only be added if there is a reference to the imported class/method.

  • Constructor Details

    • AddImport

      public AddImport(String type, @Nullable @Nullable String member, boolean onlyIfReferenced)
    • AddImport

      public AddImport(@Nullable @Nullable String packageName, String typeName, @Nullable @Nullable String member, @Nullable @Nullable String alias, boolean onlyIfReferenced)
  • Method Details

    • preVisit

      @Nullable public @Nullable org.openrewrite.java.tree.J preVisit(org.openrewrite.java.tree.J tree, P p)
      Overrides:
      preVisit in class org.openrewrite.TreeVisitor<org.openrewrite.java.tree.J,P>