use of com.squareup.javapoet.TypeSpec in project storio by pushtorefresh.
the class MappingGenerator method generateJavaFile.
@NotNull
@Override
public JavaFile generateJavaFile(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
final ClassName storIOSQLiteTypeClassName = ClassName.get(storIOSQLiteTypeMeta.packageName, storIOSQLiteTypeMeta.simpleName);
ClassName superclass = ClassName.get("com.pushtorefresh.storio.sqlite", SUFFIX);
ParameterizedTypeName superclassParametrized = ParameterizedTypeName.get(superclass, storIOSQLiteTypeClassName);
final TypeSpec mapping = TypeSpec.classBuilder(storIOSQLiteTypeMeta.simpleName + SUFFIX).addJavadoc("Generated mapping with collection of resolvers.\n").addModifiers(PUBLIC).superclass(superclassParametrized).addMethod(createConstructor(storIOSQLiteTypeMeta)).build();
return JavaFile.builder(storIOSQLiteTypeMeta.packageName, mapping).indent(INDENT).build();
}
use of com.squareup.javapoet.TypeSpec 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.javapoet.TypeSpec in project wire by square.
the class JavaGenerator method generateEnum.
/** @deprecated Use {@link #generateType(Type)} */
@Deprecated
public TypeSpec generateEnum(EnumType type) {
ClassName javaType = (ClassName) typeName(type.type());
TypeSpec.Builder builder = TypeSpec.enumBuilder(javaType.simpleName()).addModifiers(PUBLIC).addSuperinterface(WireEnum.class);
if (!type.documentation().isEmpty()) {
builder.addJavadoc("$L\n", sanitizeJavadoc(type.documentation()));
}
// Output Private tag field
builder.addField(TypeName.INT, "value", PRIVATE, FINAL);
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
constructorBuilder.addStatement("this.value = value");
constructorBuilder.addParameter(TypeName.INT, "value");
// Enum constant options, each of which requires a constructor parameter and a field.
Set<ProtoMember> allOptionFieldsBuilder = new LinkedHashSet<>();
for (EnumConstant constant : type.constants()) {
for (ProtoMember protoMember : constant.options().map().keySet()) {
Field optionField = schema.getField(protoMember);
if (allOptionFieldsBuilder.add(protoMember)) {
TypeName optionJavaType = typeName(optionField.type());
builder.addField(optionJavaType, optionField.name(), PUBLIC, FINAL);
constructorBuilder.addParameter(optionJavaType, optionField.name());
constructorBuilder.addStatement("this.$L = $L", optionField.name(), optionField.name());
}
}
}
ImmutableList<ProtoMember> allOptionMembers = ImmutableList.copyOf(allOptionFieldsBuilder);
String enumArgsFormat = "$L" + Strings.repeat(", $L", allOptionMembers.size());
builder.addMethod(constructorBuilder.build());
MethodSpec.Builder fromValueBuilder = MethodSpec.methodBuilder("fromValue").addJavadoc("Return the constant for {@code value} or null.\n").addModifiers(PUBLIC, STATIC).returns(javaType).addParameter(int.class, "value").beginControlFlow("switch (value)");
Set<Integer> seenTags = new LinkedHashSet<>();
for (EnumConstant constant : type.constants()) {
Object[] enumArgs = new Object[allOptionMembers.size() + 1];
enumArgs[0] = constant.tag();
for (int i = 0; i < allOptionMembers.size(); i++) {
ProtoMember protoMember = allOptionMembers.get(i);
Field field = schema.getField(protoMember);
Object value = constant.options().map().get(protoMember);
enumArgs[i + 1] = value != null ? fieldInitializer(field.type(), value) : null;
}
TypeSpec.Builder constantBuilder = TypeSpec.anonymousClassBuilder(enumArgsFormat, enumArgs);
if (!constant.documentation().isEmpty()) {
constantBuilder.addJavadoc("$L\n", sanitizeJavadoc(constant.documentation()));
}
if ("true".equals(constant.options().get(ENUM_DEPRECATED))) {
constantBuilder.addAnnotation(Deprecated.class);
}
builder.addEnumConstant(constant.name(), constantBuilder.build());
// Ensure constant case tags are unique, which might not be the case if allow_alias is true.
if (seenTags.add(constant.tag())) {
fromValueBuilder.addStatement("case $L: return $L", constant.tag(), constant.name());
}
}
builder.addMethod(fromValueBuilder.addStatement("default: return null").endControlFlow().build());
// ADAPTER
FieldSpec.Builder adapterBuilder = FieldSpec.builder(adapterOf(javaType), "ADAPTER").addModifiers(PUBLIC, STATIC, FINAL);
ClassName adapterJavaType = javaType.nestedClass("ProtoAdapter_" + javaType.simpleName());
if (!emitCompact) {
adapterBuilder.initializer("new $T()", adapterJavaType);
} else {
adapterBuilder.initializer("$T.newEnumAdapter($T.class)", ProtoAdapter.class, javaType);
}
builder.addField(adapterBuilder.build());
// Enum type options.
FieldSpec options = optionsField(ENUM_OPTIONS, "ENUM_OPTIONS", type.options());
if (options != null) {
builder.addField(options);
}
// Public Getter
builder.addMethod(MethodSpec.methodBuilder("getValue").addAnnotation(Override.class).addModifiers(PUBLIC).returns(TypeName.INT).addStatement("return value").build());
if (!emitCompact) {
// Adds the ProtoAdapter implementation at the bottom.
builder.addType(enumAdapter(javaType, adapterJavaType));
}
return builder.build();
}
use of com.squareup.javapoet.TypeSpec in project wire by square.
the class JavaGenerator method messageAdapter.
private TypeSpec messageAdapter(NameAllocator nameAllocator, MessageType type, ClassName javaType, ClassName adapterJavaType, ClassName builderType) {
boolean useBuilder = builderType != null;
TypeSpec.Builder adapter = TypeSpec.classBuilder(adapterJavaType.simpleName()).superclass(adapterOf(javaType));
if (useBuilder) {
adapter.addModifiers(PRIVATE, STATIC, FINAL);
} else {
adapter.addModifiers(PUBLIC, ABSTRACT);
}
adapter.addMethod(MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($T.LENGTH_DELIMITED, $T.class)", FieldEncoding.class, javaType).build());
if (!useBuilder) {
MethodSpec.Builder fromProto = MethodSpec.methodBuilder("fromProto").addModifiers(PUBLIC, ABSTRACT).returns(javaType);
for (Field field : type.fieldsAndOneOfFields()) {
TypeName fieldType = fieldType(field);
String fieldName = nameAllocator.get(field);
fromProto.addParameter(fieldType, fieldName);
adapter.addMethod(MethodSpec.methodBuilder(fieldName).addModifiers(PUBLIC, ABSTRACT).addParameter(javaType, "value").returns(fieldType).build());
}
adapter.addMethod(fromProto.build());
}
adapter.addMethod(messageAdapterEncodedSize(nameAllocator, type, javaType, useBuilder));
adapter.addMethod(messageAdapterEncode(nameAllocator, type, javaType, useBuilder));
adapter.addMethod(messageAdapterDecode(nameAllocator, type, javaType, useBuilder, builderType));
adapter.addMethod(messageAdapterRedact(nameAllocator, type, javaType, useBuilder, builderType));
for (Field field : type.fieldsAndOneOfFields()) {
if (field.type().isMap()) {
TypeName adapterType = adapterOf(fieldType(field));
adapter.addField(FieldSpec.builder(adapterType, field.name(), PRIVATE, FINAL).initializer("$T.newMapAdapter($L, $L)", ADAPTER, singleAdapterFor(field.type().keyType()), singleAdapterFor(field.type().valueType())).build());
}
}
return adapter.build();
}
use of com.squareup.javapoet.TypeSpec 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();
}
Aggregations