@@ -623,7 +623,7 @@ static MethodHandle lookupIterator(Class<?> receiverClass) {
623
623
624
624
public static boolean defToboolean (final Object value ) {
625
625
if (value instanceof Boolean ) {
626
- return (boolean ) value ;
626
+ return (boolean )value ;
627
627
} else {
628
628
throw new ClassCastException (
629
629
"cannot cast def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to boolean" );
@@ -853,11 +853,27 @@ public static double defTodoubleExplicit(final Object value) {
853
853
854
854
// Conversion methods for def to boxed types.
855
855
856
- public static Byte defToByteImplicit (final Object value ) {
856
+ public static Boolean defToBoolean (final Object value ) {
857
857
if (value == null ) {
858
858
return null ;
859
+ } else if (value instanceof Boolean ) {
860
+ return (Boolean )value ;
859
861
} else {
862
+ throw new ClassCastException ("cannot implicitly cast " +
863
+ "def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to " +
864
+ Boolean .class .getCanonicalName ());
865
+ }
866
+ }
867
+
868
+ public static Byte defToByteImplicit (final Object value ) {
869
+ if (value == null ) {
870
+ return null ;
871
+ } else if (value instanceof Byte ) {
860
872
return (Byte )value ;
873
+ } else {
874
+ throw new ClassCastException ("cannot implicitly cast " +
875
+ "def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to " +
876
+ Byte .class .getCanonicalName ());
861
877
}
862
878
}
863
879
@@ -866,16 +882,24 @@ public static Short defToShortImplicit(final Object value) {
866
882
return null ;
867
883
} else if (value instanceof Byte ) {
868
884
return (short )(byte )value ;
869
- } else {
885
+ } else if ( value instanceof Short ) {
870
886
return (Short )value ;
887
+ } else {
888
+ throw new ClassCastException ("cannot implicitly cast " +
889
+ "def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to " +
890
+ Short .class .getCanonicalName ());
871
891
}
872
892
}
873
893
874
894
public static Character defToCharacterImplicit (final Object value ) {
875
895
if (value == null ) {
876
896
return null ;
877
- } else {
897
+ } else if ( value instanceof Character ) {
878
898
return (Character )value ;
899
+ } else {
900
+ throw new ClassCastException ("cannot implicitly cast " +
901
+ "def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to " +
902
+ Character .class .getCanonicalName ());
879
903
}
880
904
}
881
905
@@ -888,8 +912,12 @@ public static Integer defToIntegerImplicit(final Object value) {
888
912
return (int )(short )value ;
889
913
} else if (value instanceof Character ) {
890
914
return (int )(char )value ;
891
- } else {
915
+ } else if ( value instanceof Integer ) {
892
916
return (Integer )value ;
917
+ } else {
918
+ throw new ClassCastException ("cannot implicitly cast " +
919
+ "def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to " +
920
+ Integer .class .getCanonicalName ());
893
921
}
894
922
}
895
923
@@ -904,8 +932,12 @@ public static Long defToLongImplicit(final Object value) {
904
932
return (long )(char )value ;
905
933
} else if (value instanceof Integer ) {
906
934
return (long )(int )value ;
907
- } else {
935
+ } else if ( value instanceof Long ) {
908
936
return (Long )value ;
937
+ } else {
938
+ throw new ClassCastException ("cannot implicitly cast " +
939
+ "def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to " +
940
+ Long .class .getCanonicalName ());
909
941
}
910
942
}
911
943
@@ -922,8 +954,12 @@ public static Float defToFloatImplicit(final Object value) {
922
954
return (float )(int )value ;
923
955
} else if (value instanceof Long ) {
924
956
return (float )(long )value ;
925
- } else {
957
+ } else if ( value instanceof Float ) {
926
958
return (Float )value ;
959
+ } else {
960
+ throw new ClassCastException ("cannot implicitly cast " +
961
+ "def [" + PainlessLookupUtility .typeToUnboxedType (value .getClass ()).getCanonicalName () + "] to " +
962
+ Float .class .getCanonicalName ());
927
963
}
928
964
}
929
965
@@ -942,8 +978,11 @@ public static Double defToDoubleImplicit(final Object value) {
942
978
return (double )(long )value ;
943
979
} else if (value instanceof Float ) {
944
980
return (double )(float )value ;
945
- } else {
981
+ } else if ( value instanceof Double ) {
946
982
return (Double )value ;
983
+ } else {
984
+ throw new ClassCastException ("cannot implicitly cast " +
985
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Double .class .getCanonicalName ());
947
986
}
948
987
}
949
988
@@ -952,8 +991,18 @@ public static Byte defToByteExplicit(final Object value) {
952
991
return null ;
953
992
} else if (value instanceof Character ) {
954
993
return (byte )(char )value ;
955
- } else {
994
+ } else if (
995
+ value instanceof Byte ||
996
+ value instanceof Short ||
997
+ value instanceof Integer ||
998
+ value instanceof Long ||
999
+ value instanceof Float ||
1000
+ value instanceof Double
1001
+ ) {
956
1002
return ((Number )value ).byteValue ();
1003
+ } else {
1004
+ throw new ClassCastException ("cannot explicitly cast " +
1005
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Byte .class .getCanonicalName ());
957
1006
}
958
1007
}
959
1008
@@ -962,8 +1011,18 @@ public static Short defToShortExplicit(final Object value) {
962
1011
return null ;
963
1012
} else if (value instanceof Character ) {
964
1013
return (short )(char )value ;
965
- } else {
1014
+ } else if (
1015
+ value instanceof Byte ||
1016
+ value instanceof Short ||
1017
+ value instanceof Integer ||
1018
+ value instanceof Long ||
1019
+ value instanceof Float ||
1020
+ value instanceof Double
1021
+ ) {
966
1022
return ((Number )value ).shortValue ();
1023
+ } else {
1024
+ throw new ClassCastException ("cannot explicitly cast " +
1025
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Short .class .getCanonicalName ());
967
1026
}
968
1027
}
969
1028
@@ -972,8 +1031,18 @@ public static Character defToCharacterExplicit(final Object value) {
972
1031
return null ;
973
1032
} else if (value instanceof Character ) {
974
1033
return (Character )value ;
975
- } else {
1034
+ } else if (
1035
+ value instanceof Byte ||
1036
+ value instanceof Short ||
1037
+ value instanceof Integer ||
1038
+ value instanceof Long ||
1039
+ value instanceof Float ||
1040
+ value instanceof Double
1041
+ ) {
976
1042
return (char )((Number )value ).intValue ();
1043
+ } else {
1044
+ throw new ClassCastException ("cannot explicitly cast " +
1045
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Character .class .getCanonicalName ());
977
1046
}
978
1047
}
979
1048
@@ -982,8 +1051,18 @@ public static Integer defToIntegerExplicit(final Object value) {
982
1051
return null ;
983
1052
} else if (value instanceof Character ) {
984
1053
return (int )(char )value ;
985
- } else {
1054
+ } else if (
1055
+ value instanceof Byte ||
1056
+ value instanceof Short ||
1057
+ value instanceof Integer ||
1058
+ value instanceof Long ||
1059
+ value instanceof Float ||
1060
+ value instanceof Double
1061
+ ) {
986
1062
return ((Number )value ).intValue ();
1063
+ } else {
1064
+ throw new ClassCastException ("cannot explicitly cast " +
1065
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Integer .class .getCanonicalName ());
987
1066
}
988
1067
}
989
1068
@@ -992,8 +1071,18 @@ public static Long defToLongExplicit(final Object value) {
992
1071
return null ;
993
1072
} else if (value instanceof Character ) {
994
1073
return (long )(char )value ;
995
- } else {
1074
+ } else if (
1075
+ value instanceof Byte ||
1076
+ value instanceof Short ||
1077
+ value instanceof Integer ||
1078
+ value instanceof Long ||
1079
+ value instanceof Float ||
1080
+ value instanceof Double
1081
+ ) {
996
1082
return ((Number )value ).longValue ();
1083
+ } else {
1084
+ throw new ClassCastException ("cannot explicitly cast " +
1085
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Long .class .getCanonicalName ());
997
1086
}
998
1087
}
999
1088
@@ -1002,8 +1091,18 @@ public static Float defToFloatExplicit(final Object value) {
1002
1091
return null ;
1003
1092
} else if (value instanceof Character ) {
1004
1093
return (float )(char )value ;
1005
- } else {
1094
+ } else if (
1095
+ value instanceof Byte ||
1096
+ value instanceof Short ||
1097
+ value instanceof Integer ||
1098
+ value instanceof Long ||
1099
+ value instanceof Float ||
1100
+ value instanceof Double
1101
+ ) {
1006
1102
return ((Number )value ).floatValue ();
1103
+ } else {
1104
+ throw new ClassCastException ("cannot explicitly cast " +
1105
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Float .class .getCanonicalName ());
1007
1106
}
1008
1107
}
1009
1108
@@ -1012,8 +1111,18 @@ public static Double defToDoubleExplicit(final Object value) {
1012
1111
return null ;
1013
1112
} else if (value instanceof Character ) {
1014
1113
return (double )(char )value ;
1015
- } else {
1114
+ } else if (
1115
+ value instanceof Byte ||
1116
+ value instanceof Short ||
1117
+ value instanceof Integer ||
1118
+ value instanceof Long ||
1119
+ value instanceof Float ||
1120
+ value instanceof Double
1121
+ ) {
1016
1122
return ((Number )value ).doubleValue ();
1123
+ } else {
1124
+ throw new ClassCastException ("cannot explicitly cast " +
1125
+ "def [" + value .getClass ().getCanonicalName () + "] to " + Double .class .getCanonicalName ());
1017
1126
}
1018
1127
}
1019
1128
0 commit comments