use of org.drools.mvel.parser.printer.PrintUtil.printNode in project drools by kiegroup.
the class ExpressionTyper method processFirstNode.
private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, java.lang.reflect.Type originalTypeCursor) {
Optional<TypedExpressionCursor> result;
if (isThisExpression(firstNode) || (firstNode instanceof DrlNameExpr && printNode(firstNode).equals(bindingId))) {
result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
} else if (firstNode instanceof DrlNameExpr) {
result = drlNameExpr(drlxExpr, (DrlNameExpr) firstNode, isInLineCast, originalTypeCursor);
} else if (firstNode instanceof NameExpr) {
result = drlNameExpr(drlxExpr, new DrlNameExpr(((NameExpr) firstNode).getName()), isInLineCast, originalTypeCursor);
} else if (firstNode instanceof FieldAccessExpr) {
if (((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
result = of(fieldAccessExpr(originalTypeCursor, ((FieldAccessExpr) firstNode).getName()));
} else {
try {
Class<?> resolvedType = ruleContext.getTypeResolver().resolveType(PrintUtil.printNode(firstNode));
result = of(new TypedExpressionCursor(new NameExpr(PrintUtil.printNode(firstNode)), resolvedType));
} catch (ClassNotFoundException e) {
result = empty();
}
}
} else if (firstNode instanceof NullSafeFieldAccessExpr && ((NullSafeFieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
result = of(fieldAccessExpr(originalTypeCursor, ((NullSafeFieldAccessExpr) firstNode).getName()));
} else if (firstNode instanceof MethodCallExpr) {
Optional<Expression> scopeExpr = ((MethodCallExpr) firstNode).getScope();
Optional<DeclarationSpec> scopeDecl = scopeExpr.flatMap(scope -> ruleContext.getDeclarationById(PrintUtil.printNode(scope)));
Expression scope;
java.lang.reflect.Type type;
if (scopeDecl.isPresent() && !scopeDecl.get().getBindingId().equals(bindingId)) {
type = scopeDecl.get().getDeclarationClass();
scope = new NameExpr(scopeDecl.get().getBindingId());
context.addUsedDeclarations(scopeDecl.get().getBindingId());
} else if (scopeExpr.isPresent()) {
TypedExpressionCursor parsedScope = processFirstNode(drlxExpr, childNodes, scopeExpr.get(), isInLineCast, originalTypeCursor).get();
type = parsedScope.typeCursor;
scope = parsedScope.expressionCursor;
} else {
type = originalTypeCursor;
scope = new NameExpr(THIS_PLACEHOLDER);
}
result = of(methodCallExpr((MethodCallExpr) firstNode, type, scope));
} else if (firstNode instanceof ObjectCreationExpr) {
result = of(objectCreationExpr((ObjectCreationExpr) firstNode));
} else if (firstNode instanceof StringLiteralExpr) {
result = of(stringLiteralExpr((StringLiteralExpr) firstNode));
} else if (firstNode instanceof EnclosedExpr) {
result = processFirstNode(drlxExpr, childNodes, ((EnclosedExpr) firstNode).getInner(), isInLineCast, originalTypeCursor);
} else if (firstNode instanceof CastExpr) {
result = castExpr((CastExpr) firstNode, drlxExpr, childNodes, isInLineCast, originalTypeCursor);
} else if (firstNode instanceof ArrayCreationExpr) {
result = of(arrayCreationExpr(((ArrayCreationExpr) firstNode)));
} else if (firstNode instanceof BinaryExpr) {
result = of(binaryExpr((BinaryExpr) firstNode));
} else if (firstNode instanceof ArrayAccessExpr) {
Optional<DeclarationSpec> scopeDecl = ruleContext.getDeclarationById(((ArrayAccessExpr) firstNode).getName().toString());
Expression scope;
java.lang.reflect.Type type;
if (scopeDecl.isPresent() && !scopeDecl.get().getBindingId().equals(bindingId)) {
type = scopeDecl.get().getDeclarationClass();
scope = new NameExpr(scopeDecl.get().getBindingId());
context.addUsedDeclarations(scopeDecl.get().getBindingId());
} else {
type = originalTypeCursor;
scope = new NameExpr(THIS_PLACEHOLDER);
}
result = arrayAccessExpr((ArrayAccessExpr) firstNode, type, scope);
} else if (firstNode instanceof MapCreationLiteralExpression) {
result = mapCreationLiteral((MapCreationLiteralExpression) firstNode, originalTypeCursor);
} else if (firstNode instanceof ListCreationLiteralExpression) {
result = listCreationLiteral((ListCreationLiteralExpression) firstNode, originalTypeCursor);
} else {
result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
}
if (result.isPresent()) {
processNullSafeDereferencing(drlxExpr);
}
return result.map(te -> {
if (isInLineCast) {
Expression exprWithInlineCast = addCastToExpression(toRawClass(te.typeCursor), te.expressionCursor, isInLineCast);
return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
} else {
return te;
}
});
}
Aggregations