more null checking, and some refactoring

This commit is contained in:
Gavin King 2024-02-24 12:59:26 +01:00
parent 7abe8f5f2b
commit 235fc26ee6
7 changed files with 161 additions and 85 deletions

View File

@ -175,8 +175,10 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
.append(" ") .append(" ")
.append(methodName); .append(methodName);
parameters( paramTypes, declaration ) ; parameters( paramTypes, declaration ) ;
declaration declaration.append(" {\n");
.append(" {\n"); }
void tryReturn(StringBuilder declaration) {
if (dataRepository) { if (dataRepository) {
declaration declaration
.append("\ttry {\n\t"); .append("\ttry {\n\t");

View File

@ -323,6 +323,10 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
abstract boolean singleResult(); abstract boolean singleResult();
static void closingBrace(StringBuilder declaration) {
declaration.append("\n}");
}
@Nullable String getSortableEntityClass() { @Nullable String getSortableEntityClass() {
return returnTypeName; return returnTypeName;
} }

View File

@ -64,6 +64,18 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
final StringBuilder declaration = new StringBuilder(); final StringBuilder declaration = new StringBuilder();
comment( declaration ); comment( declaration );
modifiers( declaration ); modifiers( declaration );
preamble( declaration, paramTypes );
nullChecks( paramTypes, declaration );
createQuery( declaration );
where( declaration, paramTypes );
orderBy( paramTypes, declaration );
executeQuery( declaration, paramTypes );
convertExceptions( declaration );
closingBrace( declaration );
return declaration.toString();
}
private void preamble(StringBuilder declaration, List<String> paramTypes) {
declaration declaration
.append(returnType()) .append(returnType())
.append(" ") .append(" ")
@ -71,15 +83,6 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
parameters(paramTypes, declaration); parameters(paramTypes, declaration);
declaration declaration
.append(" {"); .append(" {");
nullChecks(paramTypes, declaration);
createQuery(declaration);
where(declaration, paramTypes);
orderBy(paramTypes, declaration);
executeQuery(declaration, paramTypes);
convertExceptions( declaration );
declaration
.append("\n}");
return declaration.toString();
} }
private void executeQuery(StringBuilder declaration, List<String> paramTypes) { private void executeQuery(StringBuilder declaration, List<String> paramTypes) {
@ -161,14 +164,18 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
final String paramName = paramNames.get(i); final String paramName = paramNames.get(i);
final String paramType = paramTypes.get(i); final String paramType = paramTypes.get(i);
if ( !isNullable(i) && !isPrimitive(paramType) ) { if ( !isNullable(i) && !isPrimitive(paramType) ) {
nullCheck( declaration, paramName );
}
}
}
private static void nullCheck(StringBuilder declaration, String paramName) {
declaration declaration
.append("\n\tif (") .append("\n\tif (")
.append(paramName) .append(paramName)
.append(" == null) throw new IllegalArgumentException(\"Null \" + ") .append(" == null) throw new IllegalArgumentException(\"Null ")
.append(paramName) .append(paramName)
.append(");"); .append("\");");
}
}
} }
private void orderBy(List<String> paramTypes, StringBuilder declaration) { private void orderBy(List<String> paramTypes, StringBuilder declaration) {

View File

@ -8,12 +8,15 @@ package org.hibernate.jpamodelgen.annotation;
import java.util.List; import java.util.List;
import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive;
/** /**
* @author Gavin King * @author Gavin King
*/ */
public class IdFinderMethod extends AbstractFinderMethod { public class IdFinderMethod extends AbstractFinderMethod {
private final String paramName; private final String paramName;
private final String paramType;
public IdFinderMethod( public IdFinderMethod(
AnnotationMetaEntity annotationMetaEntity, AnnotationMetaEntity annotationMetaEntity,
@ -27,16 +30,18 @@ public class IdFinderMethod extends AbstractFinderMethod {
boolean dataRepository) { boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles, super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation, dataRepository ); paramNames, paramTypes, addNonnullAnnotation, dataRepository );
this.paramName = idParameterName( paramNames, paramTypes ); int idParameter = idParameter(paramNames, paramTypes);
this.paramName = paramNames.get(idParameter);
this.paramType = paramTypes.get(idParameter);
} }
private static String idParameterName(List<String> paramNames, List<String> paramTypes) { private static int idParameter(List<String> paramNames, List<String> paramTypes) {
for (int i = 0; i < paramNames.size(); i ++ ) { for (int i = 0; i < paramNames.size(); i ++ ) {
if ( !isSessionParameter( paramTypes.get(i) ) ) { if ( !isSessionParameter( paramTypes.get(i) ) ) {
return paramNames.get(i); return i;
} }
} }
return ""; // should never occur! return -1; // should never occur!
} }
@Override @Override
@ -54,6 +59,10 @@ public class IdFinderMethod extends AbstractFinderMethod {
final StringBuilder declaration = new StringBuilder(); final StringBuilder declaration = new StringBuilder();
comment( declaration ); comment( declaration );
preamble( declaration ); preamble( declaration );
if ( paramName != null && !isPrimitive(paramType) ) {
nullCheck( declaration, paramName );
}
tryReturn( declaration );
if ( fetchProfiles.isEmpty() ) { if ( fetchProfiles.isEmpty() ) {
findWithNoFetchProfiles( declaration ); findWithNoFetchProfiles( declaration );
} }
@ -61,7 +70,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
findWithFetchProfiles( declaration ); findWithFetchProfiles( declaration );
} }
convertExceptions( declaration ); convertExceptions( declaration );
declaration.append("\n}"); closingBrace( declaration );
return declaration.toString(); return declaration.toString();
} }
@ -94,4 +103,13 @@ public class IdFinderMethod extends AbstractFinderMethod {
.append("\n"); .append("\n");
} }
} }
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

