Search in sources :

Example 1 with ProtoMember

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

the class JavaGenerator method get.

public static JavaGenerator get(Schema schema) {
    Map<ProtoType, TypeName> nameToJavaName = new LinkedHashMap<>();
    Map<ProtoMember, TypeName> memberToJavaName = new LinkedHashMap<>();
    for (ProtoFile protoFile : schema.getProtoFiles()) {
        String javaPackage = javaPackage(protoFile);
        putAll(nameToJavaName, javaPackage, null, protoFile.getTypes());
        for (Service service : protoFile.getServices()) {
            ClassName className = ClassName.get(javaPackage, service.type().getSimpleName());
            nameToJavaName.put(service.type(), className);
        }
        for (Extend extend : protoFile.getExtendList()) {
            if (annotationTargetType(extend) == null)
                continue;
            for (Field field : extend.getFields()) {
                if (!eligibleAsAnnotationMember(schema, field))
                    continue;
                ProtoMember protoMember = field.getMember();
                String simpleName = camelCase(protoMember.getSimpleName(), true) + "Option";
                ClassName className = ClassName.get(javaPackage, simpleName);
                memberToJavaName.put(protoMember, className);
            }
        }
    }
    nameToJavaName.putAll(BUILT_IN_TYPES_MAP);
    return new JavaGenerator(schema, nameToJavaName, memberToJavaName, new Profile(), false, /* emitAndroid */
    false, /* emitAndroidAnnotations */
    false, /* emitCompact */
    false, /* emitDeclaredOptions */
    false);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ProtoFile(com.squareup.wire.schema.ProtoFile) Service(com.squareup.wire.schema.Service) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) LinkedHashMap(java.util.LinkedHashMap) ProtoType(com.squareup.wire.schema.ProtoType) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) Extend(com.squareup.wire.schema.Extend) ClassName(com.squareup.javapoet.ClassName) ProtoMember(com.squareup.wire.schema.ProtoMember)

Example 2 with ProtoMember

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

the class JavaGenerator method optionsField.

// Example:
//
// public static final FieldOptions FIELD_OPTIONS_FOO = new FieldOptions.Builder()
//     .setExtension(Ext_custom_options.count, 42)
//     .build();
//
private FieldSpec optionsField(ProtoType optionsType, String fieldName, Options options) {
    TypeName optionsJavaType = typeName(optionsType);
    CodeBlock.Builder initializer = CodeBlock.builder();
    initializer.add("$[new $T.Builder()", optionsJavaType);
    boolean empty = true;
    for (Map.Entry<ProtoMember, ?> entry : options.map().entrySet()) {
        if (entry.getKey().equals(FIELD_DEPRECATED) || entry.getKey().equals(PACKED)) {
            continue;
        }
        Field optionField = schema.getField(entry.getKey());
        initializer.add("\n.$L($L)", fieldName(optionsType, optionField), fieldInitializer(optionField.type(), entry.getValue()));
        empty = false;
    }
    initializer.add("\n.build()$]");
    if (empty)
        return null;
    return FieldSpec.builder(optionsJavaType, fieldName).addModifiers(PUBLIC, STATIC, FINAL).initializer(initializer.build()).build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CodeBlock(com.squareup.javapoet.CodeBlock) ProtoMember(com.squareup.wire.schema.ProtoMember) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ProtoMember

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

the class JavaGenerator method fieldInitializer.

private CodeBlock fieldInitializer(ProtoType type, @Nullable Object value) {
    TypeName javaType = typeName(type);
    if (value instanceof List) {
        CodeBlock.Builder builder = CodeBlock.builder();
        builder.add("$T.asList(", Arrays.class);
        boolean first = true;
        for (Object o : (List<?>) value) {
            if (!first)
                builder.add(",");
            first = false;
            builder.add("\n$>$>$L$<$<", fieldInitializer(type, o));
        }
        builder.add(")");
        return builder.build();
    } else if (value instanceof Map) {
        CodeBlock.Builder builder = CodeBlock.builder();
        builder.add("new $T.Builder()", javaType);
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
            ProtoMember protoMember = (ProtoMember) entry.getKey();
            Field field = schema.getField(protoMember);
            CodeBlock valueInitializer = fieldInitializer(field.getType(), entry.getValue());
            builder.add("\n$>$>.$L($L)$<$<", fieldName(type, field), valueInitializer);
        }
        builder.add("\n$>$>.build()$<$<");
        return builder.build();
    } else if (javaType.equals(TypeName.BOOLEAN)) {
        return CodeBlock.of("$L", value != null ? value : false);
    } else if (javaType.equals(TypeName.INT)) {
        return CodeBlock.of("$L", optionValueToInt(value));
    } else if (javaType.equals(TypeName.LONG)) {
        return CodeBlock.of("$LL", optionValueToLong(value));
    } else if (javaType.equals(TypeName.FLOAT)) {
        if (value == null) {
            return CodeBlock.of("0.0f");
        } else if ("inf".equals(value)) {
            return CodeBlock.of("Float.POSITIVE_INFINITY");
        } else if ("-inf".equals(value)) {
            return CodeBlock.of("Float.NEGATIVE_INFINITY");
        } else if ("nan".equals(value) || "-nan".equals(value)) {
            return CodeBlock.of("Float.NaN");
        } else {
            return CodeBlock.of("$Lf", String.valueOf(value));
        }
    } else if (javaType.equals(TypeName.DOUBLE)) {
        if (value == null) {
            return CodeBlock.of("0.0d");
        } else if ("inf".equals(value)) {
            return CodeBlock.of("Double.POSITIVE_INFINITY");
        } else if ("-inf".equals(value)) {
            return CodeBlock.of("Double.NEGATIVE_INFINITY");
        } else if ("nan".equals(value) || "-nan".equals(value)) {
            return CodeBlock.of("Double.NaN");
        } else {
            return CodeBlock.of("$Ld", String.valueOf(value));
        }
    } else if (javaType.equals(STRING)) {
        return CodeBlock.of("$S", value != null ? value : "");
    } else if (javaType.equals(BYTE_STRING)) {
        if (value == null) {
            return CodeBlock.of("$T.EMPTY", ByteString.class);
        } else {
            return CodeBlock.of("$T.decodeBase64($S)", ByteString.class, ByteString.encodeString(String.valueOf(value), Charsets.ISO_8859_1).base64());
        }
    } else if (isEnum(type) && value != null) {
        return CodeBlock.of("$T.$L", javaType, value);
    } else {
        throw new IllegalStateException(type + " is not an allowed scalar type");
    }
}
Also used : 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) CacheBuilder(com.google.common.cache.CacheBuilder) CodeBlock(com.squareup.javapoet.CodeBlock) ProtoMember(com.squareup.wire.schema.ProtoMember) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 WireField (com.squareup.wire.WireField)3 Field (com.squareup.wire.schema.Field)3 ProtoMember (com.squareup.wire.schema.ProtoMember)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 CodeBlock (com.squareup.javapoet.CodeBlock)2 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)2 Map (java.util.Map)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 ClassName (com.squareup.javapoet.ClassName)1 Extend (com.squareup.wire.schema.Extend)1 ProtoFile (com.squareup.wire.schema.ProtoFile)1 ProtoType (com.squareup.wire.schema.ProtoType)1 Service (com.squareup.wire.schema.Service)1 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ByteString (okio.ByteString)1