Class ImportLayoutStyle

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

public class ImportLayoutStyle extends Object implements KotlinStyle
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:

  • topLevelSymbolsToUseStarImport - How many imports from the same package must be present before they should be collapsed into a star import. The default is 5.
  • javaStaticsAndEnumsToUseStarImport - How many java static and enum imports from the same type must be present before they should be collapsed into a 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.
    • Nested Class Summary

      Nested Classes
      Modifier and Type
      Class
      Description
      static interface 
      A block represents a grouping of imports based on matching rules.
      static class 
       
    • Constructor Summary

      Constructors
      Constructor
      Description
      ImportLayoutStyle(int topLevelSymbolsToUseStarImport, int javaStaticsAndEnumsToUseStarImport, List<ImportLayoutStyle.Block> layout, List<ImportLayoutStyle.Block> packagesToFold, boolean importAliasesSeparately)
       
    • Method Summary

      Modifier and Type
      Method
      Description
      List<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>>
      addImport(List<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>> originalImports, org.openrewrite.java.tree.J.Import toAdd, @Nullable org.openrewrite.java.tree.J.Package pkg, Collection<org.openrewrite.java.tree.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.
       
      static boolean
      isPackageAlwaysFolded(List<ImportLayoutStyle.Block> packagesToFold, org.openrewrite.java.tree.J.Import checkImport)
       
      List<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>>
      orderImports(List<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>> originalImports, Collection<org.openrewrite.java.tree.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.
       

      Methods inherited from class java.lang.Object

      clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

      Methods inherited from interface org.openrewrite.style.Style

      applyDefaults, getJacksonPolymorphicTypeTag, merge
    • Constructor Details

    • Method Details

      • addImport

        public List<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>> addImport(List<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>> originalImports, org.openrewrite.java.tree.J.Import toAdd, @Nullable org.openrewrite.java.tree.J.Package pkg, Collection<org.openrewrite.java.tree.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<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>> orderImports(List<org.openrewrite.java.tree.JRightPadded<org.openrewrite.java.tree.J.Import>> originalImports, Collection<org.openrewrite.java.tree.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