Search in sources :

Example 66 with MethodSpec

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

the class JavaGenerator method messageAdapterRedact.

private MethodSpec messageAdapterRedact(NameAllocator nameAllocator, MessageType type, ClassName javaType, boolean useBuilder, ClassName builderJavaType) {
    MethodSpec.Builder result = MethodSpec.methodBuilder("redact").addAnnotation(Override.class).addModifiers(PUBLIC).returns(javaType).addParameter(javaType, "value");
    int redactedFieldCount = 0;
    List<String> requiredRedacted = new ArrayList<>();
    for (Field field : type.getFieldsAndOneOfFields()) {
        if (field.isRedacted()) {
            redactedFieldCount++;
            if (field.isRequired()) {
                requiredRedacted.add(nameAllocator.get(field));
            }
        }
    }
    if (!useBuilder) {
        result.addStatement((redactedFieldCount == 0) ? "return value" : "return null");
        return result.build();
    }
    if (!requiredRedacted.isEmpty()) {
        boolean isPlural = requiredRedacted.size() != 1;
        result.addStatement("throw new $T($S)", UnsupportedOperationException.class, (isPlural ? "Fields" : "Field") + " '" + Joiner.on("', '").join(requiredRedacted) + "' " + (isPlural ? "are" : "is") + " required and cannot be redacted.");
        return result.build();
    }
    result.addStatement("$1T builder = value.newBuilder()", builderJavaType);
    for (Field field : type.getFieldsAndOneOfFields()) {
        String fieldName = nameAllocator.get(field);
        if (field.isRedacted()) {
            if (field.isRepeated()) {
                result.addStatement("builder.$N = $T.emptyList()", fieldName, Collections.class);
            } else if (field.getType().isMap()) {
                result.addStatement("builder.$N = $T.emptyMap()", fieldName, Collections.class);
            } else {
                result.addStatement("builder.$N = null", fieldName);
            }
        } else if (!field.getType().isScalar() && !isEnum(field.getType())) {
            if (field.isRepeated()) {
                CodeBlock adapter = singleAdapterFor(field, nameAllocator);
                result.addStatement("$T.redactElements(builder.$N, $L)", Internal.class, fieldName, adapter);
            } else if (field.getType().isMap()) {
                // We only need to ask the values to redact themselves if the type is a message.
                if (!field.getType().getValueType().isScalar() && !isEnum(field.getType().getValueType())) {
                    CodeBlock adapter = singleAdapterFor(field.getType().getValueType());
                    result.addStatement("$T.redactElements(builder.$N, $L)", Internal.class, fieldName, adapter);
                }
            } else {
                CodeBlock adapter = adapterFor(field, nameAllocator);
                if (!field.isRequired()) {
                    result.addCode("if (builder.$N != null) ", fieldName);
                }
                result.addStatement("builder.$1N = $2L.redact(builder.$1N)", fieldName, adapter);
            }
        }
    }
    result.addStatement("builder.clearUnknownFields()");
    result.addStatement("return builder.build()");
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) Internal(com.squareup.wire.internal.Internal) ArrayList(java.util.ArrayList) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) Collections(java.util.Collections)

Example 67 with MethodSpec

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

the class JavaGenerator method setter.

private MethodSpec setter(NameAllocator nameAllocator, TypeName builderType, OneOf oneOf, Field field) {
    TypeName javaType = fieldType(field);
    String fieldName = nameAllocator.get(field);
    MethodSpec.Builder result = MethodSpec.methodBuilder(fieldName).addModifiers(PUBLIC).addParameter(javaType, fieldName).returns(builderType);
    if (!field.getDocumentation().isEmpty()) {
        result.addJavadoc("$L\n", sanitizeJavadoc(field.getDocumentation()));
    }
    if (field.isDeprecated()) {
        result.addAnnotation(Deprecated.class);
    }
    if (field.isRepeated() || field.getType().isMap()) {
        result.addStatement("$T.checkElementsNotNull($L)", Internal.class, fieldName);
    }
    result.addStatement("this.$L = $L", fieldName, fieldName);
    if (oneOf != null) {
        for (Field other : oneOf.getFields()) {
            if (field != other) {
                result.addStatement("this.$L = null", nameAllocator.get(other));
            }
        }
    }
    result.addStatement("return this");
    return result.build();
}
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) MethodSpec(com.squareup.javapoet.MethodSpec) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 68 with MethodSpec

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

