23
23
import java .lang .reflect .Modifier ;
24
24
import java .lang .reflect .ParameterizedType ;
25
25
import java .math .BigDecimal ;
26
+ import java .math .BigInteger ;
26
27
import java .util .ArrayList ;
27
28
import java .util .Arrays ;
28
29
import java .util .HashMap ;
92
93
import org .drools .mvel .parser .printer .PrintUtil ;
93
94
import org .drools .mvelcompiler .CompiledExpressionResult ;
94
95
import org .drools .mvelcompiler .ConstraintCompiler ;
95
- import org .drools .mvelcompiler .util .BigDecimalArgumentCoercion ;
96
+ import org .drools .mvelcompiler .util .BigNumberArgumentCoercion ;
96
97
import org .drools .util .ClassUtils ;
97
98
import org .drools .util .MethodUtils ;
98
99
import org .drools .util .Pair ;
@@ -827,17 +828,23 @@ public static Expression convertArithmeticBinaryToMethodCall(BinaryExpr binaryEx
827
828
}
828
829
829
830
/*
830
- * BigDecimal arithmetic operations should be converted to method calls. We may also apply this to BigInteger .
831
+ * BigDecimal/BigInteger arithmetic operations should be converted to method calls.
831
832
*/
832
833
public static boolean shouldConvertArithmeticBinaryToMethodCall (BinaryExpr .Operator operator , java .lang .reflect .Type leftType , java .lang .reflect .Type rightType ) {
833
- return isArithmeticOperator (operator ) && (leftType .equals (BigDecimal .class ) || rightType .equals (BigDecimal .class ));
834
+ return isArithmeticOperator (operator ) && (leftType .equals (BigDecimal .class ) || rightType .equals (BigDecimal .class ) || leftType . equals ( BigInteger . class ) || rightType . equals ( BigInteger . class ) );
834
835
}
835
836
836
837
/*
837
- * After arithmetic to method call conversion, BigDecimal should take precedence regardless of left or right. We may also apply this to BigInteger .
838
+ * After arithmetic to method call conversion, BigDecimal/BigInteger should take precedence regardless of left or right.
838
839
*/
839
840
public static java .lang .reflect .Type getBinaryTypeAfterConversion (java .lang .reflect .Type leftType , java .lang .reflect .Type rightType ) {
840
- return (leftType .equals (BigDecimal .class ) || rightType .equals (BigDecimal .class )) ? BigDecimal .class : leftType ;
841
+ if (leftType .equals (BigDecimal .class ) || rightType .equals (BigDecimal .class )) {
842
+ return BigDecimal .class ;
843
+ } else if (leftType .equals (BigInteger .class ) || rightType .equals (BigInteger .class )) {
844
+ return BigInteger .class ;
845
+ } else {
846
+ return leftType ;
847
+ }
841
848
}
842
849
843
850
private java .lang .reflect .Type getBinaryType (TypedExpression leftTypedExpression , TypedExpression rightTypedExpression , Operator operator ) {
@@ -906,7 +913,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
906
913
RuleContext .FunctionType typedDeclaredFunction = functionType .get ();
907
914
methodCallExpr .setScope (null );
908
915
909
- promoteBigDecimalParameters (methodCallExpr , argsType , typedDeclaredFunction .getArgumentsType ().toArray (new Class [0 ]));
916
+ promoteBigNumberParameters (methodCallExpr , argsType , typedDeclaredFunction .getArgumentsType ().toArray (new Class [0 ]));
910
917
911
918
return new TypedExpressionCursor (methodCallExpr , typedDeclaredFunction .getReturnType ());
912
919
}
@@ -917,7 +924,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
917
924
}
918
925
919
926
Class <?>[] actualArgumentTypes = m .getParameterTypes ();
920
- promoteBigDecimalParameters (methodCallExpr , argsType , actualArgumentTypes );
927
+ promoteBigNumberParameters (methodCallExpr , argsType , actualArgumentTypes );
921
928
922
929
if (methodName .equals ("get" ) && List .class .isAssignableFrom (rawClassCursor ) && originalTypeCursor instanceof ParameterizedType ) {
923
930
return new TypedExpressionCursor (methodCallExpr , ((ParameterizedType ) originalTypeCursor ).getActualTypeArguments ()[0 ]);
@@ -926,7 +933,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
926
933
return new TypedExpressionCursor (methodCallExpr , actualTypeFromGenerics (originalTypeCursor , m .getGenericReturnType (), rawClassCursor ));
927
934
}
928
935
929
- private void promoteBigDecimalParameters (MethodCallExpr methodCallExpr , Class [] argsType , Class <?>[] actualArgumentTypes ) {
936
+ private void promoteBigNumberParameters (MethodCallExpr methodCallExpr , Class [] argsType , Class <?>[] actualArgumentTypes ) {
930
937
if (actualArgumentTypes .length == argsType .length && actualArgumentTypes .length == methodCallExpr .getArguments ().size ()) {
931
938
for (int i = 0 ; i < argsType .length ; i ++) {
932
939
Class <?> argumentType = argsType [i ];
@@ -938,7 +945,7 @@ private void promoteBigDecimalParameters(MethodCallExpr methodCallExpr, Class[]
938
945
// unbind the original argumentExpression first, otherwise setArgument() will remove the argumentExpression from coercedExpression.childrenNodes
939
946
// It will result in failing to find DrlNameExpr in AST at DrlsParseUtil.transformDrlNameExprToNameExpr()
940
947
methodCallExpr .replace (argumentExpression , new NameExpr ("placeholder" ));
941
- Expression coercedExpression = new BigDecimalArgumentCoercion ().coercedArgument (argumentType , actualArgumentType , argumentExpression );
948
+ Expression coercedExpression = new BigNumberArgumentCoercion ().coercedArgument (argumentType , actualArgumentType , argumentExpression );
942
949
methodCallExpr .setArgument (i , coercedExpression );
943
950
}
944
951
}
0 commit comments