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

move some boilerplate exception-throwing into a utility class in core #8159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.hibernate.proxy.EntityNotFoundDelegate;

/**
* Standard non-JPA implementation of EntityNotFoundDelegate, throwing the
* Standard non-JPA implementation of {@link EntityNotFoundDelegate}, throwing the
* Hibernate-specific {@link ObjectNotFoundException}.
*
* @author Steve Ebersole
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.exception.spi;

import org.hibernate.Incubating;
import org.hibernate.ObjectNotFoundException;

/**
* Utility methods used by Hibernate Processor.
*
* @author Gavin King
*
* @since 6.5
*/
@Incubating
public class Exceptions {
public static <E> E require(E entity, String entityName, Object id) {
if ( entity == null ) {
throw new ObjectNotFoundException( entityName, id );
}
else {
return entity;
}
}

public static void require(Object argument, String parameterName) {
if ( argument == null ) {
throw new IllegalArgumentException("Null " + parameterName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ public List<AnnotationMirror> inheritedAnnotations() {
return emptyList();
}
}

void nullCheck(StringBuilder declaration, String parameterName) {
declaration
.append('\t')
.append(annotationMetaEntity.staticImport("org.hibernate.exception.spi.Exceptions", "require"))
.append('(')
.append(parameterName.replace('.', '$'))
.append(", \"")
.append(parameterName)
.append("\");\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,6 @@ void nullChecks(List<String> paramTypes, StringBuilder declaration) {
}
}

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(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ boolean isNullable(int index) {

@Override
boolean singleResult() {
return false; // we don't need to convert Query exceptions
return true;
}

@Override
Expand Down Expand Up @@ -92,35 +92,11 @@ private void throwIfNull(StringBuilder declaration) {
}
else if (!nullable) {
declaration
.append(";\n");
if (dataRepository) {
declaration
.append("\t\tif (_result == null) throw new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
.append("(\"No '")
.append(annotationMetaEntity.importType(entity))
.append("' for given id [\" + ")
.append(paramName)
.append(" + \"]\",\n\t\t\t\tnew ")
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
.append("((Object) ")
.append(paramName)
.append(", \"")
.append(entity)
.append("\"));\n")
.append("\t\treturn _result");
}
else {
declaration
.append("\tif (_result == null) throw new ")
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
.append("((Object) ")
.append(paramName)
.append(", \"")
.append(entity)
.append("\");\n")
.append("\treturn _result");
}
.append(", \"")
.append(entity)
.append("\", ")
.append(paramName)
.append(')');
}
declaration
.append(";\n");
Expand All @@ -139,7 +115,9 @@ private void varOrReturn(StringBuilder declaration) {
}
else if (!nullable) {
declaration
.append("\tvar _result = ");
.append("\treturn ")
.append(annotationMetaEntity.staticImport("org.hibernate.exception.spi.Exceptions", "require"))
.append('(');
}
else {
declaration
Expand All @@ -149,6 +127,31 @@ else if (!nullable) {
.append(sessionName);
}

@Override
void convertExceptions(StringBuilder declaration) {
if (dataRepository) {
if ( !nullable && containerType==null ) {
declaration
.append("\t}\n")
.append("\tcatch (")
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
.append("(exception.getMessage(), exception);\n");
}
declaration
.append("\t}\n")
.append("\tcatch (")
.append(annotationMetaEntity.importType("jakarta.persistence.PersistenceException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.DataException"))
.append("(exception.getMessage(), exception);\n")
.append("\t}\n");
}
}

private void findWithFetchProfiles(StringBuilder declaration) {
unwrapSession( declaration );
declaration
Expand Down Expand Up @@ -182,13 +185,4 @@ private void findWithNoFetchProfiles(StringBuilder declaration) {
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean hasStringAttribute() {
public String getAttributeDeclarationString() {
StringBuilder declaration = new StringBuilder();
preamble(declaration);
nullCheck(declaration);
nullCheck(declaration, parameterName);
declaration.append("\ttry {\n");
delegateCall(declaration);
returnArgument(declaration);
Expand Down Expand Up @@ -176,15 +176,6 @@ private String returnType() {
}
}

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 (")
Expand Down