use of com.squareup.wire.schema.MessageType in project wire by square.
the class JavaGeneratorTest method map.
@Test
public void map() throws Exception {
Schema schema = new RepoBuilder().add("message.proto", "" + "message Message {\n" + " map<string, CdnResource> templates = 1;\n" + " message CdnResource {\n" + " }\n" + "}\n").schema();
MessageType message = (MessageType) schema.getType("Message");
JavaGenerator javaGenerator = JavaGenerator.get(schema);
TypeSpec typeSpec = javaGenerator.generateType(message);
assertThat(JavaFile.builder("", typeSpec).build().toString()).contains("" + " @WireField(\n" + " tag = 1,\n" + " keyAdapter = \"com.squareup.wire.ProtoAdapter#STRING\",\n" + " adapter = \"Message$CdnResource#ADAPTER\"\n" + " )\n" + " public final Map<String, CdnResource> templates;\n");
}
use of com.squareup.wire.schema.MessageType in project wire by square.
the class JavaGenerator method generateMessage.
/** @deprecated Use {@link #generateType(Type)} */
@Deprecated
public TypeSpec generateMessage(MessageType type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
ClassName javaType = (ClassName) typeName(type.type());
ClassName builderJavaType = javaType.nestedClass("Builder");
TypeSpec.Builder builder = TypeSpec.classBuilder(javaType.simpleName());
builder.addModifiers(PUBLIC, FINAL);
if (javaType.enclosingClassName() != null) {
builder.addModifiers(STATIC);
}
if (!type.documentation().isEmpty()) {
builder.addJavadoc("$L\n", sanitizeJavadoc(type.documentation()));
}
ClassName messageType = emitAndroid ? ANDROID_MESSAGE : MESSAGE;
builder.superclass(messageOf(messageType, javaType, builderJavaType));
String adapterName = nameAllocator.get("ADAPTER");
String protoAdapterName = "ProtoAdapter_" + javaType.simpleName();
String protoAdapterClassName = nameAllocator.newName(protoAdapterName);
ClassName adapterJavaType = javaType.nestedClass(protoAdapterClassName);
builder.addField(messageAdapterField(adapterName, javaType, adapterJavaType));
if (emitAndroid) {
TypeName creatorType = creatorOf(javaType);
String creatorName = nameAllocator.get("CREATOR");
builder.addField(FieldSpec.builder(creatorType, creatorName, PUBLIC, STATIC, FINAL).initializer("$T.newCreator($L)", ANDROID_MESSAGE, adapterName).build());
}
builder.addField(FieldSpec.builder(TypeName.LONG, nameAllocator.get("serialVersionUID")).addModifiers(PRIVATE, STATIC, FINAL).initializer("$LL", 0L).build());
FieldSpec messageOptions = optionsField(MESSAGE_OPTIONS, nameAllocator.get("MESSAGE_OPTIONS"), type.options());
if (messageOptions != null) {
builder.addField(messageOptions);
}
for (Field field : type.fieldsAndOneOfFields()) {
String fieldName = nameAllocator.get(field);
String optionsFieldName = "FIELD_OPTIONS_" + fieldName.toUpperCase(Locale.US);
FieldSpec fieldOptions = optionsField(FIELD_OPTIONS, optionsFieldName, field.options());
if (fieldOptions != null) {
builder.addField(fieldOptions);
}
}
for (Field field : type.fieldsAndOneOfFields()) {
TypeName fieldJavaType = fieldType(field);
if ((field.type().isScalar() || isEnum(field.type())) && !field.isRepeated() && !field.isPacked()) {
builder.addField(defaultField(nameAllocator, field, fieldJavaType));
}
String fieldName = nameAllocator.get(field);
FieldSpec.Builder fieldBuilder = FieldSpec.builder(fieldJavaType, fieldName, PUBLIC, FINAL);
fieldBuilder.addAnnotation(wireFieldAnnotation(field));
if (!field.documentation().isEmpty()) {
fieldBuilder.addJavadoc("$L\n", sanitizeJavadoc(field.documentation()));
}
if (field.isExtension()) {
fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withPathOnly());
}
if (field.isDeprecated()) {
fieldBuilder.addAnnotation(Deprecated.class);
}
if (emitAndroid && field.isOptional()) {
fieldBuilder.addAnnotation(NULLABLE);
}
builder.addField(fieldBuilder.build());
}
builder.addMethod(messageFieldsConstructor(nameAllocator, type));
builder.addMethod(messageFieldsAndUnknownFieldsConstructor(nameAllocator, type));
builder.addMethod(newBuilder(nameAllocator, type));
builder.addMethod(messageEquals(nameAllocator, type));
builder.addMethod(messageHashCode(nameAllocator, type));
if (!emitCompact) {
builder.addMethod(messageToString(nameAllocator, type));
}
builder.addType(builder(nameAllocator, type, javaType, builderJavaType));
for (Type nestedType : type.nestedTypes()) {
builder.addType(generateType(nestedType));
}
if (!emitCompact) {
// Add the ProtoAdapter implementation at the very bottom since it's ugly serialization code.
builder.addType(messageAdapter(nameAllocator, type, javaType, adapterJavaType, builderJavaType));
}
return builder.build();
}
use of com.squareup.wire.schema.MessageType 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.wire.schema.MessageType in project wire by square.
the class JavaGenerator method generateAbstractAdapter.
/** Returns an abstract adapter for {@code type}. */
public TypeSpec generateAbstractAdapter(MessageType type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
ClassName adapterTypeName = abstractAdapterName(type.type());
ClassName typeName = (ClassName) typeName(type.type());
TypeSpec.Builder adapter = messageAdapter(nameAllocator, type, typeName, adapterTypeName, null).toBuilder();
if (adapterTypeName.enclosingClassName() != null) {
adapter.addModifiers(STATIC);
}
for (Type nestedType : type.nestedTypes()) {
if (profile.getAdapter(nestedType.type()) == null) {
throw new IllegalArgumentException("Missing custom proto adapter for " + nestedType.type().enclosingTypeOrPackage() + "." + nestedType.type().simpleName() + " when enclosing proto has custom proto adapter.");
}
if (nestedType instanceof MessageType) {
adapter.addType(generateAbstractAdapter((MessageType) nestedType));
}
}
return adapter.build();
}
Aggregations