cleanup hacks in repo method return type handling

This commit is contained in:
Gavin King 2024-03-18 14:37:42 +01:00
parent 30afa87434
commit 813ae8ed9d
1 changed files with 23 additions and 37 deletions

View File

@ -563,12 +563,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
*/ */
private static boolean isSessionGetter(ExecutableElement method) { private static boolean isSessionGetter(ExecutableElement method) {
if ( method.getParameters().isEmpty() ) { if ( method.getParameters().isEmpty() ) {
final TypeMirror type = ununi( method.getReturnType() ); final TypeMirror returnType = method.getReturnType();
if ( type.getKind() == TypeKind.DECLARED ) { if ( returnType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) type; final DeclaredType declaredType = (DeclaredType) ununi(returnType);
final Element element = declaredType.asElement(); final Element element = declaredType.asElement();
if ( element.getKind() == ElementKind.INTERFACE ) { if ( element.getKind() == ElementKind.INTERFACE ) {
final Name name = ((TypeElement) element).getQualifiedName(); final TypeElement typeElement = (TypeElement) element;
final Name name = typeElement.getQualifiedName();
return name.contentEquals(Constants.HIB_SESSION) return name.contentEquals(Constants.HIB_SESSION)
|| name.contentEquals(Constants.HIB_STATELESS_SESSION) || name.contentEquals(Constants.HIB_STATELESS_SESSION)
|| name.contentEquals(Constants.MUTINY_SESSION) || name.contentEquals(Constants.MUTINY_SESSION)
@ -682,13 +683,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
private void addQueryMethod(ExecutableElement method) { private void addQueryMethod(ExecutableElement method) {
final TypeMirror returnType = ununi( method.getReturnType() ); TypeMirror returnType = method.getReturnType();
final TypeKind kind = returnType.getKind(); final TypeKind kind = returnType.getKind();
if ( kind == TypeKind.VOID || kind == TypeKind.ARRAY || kind.isPrimitive() ) { if ( kind == TypeKind.VOID || kind == TypeKind.ARRAY || kind.isPrimitive() ) {
addQueryMethod( method, returnType, null ); addQueryMethod( method, returnType, null );
} }
else if ( kind == TypeKind.DECLARED ) { else if ( kind == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) returnType; final DeclaredType declaredType = (DeclaredType) ununi( returnType );
final TypeElement typeElement = (TypeElement) declaredType.asElement(); final TypeElement typeElement = (TypeElement) declaredType.asElement();
final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
switch ( typeArguments.size() ) { switch ( typeArguments.size() ) {
@ -764,13 +765,12 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
private static TypeMirror ununi(TypeMirror returnType) { private static TypeMirror ununi(TypeMirror returnType) {
if ( returnType.getKind() != TypeKind.DECLARED ) { if ( returnType.getKind() == TypeKind.DECLARED ) {
return returnType; final DeclaredType declaredType = (DeclaredType) returnType;
} final TypeElement typeElement = (TypeElement) declaredType.asElement();
final DeclaredType declaredType = (DeclaredType) returnType; if ( typeElement.getQualifiedName().contentEquals(Constants.UNI) ) {
final TypeElement typeElement = (TypeElement) declaredType.asElement(); return declaredType.getTypeArguments().get(0);
if ( typeElement.getQualifiedName().contentEquals( Constants.UNI ) ) { }
returnType = declaredType.getTypeArguments().get(0);
} }
return returnType; return returnType;
} }
@ -920,7 +920,6 @@ public class AnnotationMetaEntity extends AnnotationMeta {
ExecutableElement method, ExecutableElement method,
@Nullable TypeMirror returnType, @Nullable TypeMirror returnType,
@Nullable TypeElement containerType) { @Nullable TypeElement containerType) {
returnType = returnType != null ? ununi( returnType ) : null;
if ( returnType == null ) { if ( returnType == null ) {
context.message( method, context.message( method,
"missing return type", "missing return type",
@ -944,22 +943,22 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
else { else {
// multiple results, it has to be a criteria finder // multiple results, it has to be a criteria finder
createCriteriaFinder( method, returnType, containerType, entity ); createCriteriaFinder( method, arrayType.getComponentType(), "[]", entity );
} }
} }
} }
else if ( returnType.getKind() == TypeKind.DECLARED ) { else if ( returnType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) returnType; final DeclaredType declaredType = (DeclaredType) ununi( returnType );
final TypeElement entity = (TypeElement) declaredType.asElement(); final TypeElement entity = (TypeElement) declaredType.asElement();
if ( !containsAnnotation( entity, ENTITY ) ) { if ( !containsAnnotation( entity, ENTITY ) ) {
context.message( method, context.message( method,
"incorrect return type '" + returnType + "' is not annotated '@Entity'", "incorrect return type '" + declaredType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
else { else {
if ( containerType != null ) { if ( containerType != null ) {
// multiple results, it has to be a criteria finder // multiple results, it has to be a criteria finder
createCriteriaFinder( method, returnType, containerType, entity ); createCriteriaFinder( method, declaredType, containerType.toString(), entity );
} }
else { else {
for ( VariableElement parameter : method.getParameters() ) { for ( VariableElement parameter : method.getParameters() ) {
@ -980,10 +979,10 @@ public class AnnotationMetaEntity extends AnnotationMeta {
context.message( method, "missing parameter", Diagnostic.Kind.ERROR ); context.message( method, "missing parameter", Diagnostic.Kind.ERROR );
break; break;
case 1: case 1:
createSingleParameterFinder( method, returnType, entity ); createSingleParameterFinder( method, declaredType, entity );
break; break;
default: default:
createMultipleParameterFinder( method, returnType, entity ); createMultipleParameterFinder( method, declaredType, entity );
} }
} }
} }
@ -999,7 +998,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
* Create a finder method which returns multiple results. * Create a finder method which returns multiple results.
*/ */
private void createCriteriaFinder( private void createCriteriaFinder(
ExecutableElement method, TypeMirror returnType, @Nullable TypeElement containerType, TypeElement entity) { ExecutableElement method, TypeMirror returnType, @Nullable String containerType, TypeElement entity) {
final String methodName = method.getSimpleName().toString(); final String methodName = method.getSimpleName().toString();
final List<String> paramNames = parameterNames( method ); final List<String> paramNames = parameterNames( method );
final List<String> paramTypes = parameterTypes( method ); final List<String> paramTypes = parameterTypes( method );
@ -1027,25 +1026,12 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
} }
// TODO: this is ugly, do something better
// higher up in the call chain
final String containerTypeName;
final String entityTypeName;
if ( returnType.getKind() == TypeKind.ARRAY ) {
final ArrayType arrayType = (ArrayType) returnType;
entityTypeName = arrayType.getComponentType().toString();
containerTypeName = "[]";
}
else {
entityTypeName = returnType.toString();
containerTypeName = containerType == null ? null : containerType.toString();
}
putMember( methodKey, putMember( methodKey,
new CriteriaFinderMethod( new CriteriaFinderMethod(
this, this,
methodName, methodName,
entityTypeName, returnType.toString(),
containerTypeName, containerType,
paramNames, paramNames,
paramTypes, paramTypes,
parameterNullability(method, entity), parameterNullability(method, entity),
@ -1392,7 +1378,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return null; return null;
} }
// final String memberType = attributeType.toString(); // final String memberType = attributeType.toString();
// final String paramType = parameterType.toString(); // final String paramType = parameterType.toString();
// if ( !isLegalAssignment( paramType, memberType ) ) { // if ( !isLegalAssignment( paramType, memberType ) ) {
// context.message( param, // context.message( param,