Search in sources :

Example 1 with NameAllocator

use of com.squareup.javapoet.NameAllocator in project wire by square.

the class JavaGenerator method messageFieldsAndUnknownFieldsConstructor.

// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
//   super(ADAPTER, unknownFields);
//   this.optional_int32 = optional_int32;
//   this.optional_int64 = optional_int64;
// }
//
private MethodSpec messageFieldsAndUnknownFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
    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);
    for (OneOf oneOf : type.oneOfs()) {
        if (oneOf.fields().size() < 2)
            continue;
        CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
        boolean first = true;
        for (Field field : oneOf.fields()) {
            if (!first)
                fieldNamesBuilder.add(", ");
            fieldNamesBuilder.add("$N", 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.fieldsAndOneOfFields()) {
        TypeName javaType = fieldType(field);
        String fieldName = localNameAllocator.get(field);
        ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
        if (emitAndroid && field.isOptional()) {
            param.addAnnotation(NULLABLE);
        }
        result.addParameter(param.build());
        if (field.isRepeated() || field.type().isMap()) {
            result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $1L)", fieldName, Internal.class);
        } else {
            result.addStatement("this.$1L = $1L", fieldName);
        }
    }
    result.addParameter(BYTE_STRING, unknownFieldsName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) OneOf(com.squareup.wire.schema.OneOf) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString)

Example 2 with NameAllocator

use of com.squareup.javapoet.NameAllocator in project wire by square.

the class JavaGenerator method wireEnumConstantAnnotation.

// Example:
// 
// @WireEnumConstant(
// declaredName = "final",
// )
// 
@Nullable
private AnnotationSpec wireEnumConstantAnnotation(NameAllocator nameAllocator, EnumConstant constant) {
    AnnotationSpec.Builder result = AnnotationSpec.builder(WireEnumConstant.class);
    NameAllocator localNameAllocator = nameAllocator.clone();
    String generatedName = localNameAllocator.get(constant);
    if (generatedName.equals(constant.getName())) {
        return null;
    }
    result.addMember("declaredName", "$S", constant.getName());
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) Nullable(javax.annotation.Nullable)

Example 3 with NameAllocator

use of com.squareup.javapoet.NameAllocator in project wire by square.

the class JavaGenerator method generateMessage.

private TypeSpec generateMessage(MessageType type) {
    boolean constructorTakesAllFields = constructorTakesAllFields(type);
    NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
    ClassName javaType = (ClassName) typeName(type.getType());
    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.getDocumentation().isEmpty()) {
        builder.addJavadoc("$L\n", sanitizeJavadoc(type.getDocumentation()));
    }
    for (AnnotationSpec annotation : optionAnnotations(type.getOptions())) {
        builder.addAnnotation(annotation);
    }
    if (type.isDeprecated()) {
        builder.addAnnotation(Deprecated.class);
    }
    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, type.getType(), type.getSyntax()));
    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());
    for (Field field : type.getFieldsAndOneOfFields()) {
        TypeName fieldJavaType = fieldType(field);
        Field.EncodeMode encodeMode = field.getEncodeMode();
        if ((field.getType().isScalar() || isEnum(field.getType())) && !field.getType().equals(ProtoType.STRUCT_NULL) && encodeMode != Field.EncodeMode.REPEATED && encodeMode != Field.EncodeMode.PACKED && encodeMode != Field.EncodeMode.OMIT_IDENTITY) {
            builder.addField(defaultField(nameAllocator, field, fieldJavaType));
        }
        String fieldName = nameAllocator.get(field);
        FieldSpec.Builder fieldBuilder = FieldSpec.builder(fieldJavaType, fieldName, PUBLIC, FINAL);
        if (!field.getDocumentation().isEmpty()) {
            fieldBuilder.addJavadoc("$L\n", sanitizeJavadoc(field.getDocumentation()));
        }
        for (AnnotationSpec annotation : optionAnnotations(field.getOptions())) {
            fieldBuilder.addAnnotation(annotation);
        }
        fieldBuilder.addAnnotation(wireFieldAnnotation(nameAllocator, field, type));
        if (field.isExtension()) {
            fieldBuilder.addJavadoc("Extension source: $L\n", field.getLocation().withPathOnly());
        }
        if (field.isDeprecated()) {
            fieldBuilder.addAnnotation(Deprecated.class);
        }
        if (emitAndroidAnnotations && encodeMode == Field.EncodeMode.NULL_IF_ABSENT) {
            fieldBuilder.addAnnotation(NULLABLE);
        }
        builder.addField(fieldBuilder.build());
    }
    if (constructorTakesAllFields) {
        builder.addMethod(messageFieldsConstructor(nameAllocator, type));
    }
    builder.addMethod(messageConstructor(nameAllocator, type, builderJavaType));
    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.getNestedTypes()) {
        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();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) FieldSpec(com.squareup.javapoet.FieldSpec) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) JvmLanguages.annotationTargetType(com.squareup.wire.schema.internal.JvmLanguages.annotationTargetType) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) ElementType(java.lang.annotation.ElementType) EnclosingType(com.squareup.wire.schema.EnclosingType) EnumType(com.squareup.wire.schema.EnumType) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 4 with NameAllocator

