HHH-17895 workaround for bug in Java 21

also be more careful about type annotations in comparing types

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-03-27 10:53:21 +01:00 committed by Christian Beikov
parent 6b9d6ef4fb
commit 09e7b6dd16
1 changed files with 68 additions and 37 deletions

View File

@ -1061,8 +1061,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
case JD_PAGE: case JD_PAGE:
case JD_CURSORED_PAGE: case JD_CURSORED_PAGE:
if ( method.getParameters().stream() if ( method.getParameters().stream()
.noneMatch(param -> param.asType().toString() .noneMatch(param -> typeNameEquals(param.asType(), JD_PAGE_REQUEST))) {
.startsWith(JD_PAGE_REQUEST))) {
message(method, message(method,
"method with return type '" + typeName "method with return type '" + typeName
+ "' has no parameter of type 'PageRequest'", + "' has no parameter of type 'PageRequest'",
@ -1074,8 +1073,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
case HIB_KEYED_RESULT_LIST: case HIB_KEYED_RESULT_LIST:
if ( method.getParameters().stream() if ( method.getParameters().stream()
.noneMatch(param -> param.asType().toString() .noneMatch(param -> typeNameEquals(param.asType(), HIB_KEYED_PAGE))) {
.startsWith(HIB_KEYED_PAGE))) {
message(method, message(method,
"method with return type '" + typeName "method with return type '" + typeName
+ "' has no parameter of type 'KeyedPage'", + "' has no parameter of type 'KeyedPage'",
@ -1205,7 +1203,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
else { else {
final String entity = parameterType.toString(); final String entity = typeAsString(parameterType);
final String methodName = method.getSimpleName().toString(); final String methodName = method.getSimpleName().toString();
putMember( putMember(
methodName + '.' + entity, methodName + '.' + entity,
@ -1351,7 +1349,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
else { else {
for ( VariableElement parameter : method.getParameters() ) { for ( VariableElement parameter : method.getParameters() ) {
final String type = parameter.asType().toString(); final String type = typeName(parameter.asType());
if ( isPageParam(type) ) { if ( isPageParam(type) ) {
message( parameter, "pagination would have no effect", Diagnostic.Kind.ERROR); message( parameter, "pagination would have no effect", Diagnostic.Kind.ERROR);
} }
@ -1403,9 +1401,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
multivalued.add( false ); multivalued.add( false );
final Types types = context.getTypeUtils(); final Types types = context.getTypeUtils();
final TypeMirror parameterType = parameterType( parameter ); final TypeMirror parameterType = parameterType( parameter );
final String type = parameterType.toString(); boolean pageRequest = typeNameEquals( parameterType, JD_PAGE_REQUEST );
boolean pageRequest = type.startsWith(JD_PAGE_REQUEST); if ( isOrderParam( typeName(parameterType) ) || pageRequest ) {
if ( isOrderParam(type) || pageRequest ) {
final TypeMirror typeArgument = getTypeArgument( parameterType ); final TypeMirror typeArgument = getTypeArgument( parameterType );
if ( typeArgument == null ) { if ( typeArgument == null ) {
missingTypeArgError( entity.getSimpleName().toString(), parameter, pageRequest ); missingTypeArgError( entity.getSimpleName().toString(), parameter, pageRequest );
@ -1548,37 +1545,39 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return getTypeArgument( arrayType.getComponentType() ); return getTypeArgument( arrayType.getComponentType() );
case DECLARED: case DECLARED:
final DeclaredType type = (DeclaredType) parameterType; final DeclaredType type = (DeclaredType) parameterType;
final String parameterTypeName = parameterType.toString(); switch ( typeName(parameterType) ) {
if ( parameterTypeName.startsWith( LIST ) ) { case LIST:
for (TypeMirror arg : type.getTypeArguments()) { for (TypeMirror arg : type.getTypeArguments()) {
return getTypeArgument( arg ); return getTypeArgument( arg );
}
}
else if ( parameterTypeName.startsWith( HIB_ORDER )
|| parameterTypeName.startsWith( JD_SORT )
|| parameterTypeName.startsWith( JD_ORDER )
|| parameterTypeName.startsWith( JD_PAGE_REQUEST ) ) {
for ( TypeMirror arg : type.getTypeArguments() ) {
switch ( arg.getKind() ) {
case WILDCARD:
return ((WildcardType) arg).getSuperBound();
case ARRAY:
case DECLARED:
case TYPEVAR:
return arg;
default:
return null;
} }
} return null;
case HIB_ORDER:
case JD_SORT:
case JD_ORDER:
case JD_PAGE_REQUEST:
for ( TypeMirror arg : type.getTypeArguments() ) {
switch ( arg.getKind() ) {
case WILDCARD:
return ((WildcardType) arg).getSuperBound();
case ARRAY:
case DECLARED:
case TYPEVAR:
return arg;
default:
return null;
}
}
return null;
default:
return null;
} }
return null;
default: default:
return null; return null;
} }
} }
private static boolean isFinderParameterMappingToAttribute(VariableElement param) { private static boolean isFinderParameterMappingToAttribute(VariableElement param) {
return !isSpecialParam( param.asType().toString() ); return !isSpecialParam(typeName(param.asType()));
} }
private String[] sessionTypeFromParameters(List<String> paramNames, List<String> paramTypes) { private String[] sessionTypeFromParameters(List<String> paramNames, List<String> paramTypes) {
@ -1824,7 +1823,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
else if ( containsAnnotation( param, PATTERN ) ) { else if ( containsAnnotation( param, PATTERN ) ) {
final AnnotationMirror mirror = getAnnotationMirror(param, PATTERN); final AnnotationMirror mirror = getAnnotationMirror(param, PATTERN);
if ( mirror!=null && !param.asType().toString().equals( String.class.getName() ) ) { if ( mirror!=null && !typeNameEquals(param.asType(), String.class.getName()) ) {
message( param, mirror, message( param, mirror,
"parameter annotated '@Pattern' is not of type 'String'", "parameter annotated '@Pattern' is not of type 'String'",
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
@ -2480,10 +2479,21 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private List<String> parameterTypes(ExecutableElement method) { private List<String> parameterTypes(ExecutableElement method) {
return method.getParameters().stream() return method.getParameters().stream()
.map(param -> parameterType(param).toString()) .map(param -> typeAsString(parameterType(param)))
.collect(toList()); .collect(toList());
} }
private String typeAsString(TypeMirror type) {
String result = type.toString();
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
result = result.replace(annotation.toString(), "");
}
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
result = annotation.toString() + ' ' + result;
}
return result;
}
private TypeMirror parameterType(VariableElement parameter) { private TypeMirror parameterType(VariableElement parameter) {
final ExecutableElement method = final ExecutableElement method =
(ExecutableElement) parameter.getEnclosingElement(); (ExecutableElement) parameter.getEnclosingElement();
@ -2614,9 +2624,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
for ( VariableElement parameter : method.getParameters() ) { for ( VariableElement parameter : method.getParameters() ) {
final TypeMirror parameterType = parameterType( parameter ); final TypeMirror parameterType = parameterType( parameter );
final TypeMirror typeArgument = getTypeArgument( parameterType ); final TypeMirror typeArgument = getTypeArgument( parameterType );
final String type = parameterType.toString(); final boolean pageRequest = typeNameEquals(parameterType, JD_PAGE_REQUEST);
final boolean pageRequest = type.startsWith(JD_PAGE_REQUEST); if ( isOrderParam( typeName(parameterType) ) || pageRequest ) {
if ( isOrderParam( type ) || pageRequest) {
if ( typeArgument == null ) { if ( typeArgument == null ) {
missingTypeArgError( returnType.toString(), parameter, pageRequest ); missingTypeArgError( returnType.toString(), parameter, pageRequest );
} }
@ -2628,6 +2637,28 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
private boolean typeNameEquals(TypeMirror parameterType, String typeName) {
if ( parameterType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) parameterType;
final TypeElement typeElement = (TypeElement) declaredType.asElement();
return typeElement.getQualifiedName().contentEquals(typeName);
}
else {
return false;
}
}
private static String typeName(TypeMirror parameterType) {
if ( parameterType.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) parameterType;
final TypeElement typeElement = (TypeElement) declaredType.asElement();
return typeElement.getQualifiedName().toString();
}
else {
return parameterType.toString();
}
}
private static boolean parameterIsMissing(String hql, int i, String param, String type) { private static boolean parameterIsMissing(String hql, int i, String param, String type) {
return !hasParameter(hql, i, param) && !isSpecialParam(type); return !hasParameter(hql, i, param) && !isSpecialParam(type);
} }