Search in sources :

Example 41 with CodeBlock

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

the class JavaGenerator method optionAnnotation.

@Nullable
private AnnotationSpec optionAnnotation(ProtoMember protoMember, Object value) {
    if (!emitAppliedOptions)
        return null;
    Field field = schema.getField(protoMember);
    if (field == null)
        return null;
    if (!eligibleAsAnnotationMember(schema, field))
        return null;
    ProtoFile protoFile = schema.protoFile(field.getLocation().getPath());
    String simpleName = camelCase(field.getName(), true) + "Option";
    ClassName type = ClassName.get(javaPackage(protoFile), simpleName);
    CodeBlock fieldValue = fieldInitializer(field.getType(), value);
    return AnnotationSpec.builder(type).addMember("value", fieldValue).build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) ProtoFile(com.squareup.wire.schema.ProtoFile) ClassName(com.squareup.javapoet.ClassName) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) Nullable(javax.annotation.Nullable)

Example 42 with CodeBlock

use of com.squareup.javapoet.CodeBlock 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();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) Internal(com.squareup.wire.internal.Internal) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) OneOf(com.squareup.wire.schema.OneOf) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field)

Aggregations

CodeBlock (com.squareup.javapoet.CodeBlock)42 MethodSpec (com.squareup.javapoet.MethodSpec)19 TypeMirror (javax.lang.model.type.TypeMirror)13 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)11 TypeName (com.squareup.javapoet.TypeName)10 CompilationAbstractTest (com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)8 ClassName (com.squareup.javapoet.ClassName)8 WireField (com.squareup.wire.WireField)8 Field (com.squareup.wire.schema.Field)8 ByteString (okio.ByteString)8 Test (org.junit.Test)8 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)7 ArrayList (java.util.ArrayList)7 Serializer (com.google.auto.value.extension.serializable.serializer.interfaces.Serializer)6 ParameterSpec (com.squareup.javapoet.ParameterSpec)6 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)4 TypeSpec (com.squareup.javapoet.TypeSpec)3 Element (javax.lang.model.element.Element)3 Predicate (com.google.common.base.Predicate)2 FieldSpec (com.squareup.javapoet.FieldSpec)2