the class JavaGenerator method messageAdapterDecode.

private MethodSpec messageAdapterDecode(NameAllocator nameAllocator, MessageType type, TypeName javaType, boolean useBuilder, ClassName builderJavaType) {
    MethodSpec.Builder result = MethodSpec.methodBuilder("decode").addAnnotation(Override.class).addModifiers(PUBLIC).returns(javaType).addParameter(ProtoReader.class, "reader").addException(IOException.class);
    List<Field> fields = TAG_ORDERING.sortedCopy(type.getFieldsAndOneOfFields());
    if (useBuilder) {
        result.addStatement("$1T builder = new $1T()", builderJavaType);
    } else {
        for (Field field : fields) {
            result.addStatement("$T $N = $L", fieldType(field), nameAllocator.get(field), initialValue(field));
        }
    }
    result.addStatement("long token = reader.beginMessage()");
    result.beginControlFlow("for (int tag; (tag = reader.nextTag()) != -1;)");
    result.beginControlFlow("switch (tag)");
    for (Field field : fields) {
        int fieldTag = field.getTag();
        if (isEnum(field.getType()) && !field.getType().equals(ProtoType.STRUCT_NULL)) {
            result.beginControlFlow("case $L:", fieldTag);
            result.beginControlFlow("try");
            result.addCode(decodeAndAssign(field, nameAllocator, useBuilder));
            result.addCode(";\n");
            if (useBuilder) {
                result.nextControlFlow("catch ($T e)", EnumConstantNotFoundException.class);
                result.addStatement("builder.addUnknownField(tag, $T.VARINT, (long) e.value)", FieldEncoding.class);
                // try/catch
                result.endControlFlow();
            } else {
                result.nextControlFlow("catch ($T ignored)", EnumConstantNotFoundException.class);
                // try/catch
                result.endControlFlow();
            }
            result.addStatement("break");
            // case
            result.endControlFlow();
        } else {
            result.addCode("case $L: $L; break;\n", fieldTag, decodeAndAssign(field, nameAllocator, useBuilder));
        }
    }
    result.beginControlFlow("default:");
    if (useBuilder) {
        result.addStatement("reader.readUnknownField(tag)");
    } else {
        result.addStatement("reader.skip()");
    }
    // default
    result.endControlFlow();
    // switch
    result.endControlFlow();
    // for
    result.endControlFlow();
    if (useBuilder) {
        result.addStatement("builder.addUnknownFields(reader.endMessageAndGetUnknownFields(token))");
    } else {
        result.addStatement("reader.endMessageAndGetUnknownFields(token)");
    }
    if (useBuilder) {
        result.addStatement("return builder.build()");
    } else {
        result.addCode("return fromProto(");
        boolean first = true;
        for (Field field : type.getFieldsAndOneOfFields()) {
            if (!first)
                result.addCode(", ");
            result.addCode("$N", nameAllocator.get(field));
            first = false;
        }
        result.addCode(");\n");
    }
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) ProtoReader(com.squareup.wire.ProtoReader)

Example 69 with MethodSpec

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

the class JavaGenerator method newBuilder.

