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));
}
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));
}
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");
}
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();
}
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();
}
Aggregations