Search in sources :

Example 11 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project auto by google.

the class ImmutableListSerializerExtensionTest method toProxy.

@Test
public void toProxy() {
    TypeMirror typeMirror = declaredTypeOf(ImmutableList.class, Integer.class);
    Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get();
    CodeBlock actualCodeBlock = serializer.toProxy(CodeBlock.of("x"));
    assertThat(actualCodeBlock.toString()).isEqualTo(String.format("x.stream().map(%s.wrapper(value$ -> value$)).collect(%s.toImmutableList())", FUNCTION_WITH_EXCEPTIONS, IMMUTABLE_LIST));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) CodeBlock(com.squareup.javapoet.CodeBlock) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer) Test(org.junit.Test) CompilationAbstractTest(com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)

Example 12 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project auto by google.

the class ImmutableListSerializerExtensionTest method fromProxy.

@Test
public void fromProxy() {
    TypeMirror typeMirror = declaredTypeOf(ImmutableList.class, Integer.class);
    Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get();
    CodeBlock actualCodeBlock = serializer.fromProxy(CodeBlock.of("x"));
    assertThat(actualCodeBlock.toString()).isEqualTo(String.format("x.stream().map(%s.wrapper(value$ -> value$)).collect(%s.toImmutableList())", FUNCTION_WITH_EXCEPTIONS, IMMUTABLE_LIST));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) CodeBlock(com.squareup.javapoet.CodeBlock) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer) Test(org.junit.Test) CompilationAbstractTest(com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)

Example 13 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project auto by google.

the class OptionalSerializerExtensionTest method toProxy.

@Test
public void toProxy() {
    TypeMirror typeMirror = declaredTypeOf(Optional.class, Integer.class);
    Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get();
    CodeBlock actualCodeBlock = serializer.toProxy(CodeBlock.of("x"));
    assertThat(actualCodeBlock.toString()).isEqualTo("x.isPresent() ? x.get() : null");
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) CodeBlock(com.squareup.javapoet.CodeBlock) Serializer(com.google.auto.value.extension.serializable.serializer.interfaces.Serializer) CompilationAbstractTest(com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest) Test(org.junit.Test)

Example 14 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project dagger by square.

the class ModuleAdapterProcessor method includesInitializer.

private CodeBlock includesInitializer(TypeElement type, Object[] includes) {
    CodeBlock.Builder result = CodeBlock.builder();
    result.add("{ ");
    for (Object include : includes) {
        if (!(include instanceof TypeMirror)) {
            // TODO(tbroyer): pass annotation information
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unexpected value: " + include + " in includes of " + type, type);
            continue;
        }
        TypeMirror typeMirror = (TypeMirror) include;
        result.add("$T.class, ", typeMirror);
    }
    result.add("}");
    return result.build();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) CodeBlock(com.squareup.javapoet.CodeBlock)

Example 15 with CodeBlock

use of com.squareup.javapoet.CodeBlock 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)

Aggregations

CodeBlock (com.squareup.javapoet.CodeBlock)42 MethodSpec (com.squareup.javapoet.MethodSpec)19 TypeMirror (javax.lang.model.type.TypeMirror)13 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)11 TypeName (com.squareup.javapoet.TypeName)10 CompilationAbstractTest (com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)8 ClassName (com.squareup.javapoet.ClassName)8 WireField (com.squareup.wire.WireField)8 Field (com.squareup.wire.schema.Field)8 ByteString (okio.ByteString)8 Test (org.junit.Test)8 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)7 ArrayList (java.util.ArrayList)7 Serializer (com.google.auto.value.extension.serializable.serializer.interfaces.Serializer)6 ParameterSpec (com.squareup.javapoet.ParameterSpec)6 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)4 TypeSpec (com.squareup.javapoet.TypeSpec)3 Element (javax.lang.model.element.Element)3 Predicate (com.google.common.base.Predicate)2 FieldSpec (com.squareup.javapoet.FieldSpec)2