Search in sources :

Example 11 with NameAllocator

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();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) 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) MessageType(com.squareup.wire.schema.MessageType) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 12 with NameAllocator

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);
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) MessageType(com.squareup.wire.schema.MessageType)

Example 13 with NameAllocator

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();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) MethodSpec(com.squareup.javapoet.MethodSpec) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 14 with NameAllocator

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();
}
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

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