Skip to content

Commit 2be7461

Browse files
committed
HHH-17155 More precise primitive type assignment compatibility check
1 parent 8347d19 commit 2be7461

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/Compatibility.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ else if ( to == byte.class ) {
102102
}
103103
else if ( isIntegralTypePrimitive( to ) ) {
104104
return from == byte.class
105-
|| isIntegralTypePrimitive( from )
105+
|| isCompatibleIntegralTypePrimitive( to, from )
106106
// this would for sure cause loss of precision
107107
|| isFloatingTypePrimitive( from );
108108
}
109109
else if ( isFloatingTypePrimitive( to ) ) {
110110
return from == byte.class
111111
|| isIntegralTypePrimitive( from )
112-
|| isFloatingTypePrimitive( from );
112+
|| isCompatibleFloatingTypePrimitive( to, from );
113113
}
114114

115115
return false;
@@ -133,6 +133,22 @@ private static boolean isIntegralTypePrimitive(Class potentialIntegral) {
133133
|| potentialIntegral == long.class;
134134
}
135135

136+
private static boolean isCompatibleIntegralTypePrimitive(Class to, Class from) {
137+
assert isIntegralTypePrimitive( to );
138+
assert from.isPrimitive();
139+
140+
if ( to == short.class ) {
141+
return from == short.class;
142+
}
143+
else if ( to == int.class ) {
144+
return from == short.class
145+
|| from == int.class;
146+
}
147+
else {
148+
return isIntegralTypePrimitive( from );
149+
}
150+
}
151+
136152
public static boolean isFloatingType(Class potentialFloating) {
137153
if ( potentialFloating.isPrimitive() ) {
138154
return isFloatingTypePrimitive( potentialFloating );
@@ -150,6 +166,18 @@ private static boolean isFloatingTypePrimitive(Class potentialFloating) {
150166
|| potentialFloating == double.class;
151167
}
152168

169+
private static boolean isCompatibleFloatingTypePrimitive(Class to, Class from) {
170+
assert isFloatingTypePrimitive( to );
171+
assert from.isPrimitive();
172+
173+
if ( to == float.class ) {
174+
return from == float.class;
175+
}
176+
else {
177+
return isFloatingTypePrimitive( from );
178+
}
179+
}
180+
153181
public static boolean areAssignmentCompatible(
154182
JavaType to,
155183
JavaType from) {

0 commit comments

Comments
 (0)