Class ImportLayoutStyle

java.lang.Object
org.openrewrite.java.style.ImportLayoutStyle
All Implemented Interfaces:
JavaStyle, org.openrewrite.style.Style

public class ImportLayoutStyle extends Object implements JavaStyle
A Java Style to define how imports are grouped and ordered. Additionally, this style provides configuration to dictate how wildcard folding should be applied when multiple imports are in the same package or on the same, statically-imported type.

The import layout consist of three properties:

  • classCountToUseStarImport - How many imports from the same package must be present before they should be collapsed into a star import. The default is 5.
  • nameCountToUseStarImport - How many static imports from the same type must be present before they should be collapsed into a static star import. The default is 3.
  • layout - An ordered list of import groupings which define exactly how imports should be organized within a compilation unit.
  • packagesToFold - An ordered list of packages which are folded when 1 or more types are in use.
    • Constructor Details

    • Method Details

      • addImport

        public List<JRightPadded<org.openrewrite.java.tree.J.Import>> addImport(List<JRightPadded<org.openrewrite.java.tree.J.Import>> originalImports, org.openrewrite.java.tree.J.Import toAdd, @Nullable org.openrewrite.java.tree.J.Package pkg, Collection<JavaType.FullyQualified> classpath)
        Adds a new import in a block that best represents the import layout style without re-ordering any of the existing imports, i.e. a minimally invasive add.
        Parameters:
        originalImports - The import list before inserting.
        toAdd - The import to add.
        pkg - A package declaration, if one exists.
        Returns:
        The import list with a new import added.
      • orderImports

        public List<JRightPadded<org.openrewrite.java.tree.J.Import>> orderImports(List<JRightPadded<org.openrewrite.java.tree.J.Import>> originalImports, Collection<JavaType.FullyQualified> classpath)
        This method will order and group a list of imports producing a new list that conforms to the rules defined by the import layout style.
        Parameters:
        originalImports - A list of potentially unordered imports.
        Returns:
        A list of imports that are grouped and ordered.
      • builder

        public static ImportLayoutStyle.Builder builder()
      • isPackageAlwaysFolded

        public static boolean isPackageAlwaysFolded(List<ImportLayoutStyle.Block> packagesToFold, org.openrewrite.java.tree.J.Import checkImport)
      • toString

        public String toString()
        Overrides:
        toString in class Object