Skip to content

Commit cc0d641

Browse files
authored
Merge branch 'apache:main' into incubator-kie-issues#1748
2 parents 84a703e + 866a77a commit cc0d641

File tree

20 files changed

+1612
-69
lines changed

20 files changed

+1612
-69
lines changed

drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/CoercedExpression.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.Serializable;
2222
import java.math.BigDecimal;
23+
import java.math.BigInteger;
2324
import java.time.LocalDate;
2425
import java.time.LocalDateTime;
2526
import java.util.Arrays;
@@ -265,6 +266,7 @@ private static boolean shouldCoerceBToString(TypedExpression a, TypedExpression
265266
b.getType() != String.class &&
266267
b.getType() != Object.class &&
267268
b.getType() != BigDecimal.class &&
269+
b.getType() != BigInteger.class &&
268270
!(Map.class.isAssignableFrom(b.getRawClass())) &&
269271
!(b.getExpression() instanceof NullLiteralExpr) &&
270272
b.getType() != Serializable.class;

drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/expressiontyper/ExpressionTyper.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.Modifier;
2424
import java.lang.reflect.ParameterizedType;
2525
import java.math.BigDecimal;
26+
import java.math.BigInteger;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.HashMap;
@@ -92,7 +93,7 @@
9293
import org.drools.mvel.parser.printer.PrintUtil;
9394
import org.drools.mvelcompiler.CompiledExpressionResult;
9495
import org.drools.mvelcompiler.ConstraintCompiler;
95-
import org.drools.mvelcompiler.util.BigDecimalArgumentCoercion;
96+
import org.drools.mvelcompiler.util.BigNumberArgumentCoercion;
9697
import org.drools.util.ClassUtils;
9798
import org.drools.util.MethodUtils;
9899
import org.drools.util.Pair;
@@ -827,17 +828,23 @@ public static Expression convertArithmeticBinaryToMethodCall(BinaryExpr binaryEx
827828
}
828829

829830
/*
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.
831832
*/
832833
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));
834835
}
835836

836837
/*
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.
838839
*/
839840
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+
}
841848
}
842849

843850
private java.lang.reflect.Type getBinaryType(TypedExpression leftTypedExpression, TypedExpression rightTypedExpression, Operator operator) {
@@ -906,7 +913,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
906913
RuleContext.FunctionType typedDeclaredFunction = functionType.get();
907914
methodCallExpr.setScope(null);
908915

909-
promoteBigDecimalParameters(methodCallExpr, argsType, typedDeclaredFunction.getArgumentsType().toArray(new Class[0]));
916+
promoteBigNumberParameters(methodCallExpr, argsType, typedDeclaredFunction.getArgumentsType().toArray(new Class[0]));
910917

911918
return new TypedExpressionCursor(methodCallExpr, typedDeclaredFunction.getReturnType());
912919
}
@@ -917,7 +924,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
917924
}
918925

919926
Class<?>[] actualArgumentTypes = m.getParameterTypes();
920-
promoteBigDecimalParameters(methodCallExpr, argsType, actualArgumentTypes);
927+
promoteBigNumberParameters(methodCallExpr, argsType, actualArgumentTypes);
921928

922929
if (methodName.equals("get") && List.class.isAssignableFrom(rawClassCursor) && originalTypeCursor instanceof ParameterizedType) {
923930
return new TypedExpressionCursor(methodCallExpr, ((ParameterizedType) originalTypeCursor).getActualTypeArguments()[0]);
@@ -926,7 +933,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
926933
return new TypedExpressionCursor(methodCallExpr, actualTypeFromGenerics(originalTypeCursor, m.getGenericReturnType(), rawClassCursor));
927934
}
928935

929-
private void promoteBigDecimalParameters(MethodCallExpr methodCallExpr, Class[] argsType, Class<?>[] actualArgumentTypes) {
936+
private void promoteBigNumberParameters(MethodCallExpr methodCallExpr, Class[] argsType, Class<?>[] actualArgumentTypes) {
930937
if (actualArgumentTypes.length == argsType.length && actualArgumentTypes.length == methodCallExpr.getArguments().size()) {
931938
for (int i = 0; i < argsType.length; i++) {
932939
Class<?> argumentType = argsType[i];
@@ -938,7 +945,7 @@ private void promoteBigDecimalParameters(MethodCallExpr methodCallExpr, Class[]
938945
// unbind the original argumentExpression first, otherwise setArgument() will remove the argumentExpression from coercedExpression.childrenNodes
939946
// It will result in failing to find DrlNameExpr in AST at DrlsParseUtil.transformDrlNameExprToNameExpr()
940947
methodCallExpr.replace(argumentExpression, new NameExpr("placeholder"));
941-
Expression coercedExpression = new BigDecimalArgumentCoercion().coercedArgument(argumentType, actualArgumentType, argumentExpression);
948+
Expression coercedExpression = new BigNumberArgumentCoercion().coercedArgument(argumentType, actualArgumentType, argumentExpression);
942949
methodCallExpr.setArgument(i, coercedExpression);
943950
}
944951
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.drools.model.codegen.execmodel.bigintegertest;
20+
21+
import java.math.BigInteger;
22+
23+
public class BIFact {
24+
25+
private BigInteger value1;
26+
private BigInteger value2;
27+
28+
public BigInteger getValue1() {
29+
return value1;
30+
}
31+
32+
public void setValue1(BigInteger value1) {
33+
this.value1 = value1;
34+
}
35+
36+
public BigInteger getValue2() {
37+
return value2;
38+
}
39+
40+
public void setValue2(BigInteger value2) {
41+
this.value2 = value2;
42+
}
43+
}

0 commit comments

Comments
 (0)