use of org.drools.javaparser.ast.drlx.expr.InlineCastExpr in project drools by kiegroup.
the class ExpressionTyper method flattenScope.
private static List<Node> flattenScope(Expression expressionWithScope) {
List<Node> res = new ArrayList<>();
if (expressionWithScope instanceof FieldAccessExpr) {
FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) expressionWithScope;
res.addAll(flattenScope(fieldAccessExpr.getScope()));
res.add(fieldAccessExpr.getName());
} else if (expressionWithScope instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionWithScope;
if (methodCallExpr.getScope().isPresent()) {
res.addAll(flattenScope(methodCallExpr.getScope().get()));
}
res.add(methodCallExpr.setScope(null));
} else if (expressionWithScope instanceof InlineCastExpr && ((InlineCastExpr) expressionWithScope).getExpression() instanceof FieldAccessExpr) {
InlineCastExpr inlineCastExpr = (InlineCastExpr) expressionWithScope;
Expression internalScope = ((FieldAccessExpr) inlineCastExpr.getExpression()).getScope();
res.addAll(flattenScope((internalScope)));
res.add(expressionWithScope);
} else {
res.add(expressionWithScope);
}
return res;
}
use of org.drools.javaparser.ast.drlx.expr.InlineCastExpr in project drools by kiegroup.
the class ExpressionTyper method toTypedExpressionFromMethodCallOrField.
private TypedExpressionResult toTypedExpressionFromMethodCallOrField(Expression drlxExpr) {
final List<Node> childrenNodes = flattenScope(drlxExpr);
final Node firstChild = childrenNodes.get(0);
boolean isInLineCast = firstChild instanceof InlineCastExpr;
final Class<?> originalTypeCursor;
final Node firstNode;
if (isInLineCast) {
InlineCastExpr inlineCast = (InlineCastExpr) firstChild;
originalTypeCursor = originalTypeCursorFromInlineCast(inlineCast);
firstNode = inlineCast.getExpression();
} else {
originalTypeCursor = patternType;
firstNode = firstChild;
}
final Optional<TypedExpressionCursor> teCursor = processFirstNode(drlxExpr, childrenNodes, firstNode, isInLineCast, originalTypeCursor);
Expression previous;
Class<?> typeCursor;
if (!teCursor.isPresent()) {
return new TypedExpressionResult(Optional.empty(), context);
} else {
previous = teCursor.get().expressionCursor;
typeCursor = teCursor.get().typeCursor;
}
List<Node> childrenWithoutFirst = childrenNodes.subList(1, childrenNodes.size());
for (Node part : childrenWithoutFirst) {
if (typeCursor.isEnum()) {
previous = drlxExpr;
} else if (part instanceof SimpleName) {
String field = part.toString();
TypedExpression expression = nameExprToMethodCallExpr(field, typeCursor, previous);
typeCursor = expression.getType();
previous = expression.getExpression();
} else if (part instanceof MethodCallExpr) {
TypedExpressionCursor typedExpr = methodCallExpr((MethodCallExpr) part, typeCursor, previous);
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else if (part instanceof InlineCastExpr && ((InlineCastExpr) part).getExpression() instanceof FieldAccessExpr) {
InlineCastExpr inlineCastExprPart = (InlineCastExpr) part;
final FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) inlineCastExprPart.getExpression();
final TypedExpression toMethodCallExpr = nameExprToMethodCallExpr(fieldAccessExpr.getNameAsString(), typeCursor, previous);
final Class<?> castClass = getClassFromType(ruleContext.getTypeResolver(), inlineCastExprPart.getType());
previous = addCastToExpression(castClass, toMethodCallExpr.getExpression(), false);
} else {
throw new UnsupportedOperationException();
}
}
return new TypedExpressionResult(of(new TypedExpression().setExpression(previous).setType(typeCursor)), context);
}
Aggregations