Package com.squareup.javapoet
Class NameAllocator
- java.lang.Object
-
- com.squareup.javapoet.NameAllocator
-
- All Implemented Interfaces:
java.lang.Cloneable
public final class NameAllocator extends java.lang.Object implements java.lang.Cloneable
Assigns Java identifier names to avoid collisions, keywords, and invalid characters. To use, first create an instance and allocate all of the names that you need. Typically this is a mix of user-supplied names and constants:NameAllocator nameAllocator = new NameAllocator(); for (MyProperty property : properties) { nameAllocator.newName(property.name(), property); } nameAllocator.newName("sb", "string builder");
property
for the user-supplied property names, and"string builder"
for our constant string builder.Once we've allocated names we can use them when generating code:
MethodSpec.Builder builder = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .returns(String.class); builder.addStatement("$1T $2N = new $1T()", StringBuilder.class, nameAllocator.get("string builder")); for (MyProperty property : properties) { builder.addStatement("$N.append($N)", nameAllocator.get("string builder"), nameAllocator.get(property)); } builder.addStatement("return $N", nameAllocator.get("string builder")); return builder.build();
ab
andsb
this generates the following:@Override public String toString() { StringBuilder sb_ = new StringBuilder(); sb_.append(ab); sb_.append(sb); return sb_.toString(); }
sb
to avoid conflicting with the user-suppliedsb
property. Underscores are also prefixed for names that start with a digit, and used to replace name-unsafe characters like space or dash.When dealing with multiple independent inner scopes, use a
clone()
of the NameAllocator used for the outer scope to further refine name allocation for a specific inner scope.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Set<java.lang.String>
allocatedNames
private java.util.Map<java.lang.Object,java.lang.String>
tagToName
-
Constructor Summary
Constructors Modifier Constructor Description NameAllocator()
private
NameAllocator(java.util.LinkedHashSet<java.lang.String> allocatedNames, java.util.LinkedHashMap<java.lang.Object,java.lang.String> tagToName)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description NameAllocator
clone()
Create a deep copy of this NameAllocator.java.lang.String
get(java.lang.Object tag)
Retrieve a name created withnewName(String, Object)
.java.lang.String
newName(java.lang.String suggestion)
Return a new name usingsuggestion
that will not be a Java identifier or clash with other names.java.lang.String
newName(java.lang.String suggestion, java.lang.Object tag)
Return a new name usingsuggestion
that will not be a Java identifier or clash with other names.static java.lang.String
toJavaIdentifier(java.lang.String suggestion)
-
-
-
Method Detail
-
newName
public java.lang.String newName(java.lang.String suggestion)
Return a new name usingsuggestion
that will not be a Java identifier or clash with other names.
-
newName
public java.lang.String newName(java.lang.String suggestion, java.lang.Object tag)
Return a new name usingsuggestion
that will not be a Java identifier or clash with other names. The returned value can be queried multiple times by passingtag
toget(Object)
.
-
toJavaIdentifier
public static java.lang.String toJavaIdentifier(java.lang.String suggestion)
-
get
public java.lang.String get(java.lang.Object tag)
Retrieve a name created withnewName(String, Object)
.
-
clone
public NameAllocator clone()
Create a deep copy of this NameAllocator. Useful to create multiple independent refinements of a NameAllocator to be used in the respective definition of multiples, independently-scoped, inner code blocks.- Overrides:
clone
in classjava.lang.Object
- Returns:
- A deep copy of this NameAllocator.
-
-