Package org.openrewrite.java
Class VariableNameUtils
java.lang.Object
org.openrewrite.java.VariableNameUtils
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionfindInheritedNames
(org.openrewrite.java.tree.J.ClassDeclaration classDeclaration) Collects class field names inherited from super classes.findNamesInScope
(org.openrewrite.Cursor scope) Find the names of variables that already exist in the scope of a cursor.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.static String
normalizeName
(String name) Replace accent and diacritics with normalized characters.
-
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 byscope
once it encounters it in the tree. For this to work, the client recipe must useCursor(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
Replace accent and diacritics with normalized characters.- Parameters:
name
- variable name to normalize.- Returns:
- normalized name.
-
findNamesInScope
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
.
-