use Objects.requireNonNull in generated code for repositories

instead of directly throwing IllegalArgumentException
This commit is contained in:
Gavin King 2024-12-07 19:30:29 +01:00
parent 02cf728bcb
commit 8d30649cf1
5 changed files with 14 additions and 28 deletions

View File

@ -17,6 +17,7 @@ import static org.hibernate.processor.annotation.AnnotationMetaEntity.usingReact
import static org.hibernate.processor.annotation.AnnotationMetaEntity.usingReactiveSessionAccess;
import static org.hibernate.processor.annotation.AnnotationMetaEntity.usingStatelessSession;
import static org.hibernate.processor.util.Constants.ENTITY_MANAGER;
import static org.hibernate.processor.util.Constants.OBJECTS;
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
/**
@ -76,4 +77,15 @@ public abstract class AbstractAnnotatedMethod implements MetaAttribute {
return emptyList();
}
}
void nullCheck(StringBuilder declaration, String paramName) {
declaration
.append('\t')
.append(annotationMetaEntity.staticImport(OBJECTS, "requireNonNull"))
.append('(')
.append(paramName.replace('.', '$'))
.append(", \"Null ")
.append(paramName)
.append("\");\n");
}
}

View File

@ -114,15 +114,6 @@ public abstract class AbstractCriteriaMethod extends AbstractFinderMethod {
}
}
private static void nullCheck(StringBuilder declaration, String paramName) {
declaration
.append("\tif (")
.append(paramName.replace('.', '$'))
.append(" == null) throw new IllegalArgumentException(\"Null ")
.append(paramName)
.append("\");\n");
}
void where(StringBuilder declaration, List<String> paramTypes) {
declaration
.append("\t_query.where(");

View File

@ -180,13 +180,4 @@ public class IdFinderMethod extends AbstractFinderMethod {
declaration
.append(")");
}
private static void nullCheck(StringBuilder declaration, String parameterName) {
declaration
.append("\tif (")
.append(parameterName)
.append(" == null) throw new IllegalArgumentException(\"Null ")
.append(parameterName)
.append("\");\n");
}
}

View File

@ -56,7 +56,7 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
public String getAttributeDeclarationString() {
StringBuilder declaration = new StringBuilder();
preamble(declaration);
nullCheck(declaration);
nullCheck(declaration, parameterName);
declaration.append("\ttry {\n");
delegateCall(declaration);
returnArgument(declaration);
@ -194,15 +194,6 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
}
}
private void nullCheck(StringBuilder declaration) {
declaration
.append("\tif (")
.append(parameterName)
.append(" == null) throw new IllegalArgumentException(\"Null ")
.append(parameterName)
.append("\");\n");
}
private void convertException(StringBuilder declaration, String exception, String convertedException) {
declaration
.append("\tcatch (")

View File

@ -123,6 +123,7 @@ public final class Constants {
public static final String MAP_ATTRIBUTE = "jakarta.persistence.metamodel.MapAttribute";
public static final String JAVA_OBJECT = "java.lang.Object";
public static final String OBJECTS = "java.util.Objects";
public static final String ITERABLE = "java.lang.Iterable";
public static final String COLLECTION = "java.util.Collection";
public static final String LIST = "java.util.List";