Search in sources :

Example 1 with ArrayAccessExpr

use of org.drools.javaparser.ast.expr.ArrayAccessExpr in project drools by kiegroup.

the class DrlxParseUtil method getExpressionType.

public static Class<?> getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
    if (expr instanceof LiteralExpr) {
        return getLiteralExpressionType((LiteralExpr) expr);
    }
    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 NameExpr) {
        String name = ((NameExpr) expr).getNameAsString();
        if (usedDeclarations != null) {
            usedDeclarations.add(name);
        }
        return context.getDeclarationById(name).map(DeclarationSpec::getDeclarationClass).get();
    }
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
        Class<?> scopeType = getExpressionType(context, typeResolver, methodCallExpr.getScope().get(), usedDeclarations);
        return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
    }
    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);
        }
    }
    throw new RuntimeException("Unknown expression type: " + expr);
}
Also used : ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ArrayAccessExpr(org.drools.javaparser.ast.expr.ArrayAccessExpr) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) LongLiteralExpr(org.drools.javaparser.ast.expr.LongLiteralExpr) CharLiteralExpr(org.drools.javaparser.ast.expr.CharLiteralExpr) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) DoubleLiteralExpr(org.drools.javaparser.ast.expr.DoubleLiteralExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

ArrayAccessExpr (org.drools.javaparser.ast.expr.ArrayAccessExpr)1 ArrayCreationExpr (org.drools.javaparser.ast.expr.ArrayCreationExpr)1 BooleanLiteralExpr (org.drools.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (org.drools.javaparser.ast.expr.CharLiteralExpr)1 DoubleLiteralExpr (org.drools.javaparser.ast.expr.DoubleLiteralExpr)1 IntegerLiteralExpr (org.drools.javaparser.ast.expr.IntegerLiteralExpr)1 LiteralExpr (org.drools.javaparser.ast.expr.LiteralExpr)1 LongLiteralExpr (org.drools.javaparser.ast.expr.LongLiteralExpr)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 NameExpr (org.drools.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (org.drools.javaparser.ast.expr.NullLiteralExpr)1 ObjectCreationExpr (org.drools.javaparser.ast.expr.ObjectCreationExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)1 InvalidExpressionErrorResult (org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult)1 ExpressionTyper.findLeftLeafOfNameExpr (org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr)1