Search in sources :

Example 1 with WireField

use of com.squareup.wire.WireField in project wire by square.

the class JavaGenerator method wireFieldAnnotation.

// Example:
// 
// @WireField(
// tag = 1,
// type = INT32
// )
// 
private AnnotationSpec wireFieldAnnotation(NameAllocator nameAllocator, Field field, MessageType message) {
    AnnotationSpec.Builder result = AnnotationSpec.builder(WireField.class);
    NameAllocator localNameAllocator = nameAllocator.clone();
    int tag = field.getTag();
    result.addMember("tag", String.valueOf(tag));
    if (field.getType().isMap()) {
        result.addMember("keyAdapter", "$S", adapterString(field.getType().getKeyType()));
        result.addMember("adapter", "$S", adapterString(field.getType().getValueType()));
    } else {
        result.addMember("adapter", "$S", adapterString(field.getType()));
    }
    WireField.Label wireFieldLabel;
    // noinspection ConstantConditions
    switch(field.getEncodeMode()) {
        case REQUIRED:
            wireFieldLabel = WireField.Label.REQUIRED;
            break;
        case OMIT_IDENTITY:
            wireFieldLabel = WireField.Label.OMIT_IDENTITY;
            break;
        case REPEATED:
            wireFieldLabel = WireField.Label.REPEATED;
            break;
        case PACKED:
            wireFieldLabel = WireField.Label.PACKED;
            break;
        case MAP:
        case NULL_IF_ABSENT:
        default:
            wireFieldLabel = null;
    }
    if (wireFieldLabel != null) {
        result.addMember("label", "$T.$L", WireField.Label.class, wireFieldLabel);
    }
    if (field.isRedacted()) {
        result.addMember("redacted", "true");
    }
    String generatedName = localNameAllocator.get(field);
    if (!generatedName.equals(field.getName())) {
        result.addMember("declaredName", "$S", field.getName());
    }
    if (!field.getJsonName().equals(field.getName())) {
        result.addMember("jsonName", "$S", field.getJsonName());
    }
    if (field.isOneOf()) {
        String oneofName = null;
        for (OneOf oneOf : message.getOneOfs()) {
            if (oneOf.getFields().contains(field)) {
                oneofName = oneOf.getName();
                break;
            }
        }
        if (oneofName == null) {
            throw new IllegalArgumentException("No oneof found for field: " + field.getQualifiedName());
        }
        result.addMember("oneofName", "$S", oneofName);
    }
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) OneOf(com.squareup.wire.schema.OneOf) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) WireField(com.squareup.wire.WireField)

Aggregations

AnnotationSpec (com.squareup.javapoet.AnnotationSpec)1 NameAllocator (com.squareup.javapoet.NameAllocator)1 WireField (com.squareup.wire.WireField)1 OneOf (com.squareup.wire.schema.OneOf)1 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)1 ByteString (okio.ByteString)1