@ -47,19 +47,30 @@ public class LifecycleMethod implements MetaAttribute {
@Override @Override
public String getAttributeDeclarationString() { public String getAttributeDeclarationString() {
StringBuilder declaration = new StringBuilder() StringBuilder declaration = new StringBuilder();
.append("\n@Override\npublic void ") preamble(declaration);
.append(methodName) nullCheck(declaration);
.append('('); declaration.append("\ttry {\n");
notNull( declaration ); delegateCall(declaration);
final boolean isInsert = operationName.equals("insert"); if ( operationName.equals("insert") ) {
convertException(declaration,
"jakarta.persistence.EntityExistsException",
"jakarta.data.exceptions.EntityExistsException");
}
else {
convertException(declaration,
"jakarta.persistence.OptimisticLockException",
"jakarta.data.exceptions.OptimisticLockingFailureException");
}
convertException(declaration,
"jakarta.persistence.PersistenceException",
"jakarta.data.exceptions.DataException");
declaration.append("}");
return declaration.toString();
}
private void delegateCall(StringBuilder declaration) {
declaration declaration
.append(annotationMetaEntity.importType(entity))
.append(' ')
.append(parameterName)
.append(')')
.append(" {\n")
.append("\ttry {\n")
.append("\t\t") .append("\t\t")
.append(sessionName) .append(sessionName)
.append('.') .append('.')
@ -68,27 +79,41 @@ public class LifecycleMethod implements MetaAttribute {
.append(parameterName) .append(parameterName)
.append(')') .append(')')
.append(";\n") .append(";\n")
.append("\t}\n") .append("\t}\n");
}
private void preamble(StringBuilder declaration) {
declaration
.append("\n@Override\npublic void ")
.append(methodName)
.append('(');
notNull(declaration);
declaration
.append(annotationMetaEntity.importType(entity))
.append(' ')
.append(parameterName)
.append(')')
.append(" {\n");
}
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 (") .append("\tcatch (")
.append(annotationMetaEntity.importType(isInsert .append(annotationMetaEntity.importType(exception))
? "jakarta.persistence.EntityExistsException"
: "jakarta.persistence.OptimisticLockException"))
.append(" exception) {\n") .append(" exception) {\n")
.append("\t\tthrow new ") .append("\t\tthrow new ")
.append(annotationMetaEntity.importType(isInsert .append(annotationMetaEntity.importType(convertedException))
? "jakarta.data.exceptions.EntityExistsException"
: "jakarta.data.exceptions.OptimisticLockingFailureException"))
.append("(exception);\n") .append("(exception);\n")
.append("\t}\n") .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);\n")
.append("\t}\n")
.append("}");
return declaration.toString();
} }
private void notNull(StringBuilder declaration) { private void notNull(StringBuilder declaration) {

View File

@ -47,6 +47,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
final StringBuilder declaration = new StringBuilder(); final StringBuilder declaration = new StringBuilder();
comment( declaration ); comment( declaration );
preamble( declaration ); preamble( declaration );
tryReturn( declaration );
unwrapSession( declaration ); unwrapSession( declaration );
if ( isReactive() ) { if ( isReactive() ) {
findReactively( declaration ); findReactively( declaration );
@ -55,7 +56,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
findBlockingly( declaration ); findBlockingly( declaration );
} }
convertExceptions( declaration ); convertExceptions( declaration );
declaration.append("\n}"); closingBrace( declaration );
return declaration.toString(); return declaration.toString();
} }
@ -83,8 +84,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
} }
private void findReactively(StringBuilder declaration) { private void findReactively(StringBuilder declaration) {
boolean composite = paramTypes.stream() boolean composite = isComposite();
.filter(type -> !isSessionParameter(type)).count()>1;
declaration declaration
.append(".find("); .append(".find(");
if (composite) { if (composite) {
@ -131,4 +131,8 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
declaration.append(");"); declaration.append(");");
} }
private boolean isComposite() {
return paramTypes.stream()
.filter(type -> !isSessionParameter(type)).count() > 1;
}
} }

