Search in sources :

Example 1 with EnumType

use of com.squareup.wire.schema.EnumType 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 2 with EnumType

use of com.squareup.wire.schema.EnumType in project wire by square.

the class JavaGenerator method identityValue.

private CodeBlock identityValue(Field field) {
    switch(field.getEncodeMode()) {
        case MAP:
            return CodeBlock.of("$T.emptyMap()", Collections.class);
        case REPEATED:
        case PACKED:
            return CodeBlock.of("$T.emptyList()", Collections.class);
        case NULL_IF_ABSENT:
            return CodeBlock.of("null");
        case OMIT_IDENTITY:
            ProtoType protoType = field.getType();
            Type type = schema.getType(protoType);
            if (protoType.equals(ProtoType.STRUCT_NULL)) {
                return CodeBlock.of("null");
            } else if (field.isOneOf()) {
                return CodeBlock.of("null");
            } else if (protoType.isScalar()) {
                CodeBlock value = PROTOTYPE_TO_IDENTITY_VALUES.get(protoType);
                if (value == null) {
                    throw new IllegalArgumentException("Unexpected scalar proto type: " + protoType);
                }
                return value;
            } else if (type instanceof MessageType) {
                return CodeBlock.of("null");
            } else if (type instanceof EnumType) {
                return identity((EnumType) type);
            }
        case REQUIRED:
        default:
            throw new IllegalArgumentException("No identity value for field: " + field + "(" + field.getEncodeMode() + ")");
    }
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) 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) EnumType(com.squareup.wire.schema.EnumType) CodeBlock(com.squareup.javapoet.CodeBlock) MessageType(com.squareup.wire.schema.MessageType)

Example 3 with EnumType

use of com.squareup.wire.schema.EnumType in project wire by square.

the class JavaGenerator method abstractAdapterName.

/**
 * Returns the Java type of the abstract adapter class generated for a corresponding {@code
 * protoType}. Returns null if {@code protoType} is not using a custom proto adapter.
 */
@Nullable
public ClassName abstractAdapterName(ProtoType protoType) {
    TypeName profileJavaName = profile.javaTarget(protoType);
    if (profileJavaName == null)
        return null;
    TypeName typeName = typeToJavaName.get(protoType);
    Type type = schema.getType(protoType);
    ClassName javaName;
    if (typeName instanceof ClassName) {
        javaName = (ClassName) typeName;
    } else if (typeName instanceof ParameterizedTypeName) {
        javaName = ((ParameterizedTypeName) typeName).rawType;
    } else {
        throw new IllegalArgumentException("Unexpected typeName :" + typeName);
    }
    return type instanceof EnumType ? javaName.peerClass(javaName.simpleName() + "Adapter") : javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter");
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) 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) EnumType(com.squareup.wire.schema.EnumType) ClassName(com.squareup.javapoet.ClassName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Nullable(javax.annotation.Nullable)

Aggregations

EnclosingType (com.squareup.wire.schema.EnclosingType)3 EnumType (com.squareup.wire.schema.EnumType)3 MessageType (com.squareup.wire.schema.MessageType)3 ProtoType (com.squareup.wire.schema.ProtoType)3 Type (com.squareup.wire.schema.Type)3 JvmLanguages.annotationTargetType (com.squareup.wire.schema.internal.JvmLanguages.annotationTargetType)3 ElementType (java.lang.annotation.ElementType)3 ClassName (com.squareup.javapoet.ClassName)2 CodeBlock (com.squareup.javapoet.CodeBlock)1 NameAllocator (com.squareup.javapoet.NameAllocator)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)1 Nullable (javax.annotation.Nullable)1