use of com.squareup.javapoet.NameAllocator in project wire by square.
the class JavaGenerator method generateAdapterForCustomType.
/**
* Returns a standalone adapter for {@code type}.
*/
public TypeSpec generateAdapterForCustomType(Type type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
ClassName adapterTypeName = abstractAdapterName(type.getType());
ClassName typeName = (ClassName) typeName(type.getType());
TypeSpec.Builder adapter;
if (type instanceof MessageType) {
adapter = messageAdapter(nameAllocator, (MessageType) type, typeName, adapterTypeName, null).toBuilder();
} else {
adapter = enumAdapter(nameAllocator, (EnumType) type, typeName, adapterTypeName).toBuilder();
}
if (adapterTypeName.enclosingClassName() != null)
adapter.addModifiers(STATIC);
for (Type nestedType : type.getNestedTypes()) {
if (profile.getAdapter(nestedType.getType()) == null) {
throw new IllegalArgumentException("Missing custom proto adapter for " + nestedType.getType().getEnclosingTypeOrPackage() + "." + nestedType.getType().getSimpleName() + " when enclosing proto has custom proto adapter.");
}
adapter.addType(generateAdapterForCustomType(nestedType));
}
return adapter.build();
}
use of com.squareup.javapoet.NameAllocator in project wire by square.
the class JavaGenerator method fieldName.
private String fieldName(ProtoType type, Field field) {
MessageType messageType = (MessageType) schema.getType(type);
NameAllocator names = nameAllocators.getUnchecked(messageType);
return names.get(field);
}
use of com.squareup.javapoet.NameAllocator in project wire by square.
the class JavaGenerator method mapAdapter.
// Example:
//
// private ProtoAdapter<Map<String, ModelEvaluation>> modelsAdapter() {
// ProtoAdapter<Map<String, ModelEvaluation>> result = models;
// if (result == null) {
// result = ProtoAdapter.newMapAdapter(ProtoAdapter.STRING, ModelEvaluation.ADAPTER);
// models = result;
// }
// return result;
// }
//
private MethodSpec mapAdapter(NameAllocator nameAllocator, TypeName adapterType, String fieldName, ProtoType mapType) {
NameAllocator localNameAllocator = nameAllocator.clone();
String resultName = localNameAllocator.newName("result");
MethodSpec.Builder result = MethodSpec.methodBuilder(fieldName + "Adapter").addModifiers(PRIVATE).returns(adapterType);
result.addStatement("$T $N = $N", adapterType, resultName, fieldName);
result.beginControlFlow("if ($N == null)", resultName);
result.addStatement("$N = $T.newMapAdapter($L, $L)", resultName, ADAPTER, singleAdapterFor(mapType.getKeyType()), singleAdapterFor(mapType.getValueType()));
result.addStatement("$N = $N", fieldName, resultName);
result.endControlFlow();
result.addStatement("return $N", resultName);
return result.build();
}
use of com.squareup.javapoet.NameAllocator in project wire by square.
the class JavaGenerator method messageConstructor.
// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = optional_int32;
// this.optional_int64 = optional_int64;
// }
//
// Alternate example, where the constructor takes in a builder, would be the case when there are
// too many fields:
//
// public SimpleMessage(Builder builder, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = builder.optional_int32;
// this.optional_int64 = builder.optional_int64;
// }
//
private MethodSpec messageConstructor(NameAllocator nameAllocator, MessageType type, ClassName builderJavaType) {
boolean constructorTakesAllFields = constructorTakesAllFields(type);
NameAllocator localNameAllocator = nameAllocator.clone();
String adapterName = localNameAllocator.get("ADAPTER");
String unknownFieldsName = localNameAllocator.newName("unknownFields");
String builderName = localNameAllocator.newName("builder");
MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
for (OneOf oneOf : type.getOneOfs()) {
if (oneOf.getFields().size() < 2)
continue;
CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
boolean first = true;
for (Field field : oneOf.getFields()) {
if (!first)
fieldNamesBuilder.add(", ");
if (constructorTakesAllFields) {
fieldNamesBuilder.add("$N", localNameAllocator.get(field));
} else {
fieldNamesBuilder.add("$N.$N", builderName, localNameAllocator.get(field));
}
first = false;
}
CodeBlock fieldNames = fieldNamesBuilder.build();
result.beginControlFlow("if ($T.countNonNull($L) > 1)", Internal.class, fieldNames);
result.addStatement("throw new IllegalArgumentException($S)", "at most one of " + fieldNames + " may be non-null");
result.endControlFlow();
}
for (Field field : type.getFieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = localNameAllocator.get(field);
String fieldAccessName = constructorTakesAllFields ? fieldName : builderName + "." + fieldName;
if (constructorTakesAllFields) {
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroidAnnotations && field.getEncodeMode() == Field.EncodeMode.NULL_IF_ABSENT) {
param.addAnnotation(NULLABLE);
}
result.addParameter(param.build());
}
if (field.getEncodeMode() == Field.EncodeMode.OMIT_IDENTITY) {
// Other scalars use not-boxed types to guarantee a value.
if (field.getType().isScalar() && (field.getType() == ProtoType.STRING || field.getType() == ProtoType.BYTES) || (isEnum(field.getType()) && !field.getType().equals(ProtoType.STRUCT_NULL))) {
result.beginControlFlow("if ($L == null)", fieldAccessName);
result.addStatement("throw new IllegalArgumentException($S)", fieldAccessName + " == null");
result.endControlFlow();
}
}
if (field.getType().isMap() && isStruct(field.getType().getValueType())) {
result.addStatement("this.$1L = $2T.immutableCopyOfMapWithStructValues($1S, $3L)", fieldName, Internal.class, fieldAccessName);
} else if (isStruct(field.getType())) {
result.addStatement("this.$1L = $2T.immutableCopyOfStruct($1S, $3L)", fieldName, Internal.class, fieldAccessName);
} else if (field.isRepeated() || field.getType().isMap()) {
result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $3L)", fieldName, Internal.class, fieldAccessName);
} else {
result.addStatement("this.$1L = $2L", fieldName, fieldAccessName);
}
}
if (!constructorTakesAllFields) {
result.addParameter(builderJavaType, builderName);
}
result.addParameter(BYTE_STRING, unknownFieldsName);
return result.build();
}
Aggregations