diff --git a/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/OptionalType.java b/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/OptionalType.java index eb2a1ce8..30ff1387 100644 --- a/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/OptionalType.java +++ b/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/OptionalType.java @@ -39,10 +39,9 @@ private static boolean isOptional(ClassType component) { static Optional fromClassType(final ClassType component) { if (isOptional(component)) { - if (!(component.typeName() instanceof ParameterizedTypeName)) { + if (!(component.typeName() instanceof ParameterizedTypeName parameterizedType)) { return Optional.of(new OptionalType(optionalType, TypeName.get(Object.class))); } - var parameterizedType = (ParameterizedTypeName) component.typeName(); final TypeName containingType = parameterizedType.typeArguments.isEmpty() ? TypeName.get(Object.class) : parameterizedType.typeArguments.get(0); diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/RecordWithOptional2.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/RecordWithOptional2.java new file mode 100644 index 00000000..28e1205a --- /dev/null +++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/RecordWithOptional2.java @@ -0,0 +1,26 @@ +/** + * Copyright 2019 Jordan Zimmerman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.soabase.recordbuilder.test; + +import java.util.Optional; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; +import io.soabase.recordbuilder.core.RecordBuilder; + +@RecordBuilder.Options(emptyDefaultForOptional = true) +@RecordBuilder +public record RecordWithOptional2(Optional value, Optional raw, OptionalInt i, OptionalLong l, OptionalDouble d) {} diff --git a/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestOptional.java b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestOptional.java index a5759873..7e2bc377 100644 --- a/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestOptional.java +++ b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestOptional.java @@ -49,4 +49,20 @@ var record = RecordWithOptionalBuilder.builder() Assertions.assertEquals(OptionalLong.of(424242L), record.l()); Assertions.assertEquals(OptionalDouble.of(42.42), record.d()); } + + @Test + void testOptionalSetters() { + var record = RecordWithOptional2Builder.builder() + .value(Optional.of("value")) + .raw(Optional.of("rawValue")) + .i(OptionalInt.of(42)) + .l(OptionalLong.of(424242L)) + .d(OptionalDouble.of(42.42)) + .build(); + Assertions.assertEquals(Optional.of("value"), record.value()); + Assertions.assertEquals(Optional.of("rawValue"), record.raw()); + Assertions.assertEquals(OptionalInt.of(42), record.i()); + Assertions.assertEquals(OptionalLong.of(424242L), record.l()); + Assertions.assertEquals(OptionalDouble.of(42.42), record.d()); + } }