Skip to content

Commit

Permalink
[kie-issues#624] Fix coercing double type in Mvel compiler (#5550)
Browse files Browse the repository at this point in the history
(cherry picked from commit 67b5880)
  • Loading branch information
baldimir committed Oct 23, 2023
1 parent bd48064 commit 278726c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public TypedExpression visit(MethodCallExpr n, RHSPhase.Context arg) {
Optional<TypedExpression> scope = n.getScope().map(s -> s.accept(this, arg));
TypedExpression name = n.getName().accept(this, new RHSPhase.Context(scope.orElse(null)));
final List<TypedExpression> arguments = new ArrayList<>(n.getArguments().size());
for(Expression child : n.getArguments()) {
for (Expression child : n.getArguments()) {
TypedExpression a = child.accept(this, arg);
arguments.add(a);
}
Expand Down Expand Up @@ -92,15 +92,15 @@ private MethodCallExprT parseMethod(MethodCallExpr n,
.<Class<?>>map(ClassUtils::classFromType)
.map(scopeClazz -> MethodUtils.findMethod(scopeClazz, n.getNameAsString(), argumentsType));

if(method.isEmpty()) {
if (method.isEmpty()) {
method = mvelCompilerContext.getRootPattern()
.map(scopeClazz -> MethodUtils.findMethod(scopeClazz, n.getNameAsString(), argumentsType));
if(method.isPresent()) {
scope = mvelCompilerContext.createRootTypePrefix();
}
}

if(method.isEmpty()) {
if (method.isEmpty()) {
method = mvelCompilerContext.findStaticMethod(n.getNameAsString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.CastExpr;
import com.github.javaparser.ast.expr.CharLiteralExpr;
import com.github.javaparser.ast.expr.DoubleLiteralExpr;
import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.FieldAccessExpr;
Expand Down Expand Up @@ -61,6 +62,7 @@
import org.drools.mvelcompiler.ast.BooleanLiteralExpressionT;
import org.drools.mvelcompiler.ast.CastExprT;
import org.drools.mvelcompiler.ast.CharacterLiteralExpressionT;
import org.drools.mvelcompiler.ast.DoubleLiteralExpressionT;
import org.drools.mvelcompiler.ast.FieldAccessTExpr;
import org.drools.mvelcompiler.ast.FieldToAccessorTExpr;
import org.drools.mvelcompiler.ast.IntegerLiteralExpressionT;
Expand Down Expand Up @@ -329,6 +331,11 @@ public TypedExpression visit(IntegerLiteralExpr n, Context arg) {
return new IntegerLiteralExpressionT(n);
}

@Override
public TypedExpression visit(DoubleLiteralExpr n, Context arg) {
return new DoubleLiteralExpressionT(n);
}

@Override
public TypedExpression visit(CharLiteralExpr n, Context arg) {
return new CharacterLiteralExpressionT(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.DoubleLiteralExpr;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.LongLiteralExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.UnaryExpr;
import org.drools.mvel.parser.ast.visitor.DrlGenericVisitor;
import org.drools.mvelcompiler.ast.BigDecimalConvertedExprT;
import org.drools.mvelcompiler.ast.DoubleLiteralExpressionT;
import org.drools.mvelcompiler.ast.IntegerLiteralExpressionT;
import org.drools.mvelcompiler.ast.LongLiteralExpressionT;
import org.drools.mvelcompiler.ast.TypedExpression;
Expand Down Expand Up @@ -84,6 +86,11 @@ public Optional<TypedExpression> visit(IntegerLiteralExpr n, ReProcessRHSPhase.C
return convertWhenLHSISBigDecimal(() -> new IntegerLiteralExpressionT(n), context);
}

@Override
public Optional<TypedExpression> visit(DoubleLiteralExpr n, ReProcessRHSPhase.Context context) {
return convertWhenLHSISBigDecimal(() -> new DoubleLiteralExpressionT(n), context);
}

@Override
public Optional<TypedExpression> visit(LongLiteralExpr n, ReProcessRHSPhase.Context context) {
return convertWhenLHSISBigDecimal(() -> new LongLiteralExpressionT(n), context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.mvelcompiler.ast;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.DoubleLiteralExpr;

import java.lang.reflect.Type;
import java.util.Optional;

public class DoubleLiteralExpressionT implements TypedExpression {

private final DoubleLiteralExpr doubleLiteralExpr;

public DoubleLiteralExpressionT(DoubleLiteralExpr doubleLiteralExpr) {

this.doubleLiteralExpr = doubleLiteralExpr;
}

@Override
public Optional<Type> getType() {
return Optional.of(double.class);
}

@Override
public Node toJavaExpression() {
return doubleLiteralExpr;
}

@Override
public String toString() {
return "DoubleLiteralExpressionT{" +
"originalExpression=" + doubleLiteralExpr +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ public void testBigDecimalModInt() {
"$bd1.remainder(new java.math.BigDecimal(10), java.math.MathContext.DECIMAL128)");
}

@Test
public void testBigDecimalValueOfInteger() {
testExpression(c -> c.addDeclaration("$bdvalue", BigDecimal.class),
"$bdvalue + BigDecimal.valueOf(10)",
"$bdvalue.add(BigDecimal.valueOf(10), java.math.MathContext.DECIMAL128)");
}

@Test
public void testBigDecimalValueOfDouble() {
testExpression(c -> c.addDeclaration("$bdvalue", BigDecimal.class),
"$bdvalue + BigDecimal.valueOf(0.5)",
"$bdvalue.add(BigDecimal.valueOf(0.5), java.math.MathContext.DECIMAL128)");
}

public void testExpression(Consumer<MvelCompilerContext> testFunction,
String inputExpression,
String expectedResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,17 @@ public void testBigDecimalArithmetic() {
"}");
}

@Test
public void testBigDecimalArithmeticWithValueOfDouble() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.salary = $p.salary + BigDecimal.valueOf(0.5);\n" +
"}",
"{ " +
" $p.setSalary($p.getSalary().add(BigDecimal.valueOf(0.5), java.math.MathContext.DECIMAL128));\n" +
"}");
}

@Test
public void testBigDecimalArithmeticWithConversionLiteral() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
Expand Down

0 comments on commit 278726c

Please sign in to comment.