Top Description Fields Constructors Methods
com.squareup.javapoet

public final Class NameAllocator

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.util.LinkedHashMap, .LinkedHashSet, .Map, .Set, .UUID, javax.lang.model.SourceVersion

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");
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 use 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();
The above code generates unique names if presented with conflicts. Given user-supplied properties with names ab and sb this generates the following:
   @Override
  public String toString() {
    StringBuilder sb_ = new StringBuilder();
    sb_.append(ab);
    sb_.append(sb);
    return sb_.toString();
  }
The underscore is appended to sb to avoid conflicting with the user-supplied sb 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

Modifier and TypeField and Description
private final Set<String>
private final Map<Object, String>

Constructor Summary

AccessConstructor and Description
public
private

Method Summary

Modifier and TypeMethod and Description
public NameAllocator

Returns:

A deep copy of this NameAllocator.
clone
()

Overrides java.lang.Object.clone.

Create a deep copy of this NameAllocator.
public String
get(Object tag)

Retrieve a name created with newName(String, Object).

public String
newName(String suggestion)

Return a new name using suggestion that will not be a Java identifier or clash with other names.

public String
newName(String suggestion, Object tag)

Return a new name using suggestion that will not be a Java identifier or clash with other names.

public static String
Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allocatedNamesback to summary
private final Set<String> allocatedNames
tagToNameback to summary
private final Map<Object, String> tagToName

Constructor Detail

NameAllocatorback to summary
public NameAllocator()
NameAllocatorback to summary
private NameAllocator(LinkedHashSet<String> allocatedNames, LinkedHashMap<Object, String> tagToName)

Method Detail

cloneback to summary
public NameAllocator clone()

Overrides java.lang.Object.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.

Returns:NameAllocator

A deep copy of this NameAllocator.

Annotations
@Override
getback to summary
public String get(Object tag)

Retrieve a name created with newName(String, Object).

newNameback to summary
public String newName(String suggestion)

Return a new name using suggestion that will not be a Java identifier or clash with other names.

newNameback to summary
public String newName(String suggestion, Object tag)

Return a new name using suggestion that will not be a Java identifier or clash with other names. The returned value can be queried multiple times by passing tag to get(Object).

toJavaIdentifierback to summary
public static String toJavaIdentifier(String suggestion)