HHH-17772 support for returning the argument from lifecycle methods

This commit is contained in:
Gavin King 2024-03-01 21:00:07 +01:00
parent ca12a4c874
commit 2beb85e695
3 changed files with 35 additions and 6 deletions

View File

@ -140,4 +140,10 @@ public interface BookAuthorRepository {
@Find @Find
List<Book> allBooksWithLotsOfSorting(Sort<? super Book> s1, Order<? super Book> order, Sort<? super Book>... s3); List<Book> allBooksWithLotsOfSorting(Sort<? super Book> s1, Order<? super Book> order, Sort<? super Book>... s3);
@Save
Book write(Book book);
@Insert
Iterable<Book> createAll(Iterable<Book> books);
} }

View File

@ -824,12 +824,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
else if ( returnType == null || returnType.getKind() != TypeKind.VOID ) { else if ( returnType == null ) {
context.message( method, context.message( method,
"must be declared 'void'", "must be declared 'void'",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
else { else {
final boolean returnArgument = returnType.getKind() != TypeKind.VOID;
final String operation = lifecycleOperation( method ); final String operation = lifecycleOperation( method );
final VariableElement parameter = method.getParameters().get(0); final VariableElement parameter = method.getParameters().get(0);
final TypeMirror parameterType = parameter.asType(); final TypeMirror parameterType = parameter.asType();
@ -844,6 +845,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
"incorrect parameter type '" + parameterType + "' is not annotated '@Entity'", "incorrect parameter type '" + parameterType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
else if ( returnArgument
&& !context.getTypeUtils().isSameType( returnType, parameterType ) ) {
context.message( parameter,
"return type '" + returnType
+ "' disagrees with parameter type '" + parameterType + "'",
Diagnostic.Kind.ERROR );
}
else { else {
putMember( putMember(
method.getSimpleName().toString() method.getSimpleName().toString()
@ -856,7 +864,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
getSessionVariableName(), getSessionVariableName(),
operation, operation,
context.addNonnullAnnotation(), context.addNonnullAnnotation(),
declaredType != parameterType declaredType != parameterType,
returnArgument
) )
); );
} }

View File

@ -18,6 +18,7 @@ public class LifecycleMethod implements MetaAttribute {
private final String operationName; private final String operationName;
private final boolean addNonnullAnnotation; private final boolean addNonnullAnnotation;
private final boolean iterateParameter; private final boolean iterateParameter;
private final boolean returnArgument;
public LifecycleMethod( public LifecycleMethod(
AnnotationMetaEntity annotationMetaEntity, AnnotationMetaEntity annotationMetaEntity,
@ -27,7 +28,8 @@ public class LifecycleMethod implements MetaAttribute {
String sessionName, String sessionName,
String operationName, String operationName,
boolean addNonnullAnnotation, boolean addNonnullAnnotation,
boolean iterateParameter) { boolean iterateParameter,
boolean returnArgument) {
this.annotationMetaEntity = annotationMetaEntity; this.annotationMetaEntity = annotationMetaEntity;
this.entity = entity; this.entity = entity;
this.methodName = methodName; this.methodName = methodName;
@ -36,6 +38,7 @@ public class LifecycleMethod implements MetaAttribute {
this.operationName = operationName; this.operationName = operationName;
this.addNonnullAnnotation = addNonnullAnnotation; this.addNonnullAnnotation = addNonnullAnnotation;
this.iterateParameter = iterateParameter; this.iterateParameter = iterateParameter;
this.returnArgument = returnArgument;
} }
@Override @Override
@ -55,6 +58,8 @@ public class LifecycleMethod implements MetaAttribute {
nullCheck(declaration); nullCheck(declaration);
declaration.append("\ttry {\n"); declaration.append("\ttry {\n");
delegateCall(declaration); delegateCall(declaration);
returnArgument(declaration);
declaration.append("\t}\n");
if ( operationName.equals("insert") ) { if ( operationName.equals("insert") ) {
convertException(declaration, convertException(declaration,
"jakarta.persistence.EntityExistsException", "jakarta.persistence.EntityExistsException",
@ -72,6 +77,15 @@ public class LifecycleMethod implements MetaAttribute {
return declaration.toString(); return declaration.toString();
} }
private void returnArgument(StringBuilder declaration) {
if ( returnArgument ) {
declaration
.append("\t\treturn ")
.append(parameterName)
.append(";\n");
}
}
private void delegateCall(StringBuilder declaration) { private void delegateCall(StringBuilder declaration) {
if ( iterateParameter ) { if ( iterateParameter ) {
declaration declaration
@ -92,13 +106,13 @@ public class LifecycleMethod implements MetaAttribute {
declaration declaration
.append("\t\t}\n"); .append("\t\t}\n");
} }
declaration
.append("\t}\n");
} }
private void preamble(StringBuilder declaration) { private void preamble(StringBuilder declaration) {
declaration declaration
.append("\n@Override\npublic void ") .append("\n@Override\npublic ")
.append(returnArgument ? annotationMetaEntity.importType(entity) : "void")
.append(' ')
.append(methodName) .append(methodName)
.append('('); .append('(');
notNull(declaration); notNull(declaration);