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.CloneableAssigns 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:
Pass a unique tag object to each allocation. The tag scopes the name, and can be used to look up the allocated name later. Typically the tag is the object that is being named. In the above example we useNameAllocator nameAllocator = new NameAllocator(); for (MyProperty property : properties) { nameAllocator.newName(property.name(), property); } nameAllocator.newName("sb", "string builder");propertyfor 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:
The above code generates unique names if presented with conflicts. Given user-supplied properties with namesMethodSpec.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();abandsbthis generates the following:
The underscore is appended to@Override public String toString() { StringBuilder sb_ = new StringBuilder(); sb_.append(ab); sb_.append(sb); return sb_.toString(); }sbto avoid conflicting with the user-suppliedsbproperty. 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>allocatedNamesprivate java.util.Map<java.lang.Object,java.lang.String>tagToName
-
Constructor Summary
Constructors Modifier Constructor Description NameAllocator()privateNameAllocator(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 NameAllocatorclone()Create a deep copy of this NameAllocator.java.lang.Stringget(java.lang.Object tag)Retrieve a name created withnewName(String, Object).java.lang.StringnewName(java.lang.String suggestion)Return a new name usingsuggestionthat will not be a Java identifier or clash with other names.java.lang.StringnewName(java.lang.String suggestion, java.lang.Object tag)Return a new name usingsuggestionthat will not be a Java identifier or clash with other names.static java.lang.StringtoJavaIdentifier(java.lang.String suggestion)
-
-
-
Method Detail
-
newName
public java.lang.String newName(java.lang.String suggestion)
Return a new name usingsuggestionthat 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 usingsuggestionthat will not be a Java identifier or clash with other names. The returned value can be queried multiple times by passingtagtoget(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:
clonein classjava.lang.Object- Returns:
- A deep copy of this NameAllocator.
-
-