View File

@ -80,31 +80,18 @@ public class QueryMethod extends AbstractQueryMethod {
final StringBuilder declaration = new StringBuilder(); final StringBuilder declaration = new StringBuilder();
comment( declaration ); comment( declaration );
modifiers( paramTypes, declaration ); modifiers( paramTypes, declaration );
declaration preamble( declaration, returnType, paramTypes );
.append(returnType) tryReturn( declaration );
.append(" ") castResult( declaration, returnType );
.append(methodName); createQuery( declaration );
parameters( paramTypes, declaration ); boolean unwrapped = setParameters( paramTypes, declaration );
declaration execute( declaration, unwrapped );
.append(" {\n"); convertExceptions( declaration );
if (dataRepository) { closingBrace( declaration );
declaration return declaration.toString();
.append("\ttry {\n\t");
}
declaration
.append("\t");
if ( returnTypeName == null || !returnTypeName.equals("void") ) {
declaration
.append("return ");
}
if ( isNative && returnTypeName != null && containerTypeName == null
&& isUsingEntityManager() ) {
// EntityManager.createNativeQuery() does not return TypedQuery,
// so we need to cast to the entity type
declaration.append("(")
.append(returnType)
.append(") ");
} }
private void createQuery(StringBuilder declaration) {
declaration declaration
.append(sessionName) .append(sessionName)
.append(isNative ? ".createNativeQuery" : ".createQuery") .append(isNative ? ".createNativeQuery" : ".createQuery")
@ -117,11 +104,40 @@ public class QueryMethod extends AbstractQueryMethod {
.append(".class"); .append(".class");
} }
declaration.append(")"); declaration.append(")");
boolean unwrapped = setParameters( paramTypes, declaration ); }
execute( declaration, unwrapped );
convertExceptions( declaration ); private void castResult(StringBuilder declaration, StringBuilder returnType) {
declaration.append("\n}"); if ( isNative && returnTypeName != null && containerTypeName == null
return declaration.toString(); && isUsingEntityManager() ) {
// EntityManager.createNativeQuery() does not return TypedQuery,
// so we need to cast to the entity type
declaration.append("(")
.append(returnType)
.append(") ");
}
}
private void tryReturn(StringBuilder declaration) {
if (dataRepository) {
declaration
.append("\ttry {\n\t");
}
declaration
.append("\t");
if ( returnTypeName == null || !returnTypeName.equals("void") ) {
declaration
.append("return ");
}
}
private void preamble(StringBuilder declaration, StringBuilder returnType, List<String> paramTypes) {
declaration
.append(returnType)
.append(" ")
.append(methodName);
parameters( paramTypes, declaration );
declaration
.append(" {\n");
} }
private void execute(StringBuilder declaration, boolean unwrapped) { private void execute(StringBuilder declaration, boolean unwrapped) {