Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds RecordBuilder.Options.defaultNotNull parameters #185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions options.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ for an example.

## Null Handling

| option | details |
|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
| `@RecordBuilder.Options(interpretNotNulls = true/false)` | Add not-null checks for record components annotated with any null-pattern annotation. The default is `false`. |
| `@RecordBuilder.Options(interpretNotNullsPattern = "regex")` | The regex pattern used to determine if an annotation name means non-null. |
| `@RecordBuilder.Options(allowNullableCollections = true/false)` | Adds special null handling for record collectioncomponents. The default is `false`. |
| `@RecordBuilder.Options(nullablePattern = "regex")` | Regex pattern to use for `BuilderMode.STAGED_REQUIRED_ONLY` and `BuilderMode.STANDARD_AND_STAGED_REQUIRED_ONLY`. |
| option | details |
|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|
| `@RecordBuilder.Options(interpretNotNulls = true/false)` | Add not-null checks for record components annotated with any null-pattern annotation. The default is `false`. |
| `@RecordBuilder.Options(interpretNotNullsPattern = "regex")` | The regex pattern used to determine if an annotation name means non-null. |
| `@RecordBuilder.Options(allowNullableCollections = true/false)` | Adds special null handling for record collectioncomponents. The default is `false`. |
| `@RecordBuilder.Options(nullablePattern = "regex")` | Regex pattern to use for `BuilderMode.STAGED_REQUIRED_ONLY` and `BuilderMode.STANDARD_AND_STAGED_REQUIRED_ONLY`. |
| `@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`. |

## Collections

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@
* array.
*/
String onceOnlyAssignmentName() default "_onceOnlyCheck";

/**
* Assumes that all fields are non-null, unless explicitly marked as null.
*
* @see #nullablePattern
*/
boolean defaultNotNull() default false;
}

@Retention(RetentionPolicy.CLASS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,18 +491,20 @@ private void addStaticBuilder() {
}

private void addNullCheckCodeBlock(CodeBlock.Builder builder) {
if (metaData.interpretNotNulls()) {
if (metaData.interpretNotNulls() || metaData.defaultNotNull()) {
for (int i = 0; i < recordComponents.size(); ++i) {
addNullCheckCodeBlock(builder, i);
}
}
}

private void addNullCheckCodeBlock(CodeBlock.Builder builder, int index) {
if (metaData.interpretNotNulls()) {
if (metaData.interpretNotNulls() || metaData.defaultNotNull()) {
var component = recordComponents.get(index);
if (!collectionBuilderUtils.isImmutableCollection(component)) {
if (!component.typeName().isPrimitive() && isNotNullAnnotated(component)) {
if (!component.typeName().isPrimitive()
&& (metaData.interpretNotNulls() && isNotNullAnnotated(component)
|| metaData.defaultNotNull() && !isNullableAnnotated(component))) {
builder.addStatement("$T.requireNonNull($L, $S)", Objects.class, component.name(),
component.name() + " is required");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 The original author or authors
*
* 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 io.soabase.recordbuilder.core.RecordBuilder;
import javax.validation.constraints.Null;

@RecordBuilder.Options(defaultNotNull = true)
@RecordBuilder
public record RecordWithDefaultNotNull(Integer notNullInteger, String notNullString, @Null String nullString) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019 The original author or authors
*
* 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 org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestDefaultNotNull {
@Test
void testDefaultNotNullFieldSet() {
final var validRecord = RecordWithDefaultNotNullBuilder.RecordWithDefaultNotNull(123, "wibble", null);
Assertions.assertEquals(123, validRecord.notNullInteger());
Assertions.assertEquals("wibble", validRecord.notNullString());
Assertions.assertNull(validRecord.nullString());
}

@Test
void testDefaultNotNullThrowsExceptionForNullFields() {
Assertions.assertThrows(NullPointerException.class,
() -> RecordWithDefaultNotNullBuilder.RecordWithDefaultNotNull(null, null, null));
}
}
Loading