From 8d30649cf1b9c967c4c74e7fd044dd9f934f536d Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sat, 7 Dec 2024 19:30:29 +0100 Subject: [PATCH] use Objects.requireNonNull in generated code for repositories instead of directly throwing IllegalArgumentException --- .../annotation/AbstractAnnotatedMethod.java | 12 ++++++++++++ .../processor/annotation/AbstractCriteriaMethod.java | 9 --------- .../processor/annotation/IdFinderMethod.java | 9 --------- .../processor/annotation/LifecycleMethod.java | 11 +---------- .../java/org/hibernate/processor/util/Constants.java | 1 + 5 files changed, 14 insertions(+), 28 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractAnnotatedMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractAnnotatedMethod.java index 05a1421e33..5487ffaf7a 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractAnnotatedMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractAnnotatedMethod.java @@ -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"); + } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java index 0a3ff3b980..c29373d85f 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java @@ -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 paramTypes) { declaration .append("\t_query.where("); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java index 328ed90cc8..87ac3a1918 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java @@ -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"); - } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/LifecycleMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/LifecycleMethod.java index b49ba85280..79b4ddc5e7 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/LifecycleMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/LifecycleMethod.java @@ -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 (") diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java index 8d93474d47..922e20cd95 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java @@ -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";