fix typechecking of primitives in instantiations for query methods

This commit is contained in:
Gavin King 2024-02-11 18:46:56 +01:00
parent 70705f3e96
commit e5a994bfa1
6 changed files with 37 additions and 9 deletions

View File

@ -1495,7 +1495,7 @@ public class SemanticQueryBuilder<R> extends HqlParserBaseVisitor<Object> implem
final HqlParser.VariableContext variable = ctx.variable();
final String alias = variable == null ? null : extractAlias( variable );
final SqmSelectableNode<?> argExpression =
final SqmSelectableNode<?> argExpression =
(SqmSelectableNode<?>) ctx.instantiationArgumentExpression().accept( this );
final SqmDynamicInstantiationArgument<?> argument = new SqmDynamicInstantiationArgument<>(

View File

@ -164,6 +164,6 @@ public class Compatibility {
assert isFloatingTypePrimitive( to );
assert from.isPrimitive();
return to == float.class ? from == float.class : isFloatingTypePrimitive( from );
return to == float.class ? from == float.class : isFloatingTypePrimitive( from );
}
}

View File

@ -92,7 +92,7 @@ public class DynamicInstantiationAssemblerInjectionImpl<T> implements DomainResu
result = constructor.newInstance();
}
catch ( NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException
| java.lang.InstantiationException e ) {
| java.lang.InstantiationException e ) {
throw new InstantiationException( "Error instantiating class '"
+ target.getJavaType().getTypeName() + "' using default constructor: " + e.getMessage(), e );
}

View File

@ -35,8 +35,8 @@ public class InstantiationHelper {
return BeanInfoHelper.visitBeanInfo(
targetJavaType,
beanInfo -> {
for ( int i = 0; i < aliases.size(); i++ ) {
final String alias = aliases.get(i);
for ( int i = 0; i < aliases.size(); i++ ) {
final String alias = aliases.get(i);
final Class<?> argType = argTypes.get(i);
if ( !checkArgument( targetJavaType, beanInfo, alias, argType ) ) {
return false;
@ -78,7 +78,7 @@ public class InstantiationHelper {
? (Class<?>) parameterType
: typeConfiguration.getJavaTypeRegistry().resolveDescriptor( parameterType ).getJavaTypeClass();
if ( !areAssignmentCompatible( type, argumentType ) ) {
if ( !areAssignmentCompatible( type, argumentType ) ) {
if ( log.isDebugEnabled() ) {
log.debugf(
"Skipping constructor for dynamic-instantiation match due to argument mismatch [%s] : %s -> %s",

View File

@ -1260,8 +1260,10 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
private static boolean parameterMatches(VariableElement parameter, JpaSelection<?> item) {
final Class<?> itemType = item.getJavaType();
final TypeMirror parameterType = parameter.asType();
return parameterMatches( parameter.asType(), item.getJavaType() );
}
private static boolean parameterMatches(TypeMirror parameterType, Class<?> itemType) {
final TypeKind kind = parameterType.getKind();
final String itemTypeName = itemType.getName();
if ( kind == TypeKind.DECLARED ) {
@ -1270,7 +1272,30 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return paramTypeElement.getQualifiedName().contentEquals(itemTypeName);
}
else if ( kind.isPrimitive() ) {
return parameterType.toString().equals(itemTypeName);
switch ( kind ) {
case SHORT:
return itemType.equals(Short.class);
case INT:
return itemType.equals(Integer.class);
case LONG:
return itemType.equals(Long.class);
case BOOLEAN:
return itemType.equals(Boolean.class);
case FLOAT:
return itemType.equals(Float.class);
case DOUBLE:
return itemType.equals(Double.class);
case CHAR:
return itemType.equals(Character.class);
case BYTE:
return itemType.equals(Byte.class);
default:
return false;
}
}
else if ( kind == TypeKind.ARRAY ) {
return itemType.isArray()
&& parameterMatches( ((ArrayType) parameterType).getComponentType(), itemType.getComponentType() );
}
else {
return false;

View File

@ -51,4 +51,7 @@ public interface Dao {
@HQL("select new org.hibernate.jpamodelgen.test.hqlsql.Dto(title, pages) from Book")
List<Dto> dtoQuery();
@HQL("select title, pages from Book")
List<Dto> dtoQuery2();
}