use of com.squareup.javapoet.NameAllocator in project wire by square.

the class JavaGenerator method newBuilder.

// Example:
// 
// @Override
// public Message.Builder newBuilder() {
// Builder builder = new Builder();
// builder.optional_int32 = optional_int32;
// ...
// builder.addUnknownFields(unknownFields());
// return builder;
// }
private MethodSpec newBuilder(NameAllocator nameAllocator, MessageType message) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String builderName = localNameAllocator.newName("builder");
    ClassName javaType = (ClassName) typeName(message.getType());
    ClassName builderJavaType = javaType.nestedClass("Builder");
    MethodSpec.Builder result = MethodSpec.methodBuilder("newBuilder").addAnnotation(Override.class).addModifiers(PUBLIC).returns(builderJavaType).addStatement("$1T $2L = new $1T()", builderJavaType, builderName);
    List<Field> fields = message.getFieldsAndOneOfFields();
    for (Field field : fields) {
        String fieldName = localNameAllocator.get(field);
        if (field.isRepeated() || field.getType().isMap()) {
            result.addStatement("$1L.$2L = $3T.copyOf($2L)", builderName, fieldName, Internal.class);
        } else {
            result.addStatement("$1L.$2L = $2L", builderName, fieldName);
        }
    }
    result.addStatement("$L.addUnknownFields(unknownFields())", builderName);
    result.addStatement("return $L", builderName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 5 with NameAllocator

use of com.squareup.javapoet.NameAllocator in project wire by square.

the class JavaGenerator method messageHashCode.

// Example:
// 
// @Override
// public int hashCode() {
// int result = hashCode;
// if (result == 0) {
// result = unknownFields().hashCode();
// result = result * 37 + (f != null ? f.hashCode() : 0);
// hashCode = result;
// }
// return result;
// }
// 
// For repeated fields, the final "0" in the example above changes to a "1"
// in order to be the same as the system hash code for an empty list.
// 
private MethodSpec messageHashCode(NameAllocator nameAllocator, MessageType type) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String resultName = localNameAllocator.newName("result");
    MethodSpec.Builder result = MethodSpec.methodBuilder("hashCode").addAnnotation(Override.class).addModifiers(PUBLIC).returns(int.class);
    List<Field> fields = type.getFieldsAndOneOfFields();
    if (fields.isEmpty()) {
        result.addStatement("return unknownFields().hashCode()");
        return result.build();
    }
    result.addStatement("int $N = super.hashCode", resultName);
    result.beginControlFlow("if ($N == 0)", resultName);
    result.addStatement("$N = unknownFields().hashCode()", resultName);
    for (Field field : fields) {
        String fieldName = localNameAllocator.get(field);
        TypeName typeName = fieldType(field);
        result.addCode("$1N = $1N * 37 + ", resultName);
        if (typeName == TypeName.BOOLEAN) {
            result.addStatement("$T.hashCode($N)", Boolean.class, fieldName);
        } else if (typeName == TypeName.INT) {
            result.addStatement("$T.hashCode($N)", Integer.class, fieldName);
        } else if (typeName == TypeName.LONG) {
            result.addStatement("$T.hashCode($N)", Long.class, fieldName);
        } else if (typeName == TypeName.FLOAT) {
            result.addStatement("$T.hashCode($N)", Float.class, fieldName);
        } else if (typeName == TypeName.DOUBLE) {
            result.addStatement("$T.hashCode($N)", Double.class, fieldName);
        } else if (field.isRequired() || field.isRepeated() || field.getType().isMap()) {
            result.addStatement("$L.hashCode()", fieldName);
        } else {
            result.addStatement("($1L != null ? $1L.hashCode() : 0)", fieldName);
        }
    }
    result.addStatement("super.hashCode = $N", resultName);
    result.endControlFlow();
    result.addStatement("return $N", resultName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Aggregations

NameAllocator (com.squareup.javapoet.NameAllocator)14 ByteString (okio.ByteString)11 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)10 MethodSpec (com.squareup.javapoet.MethodSpec)8 WireField (com.squareup.wire.WireField)8 Field (com.squareup.wire.schema.Field)7 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)6 TypeName (com.squareup.javapoet.TypeName)6 ClassName (com.squareup.javapoet.ClassName)5 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)5 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)4 TypeSpec (com.squareup.javapoet.TypeSpec)4 MessageType (com.squareup.wire.schema.MessageType)4 EnclosingType (com.squareup.wire.schema.EnclosingType)3 EnumType (com.squareup.wire.schema.EnumType)3 OneOf (com.squareup.wire.schema.OneOf)3 ProtoType (com.squareup.wire.schema.ProtoType)3 Type (com.squareup.wire.schema.Type)3 CodeBlock (com.squareup.javapoet.CodeBlock)2 FieldSpec (com.squareup.javapoet.FieldSpec)2