Class VariableNameUtils

java.lang.Object
org.openrewrite.java.VariableNameUtils

@Incubating(since="7.25.0") public class VariableNameUtils extends Object
  • Method Details

    • generateVariableName

      public static String generateVariableName(String baseName, org.openrewrite.Cursor scope, VariableNameUtils.GenerationStrategy strategy)
      Generates a variable name without namespace conflicts in the scope of a cursor.

      A JavaSourceFile must be available in the Cursor path to account for all names available in the cursor scope.

      Since the client recipe may have modified the LST element represented by scope, this method will visit the value held by scope once it encounters it in the tree. For this to work, the client recipe must use Cursor(Cursor, Object) to pass a cursor holding the modified LST element.

      Parameters:
      baseName - baseName for the variable.
      scope - The cursor position of a JavaVisitor, TreeVisitor.getCursor().
      strategy - VariableNameUtils.GenerationStrategy to use if a name already exists with the baseName.
      Returns:
      either the baseName if a namespace conflict does not exist or a name generated with the provided strategy.
    • normalizeName

      public static String normalizeName(String name)
      Replace accent and diacritics with normalized characters.
      Parameters:
      name - variable name to normalize.
      Returns:
      normalized name.
    • findNamesInScope

      public static Set<String> findNamesInScope(org.openrewrite.Cursor scope)
      Find the names of variables that already exist in the scope of a cursor. A JavaSourceFile must be available in the Cursor path to account for all names available in the cursor scope.
      Parameters:
      scope - The cursor position of a JavaVisitor, TreeVisitor.getCursor().
      Returns:
      Variable names available in the name scope of the cursor.
    • findInheritedNames

      public static Set<String> findInheritedNames(org.openrewrite.java.tree.J.ClassDeclaration classDeclaration)
      Collects class field names inherited from super classes.

      Note: Does not currently account for JavaType.Unknown.