Skip to content

Commit

Permalink
HHH-15454 correct get(alias, class) method in TupleImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
karge-itestra authored and beikov committed Aug 24, 2022
1 parent 70e2887 commit 254d695
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import jakarta.persistence.TupleElement;

import org.hibernate.internal.util.type.PrimitiveWrapperHelper;
import org.hibernate.query.JpaTuple;

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ public <X> X get(TupleElement<X> tupleElement) {
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 Down Expand Up @@ -73,7 +74,7 @@ public Object get(String alias) {
@SuppressWarnings("unchecked")
public <X> X get(int i, Class<X> type) {
final Object result = get( i );
if ( result != null && !type.isInstance( result ) ) {
if ( result != null && !elementTypeMatches( type, result ) ) {
throw new IllegalArgumentException(
String.format(
"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]",
Expand All @@ -96,6 +97,12 @@ public Object get(int i) {
return row[i];
}

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

@Override
public Object[] toArray() {
return row;
Expand Down

0 comments on commit 254d695

Please sign in to comment.