Skip to content

Commit

Permalink
HHH-15454 correct get(alias, class) method in CriteriaQueryTupleTrans…
Browse files Browse the repository at this point in the history
…former

maybe the fix needs to be applied to NativeTupleImpl and TupleBuilderTransformer as well, but I do not have a testcase for that and do not know when they are used.
  • Loading branch information
karge-itestra authored and beikov committed Aug 23, 2022
1 parent 4b41584 commit 570969c
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.persistence.Tuple;
import javax.persistence.TupleElement;

import org.hibernate.internal.util.type.PrimitiveWrapperHelper;
import org.hibernate.query.criteria.internal.ValueHandlerFactory;
import org.hibernate.transform.BasicTransformerAdapter;

Expand Down Expand Up @@ -101,7 +102,7 @@ public Object get(String alias) {
public <X> X get(String alias, Class<X> type) {
final Object untyped = get( alias );
if ( untyped != null ) {
if ( !type.isInstance( untyped ) ) {
if (!elementTypeMatches(type, untyped)) {
throw new IllegalArgumentException(
String.format(
"Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]",
Expand All @@ -126,19 +127,27 @@ public Object get(int i) {

public <X> X get(int i, Class<X> type) {
final Object result = get( i );
if ( result != null && !type.isInstance( result ) ) {
throw new IllegalArgumentException(
String.format(
"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]",
i,
result.getClass().getName(),
type.getName()
)
);
if (result != null) {
if (!elementTypeMatches(type, result)) {
throw new IllegalArgumentException(
String.format(
"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]",
i,
result.getClass().getName(),
type.getName()
)
);
}
}
return (X) result;
}

private <X> boolean elementTypeMatches(Class<X> type, Object untyped) {
return type.isInstance(untyped)
|| type.isPrimitive()
&& PrimitiveWrapperHelper.getDescriptorByPrimitiveType(type).getWrapperClass().isInstance(untyped);
}

public Object[] toArray() {
// todo : make a copy?
return tuples;
Expand Down

0 comments on commit 570969c

Please sign in to comment.