HHH-18322 Immediately throw InstantiationException when resolving constructor for single null argument

This commit is contained in:
Čedomir Igaly 2024-07-03 12:41:37 +02:00 committed by Steve Ebersole
parent 8bec334ab2
commit 289ab0e6ca

View File

@ -9,7 +9,6 @@
import org.hibernate.sql.results.spi.RowTransformer; import org.hibernate.sql.results.spi.RowTransformer;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.hibernate.query.sqm.SqmExpressible; import org.hibernate.query.sqm.SqmExpressible;
@ -33,6 +32,10 @@ public RowTransformerConstructorImpl(Class<T> type, TupleMetadata tupleMetadata)
for (int i = 0; i < elements.size(); i++) { for (int i = 0; i < elements.size(); i++) {
sig[i] = resolveElementJavaType( elements.get( i ) ); sig[i] = resolveElementJavaType( elements.get( i ) );
} }
if ( sig.length == 1 && sig[0] == null ) {
// Can not (properly) resolve constructor for single null element
throw new InstantiationException( "Cannot instantiate query result type, argument types are unknown ", type );
}
try { try {
constructor = findMatchingConstructor( type, sig ); constructor = findMatchingConstructor( type, sig );
constructor.setAccessible( true ); constructor.setAccessible( true );
@ -54,9 +57,7 @@ private static Class<?> resolveElementJavaType(TupleElement<?> element) {
return element.getJavaType(); return element.getJavaType();
} }
private Constructor<T> findMatchingConstructor( private Constructor<T> findMatchingConstructor(Class<T> type, Class<?>[] sig) throws Exception {
Class<T> type,
Class<?>[] sig) throws NoSuchMethodException {
try { try {
return type.getDeclaredConstructor( sig ); return type.getDeclaredConstructor( sig );
} }
@ -82,7 +83,7 @@ private Constructor<T> findMatchingConstructor(
return (Constructor<T>) constructor; return (Constructor<T>) constructor;
} }
} }
throw new NoSuchMethodException(); throw e;
} }
} }