Skip to content

Commit

Permalink
Merge pull request square#592 from square/jw/local-name-allocators
Browse files Browse the repository at this point in the history
Scope name-allocated locals to their methods.
  • Loading branch information
swankjesse authored Jun 11, 2016
2 parents 5566f4d + 20f6917 commit 2ac980d
Showing 1 changed file with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ public final class JavaGenerator {
/**
* Preallocate all of the names we'll need for {@code type}. Names are allocated in precedence
* order, so names we're stuck with (serialVersionUID etc.) occur before proto field names are
* assigned. Names we aren't stuck with (typically for locals) yield to message fields.
* assigned.
*
* <p>Name allocations are computed once and reused because some types may be needed when
* generating other types.
*/
private final LoadingCache<MessageType, NameAllocator> typeToNameAllocator
private final LoadingCache<MessageType, NameAllocator> nameAllocators
= CacheBuilder.newBuilder().build(new CacheLoader<MessageType, NameAllocator>() {
@Override public NameAllocator load(MessageType type) throws Exception {
NameAllocator nameAllocator = new NameAllocator();
Expand All @@ -148,12 +148,6 @@ public final class JavaGenerator {
: field.name();
nameAllocator.newName(suggestion, field);
}
nameAllocator.newName("unknownFields", "unknownFields");
nameAllocator.newName("result", "result");
nameAllocator.newName("message", "message");
nameAllocator.newName("other", "other");
nameAllocator.newName("o", "o");
nameAllocator.newName("builder", "builder");
return nameAllocator;
}
});
Expand Down Expand Up @@ -397,7 +391,7 @@ public TypeSpec generateEnum(EnumType type) {

/** Returns the generated code for {@code type}, which may be a top-level or a nested type. */
public TypeSpec generateMessage(MessageType type) {
NameAllocator nameAllocator = typeToNameAllocator.getUnchecked(type);
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);

ClassName javaType = (ClassName) typeName(type.type());
ClassName builderJavaType = javaType.nestedClass("Builder");
Expand Down Expand Up @@ -743,7 +737,7 @@ private FieldSpec optionsField(ProtoType optionsType, String fieldName, Options

private String fieldName(ProtoType type, Field field) {
MessageType messageType = (MessageType) schema.getType(type);
NameAllocator names = typeToNameAllocator.getUnchecked(messageType);
NameAllocator names = nameAllocators.getUnchecked(messageType);
return names.get(field);
}

Expand Down Expand Up @@ -836,8 +830,10 @@ private MethodSpec messageFieldsConstructor(NameAllocator nameAllocator, Message
//
private MethodSpec messageFieldsAndUnknownFieldsConstructor(
NameAllocator nameAllocator, MessageType type) {
String adapterName = nameAllocator.get("ADAPTER");
String unknownFieldsName = nameAllocator.get("unknownFields");
NameAllocator localNameAllocator = nameAllocator.clone();

String adapterName = localNameAllocator.get("ADAPTER");
String unknownFieldsName = localNameAllocator.newName("unknownFields");
MethodSpec.Builder result = MethodSpec.constructorBuilder()
.addModifiers(PUBLIC)
.addStatement("super($N, $N)", adapterName, unknownFieldsName);
Expand All @@ -848,7 +844,7 @@ private MethodSpec messageFieldsAndUnknownFieldsConstructor(
boolean first = true;
for (Field field : oneOf.fields()) {
if (!first) fieldNamesBuilder.add(", ");
fieldNamesBuilder.add("$N", nameAllocator.get(field));
fieldNamesBuilder.add("$N", localNameAllocator.get(field));
first = false;
}
CodeBlock fieldNames = fieldNamesBuilder.build();
Expand All @@ -859,7 +855,7 @@ private MethodSpec messageFieldsAndUnknownFieldsConstructor(
}
for (Field field : type.fieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = nameAllocator.get(field);
String fieldName = localNameAllocator.get(field);
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroid && field.isOptional()) {
param.addAnnotation(NULLABLE);
Expand Down Expand Up @@ -889,8 +885,9 @@ private MethodSpec messageFieldsAndUnknownFieldsConstructor(
// && equals(optional_int32, o.optional_int32);
//
private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type) {
String otherName = nameAllocator.get("other");
String oName = nameAllocator.get("o");
NameAllocator localNameAllocator = nameAllocator.clone();
String otherName = localNameAllocator.newName("other");
String oName = localNameAllocator.newName("o");

TypeName javaType = typeName(type.type());
MethodSpec.Builder result = MethodSpec.methodBuilder("equals")
Expand All @@ -911,7 +908,7 @@ private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type)
result.addStatement("$T $N = ($T) $N", javaType, oName, javaType, otherName);
result.addCode("$[return unknownFields().equals($N.unknownFields())", oName);
for (Field field : fields) {
String fieldName = nameAllocator.get(field);
String fieldName = localNameAllocator.get(field);
if (field.isRequired() || field.isRepeated()) {
result.addCode("\n&& $1L.equals($2N.$1L)", fieldName, oName);
} else {
Expand Down Expand Up @@ -940,7 +937,9 @@ private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type)
// in order to be the same as the system hash code for an empty list.
//
private MethodSpec messageHashCode(NameAllocator nameAllocator, MessageType type) {
String resultName = nameAllocator.get("result");
NameAllocator localNameAllocator = nameAllocator.clone();

String resultName = localNameAllocator.newName("result");
MethodSpec.Builder result = MethodSpec.methodBuilder("hashCode")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
Expand All @@ -956,7 +955,7 @@ private MethodSpec messageHashCode(NameAllocator nameAllocator, MessageType type
result.beginControlFlow("if ($N == 0)", resultName);
result.addStatement("$N = unknownFields().hashCode()", resultName);
for (Field field : fields) {
String fieldName = nameAllocator.get(field);
String fieldName = localNameAllocator.get(field);
result.addCode("$1N = $1N * 37 + ", resultName);
if (field.isRepeated() || field.isRequired()) {
result.addStatement("$L.hashCode()", fieldName);
Expand All @@ -971,12 +970,14 @@ private MethodSpec messageHashCode(NameAllocator nameAllocator, MessageType type
}

private MethodSpec messageToString(NameAllocator nameAllocator, MessageType type) {
String builderName = nameAllocator.get("builder");
NameAllocator localNameAllocator = nameAllocator.clone();

MethodSpec.Builder result = MethodSpec.methodBuilder("toString")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.returns(String.class);

String builderName = localNameAllocator.newName("builder");
result.addStatement("$1T $2N = new $1T()", StringBuilder.class, builderName);

for (Field field : type.fieldsAndOneOfFields()) {
Expand Down Expand Up @@ -1056,7 +1057,9 @@ private MethodSpec builderNoArgsConstructor(NameAllocator nameAllocator, Message
// return builder;
// }
private MethodSpec newBuilder(NameAllocator nameAllocator, MessageType message) {
String builderName = nameAllocator.get("builder");
NameAllocator localNameAllocator = nameAllocator.clone();

String builderName = localNameAllocator.newName("builder");
ClassName javaType = (ClassName) typeName(message.type());
ClassName builderJavaType = javaType.nestedClass("Builder");

Expand All @@ -1068,7 +1071,7 @@ private MethodSpec newBuilder(NameAllocator nameAllocator, MessageType message)

List<Field> fields = message.fieldsAndOneOfFields();
for (Field field : fields) {
String fieldName = nameAllocator.get(field);
String fieldName = localNameAllocator.get(field);
if (field.isRepeated()) {
result.addStatement("$1L.$2L = $3T.copyOf($2S, $2L)", builderName, fieldName,
Internal.class);
Expand Down

0 comments on commit 2ac980d

Please sign in to comment.