Search in sources :

Example 1 with FEELCompilationError

use of org.kie.dmn.feel.codegen.feel11.FEELCompilationError in project drools by kiegroup.

the class FunctionDefs method asMethodCall.

public static Expression asMethodCall(String className, String methodSignature, List<String> params) {
    // creating a simple algorithm to find the method in java
    // without using any external libraries in this initial implementation
    // might need to explicitly use a classloader here
    String[] mp = FunctionDefNode.parseMethod(methodSignature);
    try {
        String methodName = mp[0];
        String[] paramTypeNames = FunctionDefNode.parseParams(mp[1]);
        ArrayList<Expression> paramExprs = new ArrayList<>();
        if (paramTypeNames.length == params.size()) {
            for (int i = 0; i < params.size(); i++) {
                String paramName = params.get(i);
                String paramTypeName = paramTypeNames[i];
                Type paramTypeCanonicalName = parseType(FunctionDefNode.getType(paramTypeName, null).getCanonicalName());
                Expression param = new CastExpr(paramTypeCanonicalName, new MethodCallExpr(null, "coerceTo", new NodeList<>(new ClassExpr(paramTypeCanonicalName), new MethodCallExpr(new NameExpr("feelExprCtx"), "getValue", new NodeList<>(new StringLiteralExpr(paramName))))));
                paramExprs.add(param);
            }
            return new MethodCallExpr(new NameExpr(className), methodName, new NodeList<>(paramExprs));
        } else {
            throw new FEELCompilationError(Msg.createMessage(Msg.ERROR_RESOLVING_EXTERNAL_FUNCTION_AS_DEFINED_BY, methodSignature));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : NodeList(com.github.javaparser.ast.NodeList) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) FEELCompilationError(org.kie.dmn.feel.codegen.feel11.FEELCompilationError) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)1 NodeList (com.github.javaparser.ast.NodeList)1 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 Type (com.github.javaparser.ast.type.Type)1 ArrayList (java.util.ArrayList)1 FEELCompilationError (org.kie.dmn.feel.codegen.feel11.FEELCompilationError)1