Skip to content

Commit eb1b78f

Browse files
committed
allow binding Lists to 'in' condition parameters for @query method
required by Jakarta Data, and generally very useful! Signed-off-by: Gavin King <[email protected]>
1 parent 473965f commit eb1b78f

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,44 +2493,71 @@ private boolean checkReturnedEntity(EntityDomainType<?> model, TypeMirror return
24932493
private void checkParameter(
24942494
SqmParameter<?> param, List<String> paramNames, List<String> paramTypes,
24952495
ExecutableElement method, AnnotationMirror mirror, AnnotationValue value) {
2496-
final SqmExpressible<?> expressible = param.getExpressible();
2497-
final String queryParamType = expressible == null ? "unknown" : expressible.getTypeName(); //getTypeName() can return "unknown"
2498-
if ( param.getName() != null ) {
2499-
final String name = param.getName();
2500-
int index = paramNames.indexOf( name );
2501-
if ( index < 0 ) {
2502-
message( method, mirror, value,
2503-
"missing method parameter for query parameter :" + name
2504-
+ " (add a parameter '" + queryParamType + ' ' + name + "' to '" + method.getSimpleName() + "')",
2505-
Diagnostic.Kind.ERROR );
2496+
final SqmExpressible<?> expressible = param.getExpressible(); //same object as param.getAnticipatedType()
2497+
final String queryParamType = expressible == null ? null : expressible.getTypeName(); //getTypeName() can return "unknown"
2498+
if ( queryParamType!=null
2499+
//TODO: arguments of functions get assigned "unknown" which sucks
2500+
&& !"unknown".equals(queryParamType) ) {
2501+
if ( param.getName() != null ) {
2502+
checkNamedParameter(param, paramNames, paramTypes, method, mirror, value, queryParamType);
25062503
}
2507-
else if ( !isLegalAssignment( paramTypes.get(index), queryParamType ) ) {
2508-
message( method, mirror, value,
2509-
"parameter matching query parameter :" + name + " has the wrong type"
2510-
+ " (change the method parameter type to '" + queryParamType + "')",
2511-
Diagnostic.Kind.ERROR );
2504+
else if ( param.getPosition() != null ) {
2505+
checkOrdinalParameter(param, paramNames, paramTypes, method, mirror, value, queryParamType);
25122506
}
25132507
}
2514-
else if ( param.getPosition() != null ) {
2515-
int position = param.getPosition();
2516-
if ( position > paramNames.size() ) {
2517-
message( method, mirror, value,
2518-
"missing method parameter for query parameter ?" + position
2519-
+ " (add a parameter of type '" + queryParamType + "' to '" + method.getSimpleName() + "')",
2520-
Diagnostic.Kind.ERROR );
2521-
}
2522-
else if ( !isLegalAssignment( paramTypes.get(position-1), queryParamType ) ) {
2523-
message( method, mirror, value,
2508+
}
2509+
2510+
private void checkOrdinalParameter(
2511+
SqmParameter<?> param, List<String> paramNames, List<String> paramTypes, ExecutableElement method,
2512+
AnnotationMirror mirror, AnnotationValue value, String queryParamType) {
2513+
int position = param.getPosition();
2514+
if ( position > paramNames.size() ) {
2515+
message(method, mirror, value,
2516+
"missing method parameter for query parameter ?" + position
2517+
+ " (add a parameter of type '" + queryParamType + "' to '" + method.getSimpleName() + "')",
2518+
Diagnostic.Kind.ERROR);
2519+
}
2520+
else {
2521+
final String argType = paramTypes.get(position - 1);
2522+
if ( !isLegalAssignment(param, argType, queryParamType) ) {
2523+
message(method, mirror, value,
25242524
"parameter matching query parameter ?" + position + " has the wrong type"
25252525
+ " (change the method parameter type to '" + queryParamType + "')",
2526-
Diagnostic.Kind.ERROR );
2526+
Diagnostic.Kind.ERROR);
25272527
}
25282528
}
25292529
}
25302530

2531+
private void checkNamedParameter(
2532+
SqmParameter<?> param, List<String> paramNames, List<String> paramTypes, ExecutableElement method,
2533+
AnnotationMirror mirror, AnnotationValue value, String queryParamType) {
2534+
final String name = param.getName();
2535+
int index = paramNames.indexOf( name );
2536+
if ( index < 0 ) {
2537+
message(method, mirror, value,
2538+
"missing method parameter for query parameter :" + name
2539+
+ " (add a parameter '" + queryParamType + ' ' + name + "' to '" + method.getSimpleName() + "')",
2540+
Diagnostic.Kind.ERROR);
2541+
}
2542+
else {
2543+
final String argType = paramTypes.get(index);
2544+
if ( !isLegalAssignment(param, argType, queryParamType) ) {
2545+
message(method, mirror, value,
2546+
"parameter matching query parameter :" + name + " has the wrong type"
2547+
+ " (change the method parameter type to '" + queryParamType + "')",
2548+
Diagnostic.Kind.ERROR);
2549+
}
2550+
}
2551+
}
2552+
2553+
private static boolean isLegalAssignment(SqmParameter<?> param, String argType, String queryParamType) {
2554+
return param.allowMultiValuedBinding()
2555+
? isLegalAssignment(argType, LIST + '<' + queryParamType + '>')
2556+
: isLegalAssignment(argType, queryParamType);
2557+
}
2558+
25312559
private static boolean isLegalAssignment(String argType, String paramType) {
2532-
return paramType.equals("unknown")
2533-
|| paramType.equals(argType)
2560+
return paramType.equals(argType)
25342561
|| paramType.equals(fromPrimitive(argType));
25352562
}
25362563

0 commit comments

Comments
 (0)