use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class ConstraintParser method getEqualityExpression.
private SpecialComparisonResult getEqualityExpression(TypedExpression left, TypedExpression right, BinaryExpr.Operator operator) {
if ((isAnyOperandBigDecimal(left, right) || isAnyOperandBigInteger(left, right)) && !isAnyOperandNullLiteral(left, right)) {
return compareBigDecimal(operator, left, right);
}
boolean isLeftNumber = isNumber(left);
boolean isRightNumber = isNumber(right);
String equalsMethod = isLeftNumber && isRightNumber ? "org.drools.modelcompiler.util.EvaluationUtil.areNumbersNullSafeEquals" : "org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals";
Expression leftExpr = left.uncastExpression();
Expression rightExpr = right.uncastExpression();
if (isLeftNumber) {
if (isString(right)) {
leftExpr = new BinaryExpr(new StringLiteralExpr(""), leftExpr, PLUS);
}
} else if (isRightNumber && isString(left)) {
rightExpr = new BinaryExpr(new StringLiteralExpr(""), rightExpr, PLUS);
}
MethodCallExpr methodCallExpr = new MethodCallExpr(null, equalsMethod);
// Avoid casts, by using an helper method we leverage autoboxing and equals
methodCallExpr.addArgument(leftExpr);
methodCallExpr.addArgument(rightExpr);
Expression expression = operator == BinaryExpr.Operator.EQUALS ? methodCallExpr : new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT);
return new SpecialComparisonResult(expression, left, right);
}
use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class ConstraintParser method compileToJavaRecursive.
private DrlxParseResult compileToJavaRecursive(Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional) {
boolean isEnclosed = false;
SimpleName bind = null;
if (drlxExpr instanceof FullyQualifiedInlineCastExpr) {
drlxExpr = transformFullyQualifiedInlineCastExpr(context.getTypeResolver(), (FullyQualifiedInlineCastExpr) drlxExpr);
}
while (drlxExpr instanceof EnclosedExpr) {
drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
isEnclosed = true;
}
if (drlxExpr instanceof DrlxExpression) {
bind = ((DrlxExpression) drlxExpr).getBind();
drlxExpr = ((DrlxExpression) drlxExpr).getExpr();
}
if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
}
if (drlxExpr instanceof BinaryExpr) {
DrlxParseResult result = parseBinaryExpr((BinaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional, isEnclosed);
if (result instanceof SingleDrlxParseSuccess && bind != null) {
((SingleDrlxParseSuccess) result).setExprBinding(bind.asString());
}
return result;
}
if (drlxExpr instanceof UnaryExpr) {
return parseUnaryExpr((UnaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional);
}
if (drlxExpr instanceof PointFreeExpr) {
return parsePointFreeExpr((PointFreeExpr) drlxExpr, patternType, bindingId, isPositional);
}
if (patternType == null && drlxExpr instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
if (functionCall.isPresent()) {
return parseFunctionInEval(methodCallExpr, patternType, bindingId, isPositional, functionCall);
}
}
if (drlxExpr instanceof FieldAccessExpr) {
return parseFieldAccessExpr((FieldAccessExpr) drlxExpr, patternType, bindingId);
}
String expression = constraint.getExpression();
if (drlxExpr instanceof DrlNameExpr) {
return parseNameExpr((DrlNameExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
}
if (drlxExpr instanceof OOPathExpr) {
return parseOOPathExpr((OOPathExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
}
if (drlxExpr instanceof LiteralExpr) {
Class<?> literalExpressionType = getLiteralExpressionType(((LiteralExpr) drlxExpr));
return new SingleDrlxParseSuccess(patternType, bindingId, drlxExpr, literalExpressionType).setIsPredicate(isBooleanBoxedUnboxed(literalExpressionType));
}
if (patternType != null) {
ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
Expression combo = left.getExpression();
Type exprType = left.getType();
boolean isPredicate = isBooleanBoxedUnboxed(exprType);
if (isPredicate) {
combo = combineExpressions(leftTypedExpressionResult, combo);
}
return new SingleDrlxParseSuccess(patternType, bindingId, combo, exprType).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setImplicitCastExpression(expressionTyperContext.getInlineCastExpression()).setNullSafeExpressions(expressionTyperContext.getNullSafeExpressions()).setIsPredicate(isPredicate);
} else {
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, null, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
return new SingleDrlxParseSuccess(null, bindingId, drlxExpr, left.getType()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setIsPredicate(true);
}
}
use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class Consequence method createCall.
public MethodCallExpr createCall(RuleDescr ruleDescr, String consequenceString, BlockStmt ruleVariablesBlock, boolean isBreaking) {
BlockStmt ruleConsequence = null;
if (context.getRuleDialect() == RuleContext.RuleDialect.JAVA) {
// for MVEL, it will be done in createExecuteCallMvel()
ruleConsequence = rewriteConsequence(consequenceString);
if (ruleConsequence != null) {
replaceKcontext(ruleConsequence);
rewriteChannels(ruleConsequence);
} else {
return null;
}
}
Set<String> usedDeclarationInRHS = extractUsedDeclarations(ruleConsequence, consequenceString);
Set<String> usedUnusableDeclarations = new HashSet<>(context.getUnusableOrBinding());
usedUnusableDeclarations.retainAll(usedDeclarationInRHS);
for (String s : usedUnusableDeclarations) {
context.addCompilationError(new InvalidExpressionErrorResult(String.format("%s cannot be resolved to a variable", s), Optional.of(context.getRuleDescr())));
}
MethodCallExpr onCall = onCall(usedDeclarationInRHS);
MethodCallExpr executeCall;
switch(context.getRuleDialect()) {
case JAVA:
rewriteReassignedDeclarations(ruleConsequence, usedDeclarationInRHS);
executeCall = executeCall(ruleVariablesBlock, ruleConsequence, usedDeclarationInRHS, onCall, Collections.emptySet());
break;
case MVEL:
executeCall = createExecuteCallMvel(consequenceString, ruleVariablesBlock, usedDeclarationInRHS, onCall);
break;
default:
throw new IllegalArgumentException("Unknown rule dialect " + context.getRuleDialect() + "!");
}
if (isBreaking) {
executeCall = new MethodCallExpr(executeCall, BREAKING_CALL);
}
return executeCall;
}
use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class DrlxParseUtil method getExpressionType.
public static java.lang.reflect.Type getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
if (expr instanceof LiteralExpr) {
return getLiteralExpressionType((LiteralExpr) expr);
}
if (expr instanceof UnaryExpr) {
return getExpressionType(context, typeResolver, expr.asUnaryExpr().getExpression(), usedDeclarations);
}
if (expr instanceof ArrayAccessExpr) {
return getClassFromContext(typeResolver, ((ArrayCreationExpr) ((ArrayAccessExpr) expr).getName()).getElementType().asString());
}
if (expr instanceof ArrayCreationExpr) {
return getClassFromContext(typeResolver, ((ArrayCreationExpr) expr).getElementType().asString());
}
if (expr instanceof MapCreationLiteralExpression) {
return Map.class;
}
if (expr instanceof ListCreationLiteralExpression) {
return List.class;
}
if (expr instanceof NameExpr) {
return expressionTypeNameExpr(context, usedDeclarations, ((NameExpr) expr).getNameAsString());
}
if (expr instanceof DrlNameExpr) {
return expressionTypeNameExpr(context, usedDeclarations, ((DrlNameExpr) expr).getNameAsString());
}
if (expr instanceof BinaryExpr) {
return boolean.class;
}
if (expr instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
Optional<Expression> scopeExpression = methodCallExpr.getScope();
if (scopeExpression.isPresent()) {
java.lang.reflect.Type scopeType = getExpressionType(context, typeResolver, scopeExpression.get(), usedDeclarations);
return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
} else {
throw new IllegalStateException("Scope expression is not present for " + ((MethodCallExpr) expr).getNameAsString() + "!");
}
}
if (expr instanceof ObjectCreationExpr) {
final ClassOrInterfaceType type = ((ObjectCreationExpr) expr).getType();
return getClassFromContext(typeResolver, type.asString());
}
if (expr.isCastExpr()) {
String typeName = expr.asCastExpr().getType().toString();
try {
return typeResolver.resolveType(expr.asCastExpr().getType().toString());
} catch (ClassNotFoundException e) {
context.addCompilationError(new InvalidExpressionErrorResult("Unknown type in cast expression: " + typeName));
throw new RuntimeException("Unknown type in cast expression: " + typeName);
}
}
if (expr instanceof ConditionalExpr) {
ConditionalExpr ternaryExpr = ((ConditionalExpr) expr);
java.lang.reflect.Type conditionType = getExpressionType(context, typeResolver, ternaryExpr.getCondition(), usedDeclarations);
if (conditionType != Boolean.class && conditionType != boolean.class) {
context.addCompilationError(new InvalidExpressionErrorResult("Condtion used in ternary expression '" + expr + "' isn't boolean"));
return Object.class;
}
java.lang.reflect.Type leftType = getExpressionType(context, typeResolver, ternaryExpr.getThenExpr(), usedDeclarations);
java.lang.reflect.Type rightType = getExpressionType(context, typeResolver, ternaryExpr.getElseExpr(), usedDeclarations);
Class<?> leftClass = toRawClass(leftType);
Class<?> rightClass = toRawClass(rightType);
if (leftClass.isAssignableFrom(rightClass)) {
return leftType;
}
if (rightClass.isAssignableFrom(leftClass)) {
return rightType;
}
return Object.class;
}
if (expr.isClassExpr()) {
return Class.class;
}
throw new RuntimeException("Unknown expression type: " + PrintUtil.printNode(expr));
}
use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class DrlxParseUtil method nameExprToMethodCallExpr.
public static TypedExpression nameExprToMethodCallExpr(String name, java.lang.reflect.Type type, Expression scope, RuleContext context) {
if (type == null) {
return null;
}
Class<?> clazz = toRawClass(type);
Method accessor = getAccessor(clazz, name, context);
if (accessor != null) {
MethodCallExpr body = new MethodCallExpr(scope, accessor.getName());
return new TypedExpression(body, accessor.getGenericReturnType());
} else {
// try parse it as inner class
for (Class<?> declaredClass : clazz.getClasses()) {
// An internal class has always a dot on the canonical name path
if (declaredClass.getCanonicalName().endsWith("." + name)) {
FieldAccessExpr fieldAccessExpr = new FieldAccessExpr(scope, name);
return new TypedExpression(fieldAccessExpr, declaredClass);
}
}
}
if (clazz.isArray() && name.equals("length")) {
FieldAccessExpr expr = new FieldAccessExpr(scope != null ? scope : new NameExpr(THIS_PLACEHOLDER), name);
return new TypedExpression(expr, int.class);
}
try {
Field field = clazz.getField(name);
if (scope == null) {
scope = new NameExpr(Modifier.isStatic(field.getModifiers()) ? clazz.getCanonicalName() : THIS_PLACEHOLDER);
}
FieldAccessExpr expr = new FieldAccessExpr(scope, name);
return new TypedExpression(expr, field.getType());
} catch (NoSuchFieldException e) {
// There's no field with the given name, return null and manage the problem on the caller
}
return null;
}
Aggregations