Skip to content

Commit 859e912

Browse files
agustafson-atlRandgalt
authored andcommitted
Adds RecordBuilder.Options.defaultNotNull parameters
1 parent 0cf07ca commit 859e912

File tree

5 files changed

+78
-9
lines changed

5 files changed

+78
-9
lines changed

options.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ for an example.
9494

9595
## Null Handling
9696

97-
| option | details |
98-
|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
99-
| `@RecordBuilder.Options(interpretNotNulls = true/false)` | Add not-null checks for record components annotated with any null-pattern annotation. The default is `false`. |
100-
| `@RecordBuilder.Options(interpretNotNullsPattern = "regex")` | The regex pattern used to determine if an annotation name means non-null. |
101-
| `@RecordBuilder.Options(allowNullableCollections = true/false)` | Adds special null handling for record collectioncomponents. The default is `false`. |
102-
| `@RecordBuilder.Options(nullablePattern = "regex")` | Regex pattern to use for `BuilderMode.STAGED_REQUIRED_ONLY` and `BuilderMode.STANDARD_AND_STAGED_REQUIRED_ONLY`. |
97+
| option | details |
98+
|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|
99+
| `@RecordBuilder.Options(interpretNotNulls = true/false)` | Add not-null checks for record components annotated with any null-pattern annotation. The default is `false`. |
100+
| `@RecordBuilder.Options(interpretNotNullsPattern = "regex")` | The regex pattern used to determine if an annotation name means non-null. |
101+
| `@RecordBuilder.Options(allowNullableCollections = true/false)` | Adds special null handling for record collectioncomponents. The default is `false`. |
102+
| `@RecordBuilder.Options(nullablePattern = "regex")` | Regex pattern to use for `BuilderMode.STAGED_REQUIRED_ONLY` and `BuilderMode.STANDARD_AND_STAGED_REQUIRED_ONLY`. |
103+
| `@RecordBuilder.Options(defaultNotNull = true/false)` | Add not-null checks for all record components unless annotated with any non-null-pattern annotation. The default is `false`. |
103104

104105
## Collections
105106

record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,13 @@
339339
* array.
340340
*/
341341
String onceOnlyAssignmentName() default "_onceOnlyCheck";
342+
343+
/**
344+
* Assumes that all fields are non-null, unless explicitly marked as null.
345+
*
346+
* @see #nullablePattern
347+
*/
348+
boolean defaultNotNull() default false;
342349
}
343350

344351
@Retention(RetentionPolicy.CLASS)

record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/InternalRecordBuilderProcessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,18 +491,20 @@ private void addStaticBuilder() {
491491
}
492492

493493
private void addNullCheckCodeBlock(CodeBlock.Builder builder) {
494-
if (metaData.interpretNotNulls()) {
494+
if (metaData.interpretNotNulls() || metaData.defaultNotNull()) {
495495
for (int i = 0; i < recordComponents.size(); ++i) {
496496
addNullCheckCodeBlock(builder, i);
497497
}
498498
}
499499
}
500500

501501
private void addNullCheckCodeBlock(CodeBlock.Builder builder, int index) {
502-
if (metaData.interpretNotNulls()) {
502+
if (metaData.interpretNotNulls() || metaData.defaultNotNull()) {
503503
var component = recordComponents.get(index);
504504
if (!collectionBuilderUtils.isImmutableCollection(component)) {
505-
if (!component.typeName().isPrimitive() && isNotNullAnnotated(component)) {
505+
if (!component.typeName().isPrimitive()
506+
&& (metaData.interpretNotNulls() && isNotNullAnnotated(component)
507+
|| metaData.defaultNotNull() && !isNullableAnnotated(component))) {
506508
builder.addStatement("$T.requireNonNull($L, $S)", Objects.class, component.name(),
507509
component.name() + " is required");
508510
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2019 The original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.soabase.recordbuilder.test;
17+
18+
import io.soabase.recordbuilder.core.RecordBuilder;
19+
import javax.validation.constraints.Null;
20+
21+
@RecordBuilder.Options(defaultNotNull = true)
22+
@RecordBuilder
23+
public record RecordWithDefaultNotNull(Integer notNullInteger, String notNullString, @Null String nullString) {
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2019 The original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.soabase.recordbuilder.test;
17+
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
21+
public class TestDefaultNotNull {
22+
@Test
23+
void testDefaultNotNullFieldSet() {
24+
final var validRecord = RecordWithDefaultNotNullBuilder.RecordWithDefaultNotNull(123, "wibble", null);
25+
Assertions.assertEquals(123, validRecord.notNullInteger());
26+
Assertions.assertEquals("wibble", validRecord.notNullString());
27+
Assertions.assertNull(validRecord.nullString());
28+
}
29+
30+
@Test
31+
void testDefaultNotNullThrowsExceptionForNullFields() {
32+
Assertions.assertThrows(NullPointerException.class,
33+
() -> RecordWithDefaultNotNullBuilder.RecordWithDefaultNotNull(null, null, null));
34+
}
35+
}

0 commit comments

Comments
 (0)