// Example:
// 
// @Override
// public Message.Builder newBuilder() {
// Builder builder = new Builder();
// builder.optional_int32 = optional_int32;
// ...
// builder.addUnknownFields(unknownFields());
// return builder;
// }
private MethodSpec newBuilder(NameAllocator nameAllocator, MessageType message) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String builderName = localNameAllocator.newName("builder");
    ClassName javaType = (ClassName) typeName(message.getType());
    ClassName builderJavaType = javaType.nestedClass("Builder");
    MethodSpec.Builder result = MethodSpec.methodBuilder("newBuilder").addAnnotation(Override.class).addModifiers(PUBLIC).returns(builderJavaType).addStatement("$1T $2L = new $1T()", builderJavaType, builderName);
    List<Field> fields = message.getFieldsAndOneOfFields();
    for (Field field : fields) {
        String fieldName = localNameAllocator.get(field);
        if (field.isRepeated() || field.getType().isMap()) {
            result.addStatement("$1L.$2L = $3T.copyOf($2L)", builderName, fieldName, Internal.class);
        } else {
            result.addStatement("$1L.$2L = $2L", builderName, fieldName);
        }
    }
    result.addStatement("$L.addUnknownFields(unknownFields())", builderName);
    result.addStatement("return $L", builderName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 70 with MethodSpec

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

the class JavaGenerator method messageHashCode.

// Example:
// 
// @Override
// public int hashCode() {
// int result = hashCode;
// if (result == 0) {
// result = unknownFields().hashCode();
// result = result * 37 + (f != null ? f.hashCode() : 0);
// hashCode = result;
// }
// return result;
// }
// 
// For repeated fields, the final "0" in the example above changes to a "1"
// in order to be the same as the system hash code for an empty list.
// 
private MethodSpec messageHashCode(NameAllocator nameAllocator, MessageType type) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String resultName = localNameAllocator.newName("result");
    MethodSpec.Builder result = MethodSpec.methodBuilder("hashCode").addAnnotation(Override.class).addModifiers(PUBLIC).returns(int.class);
    List<Field> fields = type.getFieldsAndOneOfFields();
    if (fields.isEmpty()) {
        result.addStatement("return unknownFields().hashCode()");
        return result.build();
    }
    result.addStatement("int $N = super.hashCode", resultName);
    result.beginControlFlow("if ($N == 0)", resultName);
    result.addStatement("$N = unknownFields().hashCode()", resultName);
    for (Field field : fields) {
        String fieldName = localNameAllocator.get(field);
        TypeName typeName = fieldType(field);
        result.addCode("$1N = $1N * 37 + ", resultName);
        if (typeName == TypeName.BOOLEAN) {
            result.addStatement("$T.hashCode($N)", Boolean.class, fieldName);
        } else if (typeName == TypeName.INT) {
            result.addStatement("$T.hashCode($N)", Integer.class, fieldName);
        } else if (typeName == TypeName.LONG) {
            result.addStatement("$T.hashCode($N)", Long.class, fieldName);
        } else if (typeName == TypeName.FLOAT) {
            result.addStatement("$T.hashCode($N)", Float.class, fieldName);
        } else if (typeName == TypeName.DOUBLE) {
            result.addStatement("$T.hashCode($N)", Double.class, fieldName);
        } else if (field.isRequired() || field.isRepeated() || field.getType().isMap()) {
            result.addStatement("$L.hashCode()", fieldName);
        } else {
            result.addStatement("($1L != null ? $1L.hashCode() : 0)", fieldName);
        }
    }
    result.addStatement("super.hashCode = $N", resultName);
    result.endControlFlow();
    result.addStatement("return $N", resultName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) 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) MethodSpec(com.squareup.javapoet.MethodSpec) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)155 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)43 TypeName (com.squareup.javapoet.TypeName)42 ArrayList (java.util.ArrayList)38 ClassName (com.squareup.javapoet.ClassName)34 TypeSpec (com.squareup.javapoet.TypeSpec)31 ParameterSpec (com.squareup.javapoet.ParameterSpec)24 CodeBlock (com.squareup.javapoet.CodeBlock)23 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)20 Nonnull (javax.annotation.Nonnull)17 TypeMirror (javax.lang.model.type.TypeMirror)15 WireField (com.squareup.wire.WireField)14 Field (com.squareup.wire.schema.Field)14 TypeElement (javax.lang.model.element.TypeElement)14 ByteString (okio.ByteString)13 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)12 List (java.util.List)11 VariableElement (javax.lang.model.element.VariableElement)11 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)11 FieldSpec (com.squareup.javapoet.FieldSpec)10