Search in sources :

Example 6 with ThrowStmt

use of com.github.javaparser.ast.stmt.ThrowStmt in project automatiko-engine by automatiko-io.

the class ServiceTaskDescriptor method classDeclaration.

protected ClassOrInterfaceDeclaration classDeclaration() {
    String unqualifiedName = StaticJavaParser.parseName(mangledName).removeQualifier().asString();
    ClassOrInterfaceDeclaration cls = new ClassOrInterfaceDeclaration().setName(unqualifiedName).setModifiers(Modifier.Keyword.PUBLIC).addImplementedType(WorkItemHandler.class.getCanonicalName());
    ClassOrInterfaceType serviceType = new ClassOrInterfaceType(null, interfaceName);
    FieldDeclaration serviceField = new FieldDeclaration().addVariable(new VariableDeclarator(serviceType, "service"));
    cls.addMember(serviceField);
    ClassOrInterfaceType completionHandlerType = new ClassOrInterfaceType(null, WorkItemExecutionManager.class.getCanonicalName());
    FieldDeclaration completionHandlerField = new FieldDeclaration().addVariable(new VariableDeclarator(completionHandlerType, "completionHandler"));
    cls.addMember(completionHandlerField);
    // executeWorkItem method
    BlockStmt executeWorkItemBody = new BlockStmt();
    MethodCallExpr callService = new MethodCallExpr(new NameExpr("service"), operationName);
    int counter = 0;
    for (Map.Entry<String, String> paramEntry : parameters.entrySet()) {
        MethodCallExpr getParamMethod = new MethodCallExpr(new NameExpr("workItem"), "getParameter").addArgument(new StringLiteralExpr(paramEntry.getKey()));
        if (isServerlessWorkflow) {
            String paramType = opTypes.size() > counter ? opTypes.get(counter).getCanonicalName() : paramEntry.getValue();
            MethodCallExpr extractValueMethod = new MethodCallExpr(new NameExpr("io.automatiko.engine.workflow.json.ValueExtractor"), "extract").addArgument(getParamMethod).addArgument(new NameExpr(paramType + ".class"));
            callService.addArgument(new CastExpr(new ClassOrInterfaceType(null, paramType), extractValueMethod));
        } else {
            callService.addArgument(new CastExpr(new ClassOrInterfaceType(null, paramEntry.getValue()), getParamMethod));
        }
        counter++;
    }
    MethodCallExpr completeWorkItem = completeWorkItem(executeWorkItemBody, callService);
    MethodCallExpr completionHandlerCompleteMethod = completeWorkItemViaHandler();
    IfStmt handleCompletion = new IfStmt(new BinaryExpr(new NameExpr("completionHandler"), new NullLiteralExpr(), BinaryExpr.Operator.EQUALS), new BlockStmt().addStatement(completeWorkItem), new BlockStmt().addStatement(completionHandlerCompleteMethod));
    executeWorkItemBody.addStatement(handleCompletion);
    if (implementation.equalsIgnoreCase("##webservice")) {
        BlockStmt catchbody = new BlockStmt();
        catchbody.addStatement(new ThrowStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError"), NodeList.nodeList(new MethodCallExpr(new NameExpr("wex"), "getMessage"), new MethodCallExpr(new NameExpr("String"), "valueOf").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("wex"), "getResponse"), "getStatus")), new MethodCallExpr(new NameExpr("io.automatiko.engine.services.utils.IoUtils"), "valueOf").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("wex"), "getResponse"), "getEntity"))))));
        CatchClause catchClause = new CatchClause(new Parameter(new ClassOrInterfaceType(null, "javax.ws.rs.WebApplicationException"), "wex"), catchbody);
        BlockStmt unavailablecatchbody = new BlockStmt();
        unavailablecatchbody.addStatement(new ThrowStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError"), NodeList.nodeList(new StringLiteralExpr("503"), new MethodCallExpr(new NameExpr("ex"), "getMessage"), new NameExpr("ex")))));
        CatchClause unavailablecatchClause = new CatchClause(new Parameter(new ClassOrInterfaceType(null, "javax.ws.rs.ProcessingException"), "ex"), unavailablecatchbody);
        TryStmt trystmt = new TryStmt(executeWorkItemBody, NodeList.nodeList(catchClause, unavailablecatchClause), null);
        executeWorkItemBody = new BlockStmt().addStatement(trystmt);
    }
    Set<String> handledErrorCodes = collectHandledErrorCodes();
    if (!handledErrorCodes.isEmpty()) {
        // add exception wrapper to handle errors that have error handlers attached
        BlockStmt runtimeCatchBody = new BlockStmt();
        MethodCallExpr wrapMethodCall = new MethodCallExpr(new NameExpr("io.automatiko.engine.workflow.ErrorMapper"), "wrap").addArgument(new NameExpr("rex"));
        for (String errorCode : handledErrorCodes) {
            wrapMethodCall.addArgument(new StringLiteralExpr(errorCode));
        }
        runtimeCatchBody.addStatement(new ThrowStmt(wrapMethodCall));
        CatchClause runtimeCatchClause = new CatchClause(new Parameter(new ClassOrInterfaceType(null, RuntimeException.class.getCanonicalName()), "rex"), runtimeCatchBody);
        TryStmt wrapperTryCatch = new TryStmt(executeWorkItemBody, NodeList.nodeList(runtimeCatchClause), null);
        executeWorkItemBody = new BlockStmt().addStatement(wrapperTryCatch);
    }
    MethodDeclaration executeWorkItem = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(void.class).setName("executeWorkItem").setBody(executeWorkItemBody).addParameter(WorkItem.class.getCanonicalName(), "workItem").addParameter(WorkItemManager.class.getCanonicalName(), "workItemManager");
    // abortWorkItem method
    BlockStmt abortWorkItemBody = new BlockStmt();
    MethodDeclaration abortWorkItem = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(void.class).setName("abortWorkItem").setBody(abortWorkItemBody).addParameter(WorkItem.class.getCanonicalName(), "workItem").addParameter(WorkItemManager.class.getCanonicalName(), "workItemManager");
    // getName method
    MethodDeclaration getName = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(String.class).setName("getName").setBody(new BlockStmt().addStatement(new ReturnStmt(new StringLiteralExpr(mangledName))));
    cls.addMember(executeWorkItem).addMember(abortWorkItem).addMember(getName);
    return cls;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) WorkItemExecutionManager(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionManager) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CastExpr(com.github.javaparser.ast.expr.CastExpr) WorkItemManager(io.automatiko.engine.api.runtime.process.WorkItemManager) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) TryStmt(com.github.javaparser.ast.stmt.TryStmt) CatchClause(com.github.javaparser.ast.stmt.CatchClause) WorkItemHandler(io.automatiko.engine.api.runtime.process.WorkItemHandler) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Parameter(com.github.javaparser.ast.body.Parameter) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ThrowStmt(com.github.javaparser.ast.stmt.ThrowStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 7 with ThrowStmt

use of com.github.javaparser.ast.stmt.ThrowStmt in project kogito-runtimes by kiegroup.

the class QueryEndpointGenerator method wrapBodyAddingExceptionLogging.

private BlockStmt wrapBodyAddingExceptionLogging(BlockStmt body, String nameURL) {
    TryStmt ts = new TryStmt();
    ts.setTryBlock(body);
    CatchClause cc = new CatchClause();
    String exceptionName = "e";
    cc.setParameter(new Parameter().setName(exceptionName).setType(Exception.class));
    BlockStmt cb = new BlockStmt();
    cb.addStatement(parseStatement(String.format("systemMetricsCollectorProvider.get().registerException(\"%s\", %s.getStackTrace()[0].toString());", nameURL, exceptionName)));
    cb.addStatement(new ThrowStmt(new NameExpr(exceptionName)));
    cc.setBody(cb);
    ts.setCatchClauses(new NodeList<>(cc));
    return new BlockStmt(new NodeList<>(ts));
}
Also used : TryStmt(com.github.javaparser.ast.stmt.TryStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) Parameter(com.github.javaparser.ast.body.Parameter) CatchClause(com.github.javaparser.ast.stmt.CatchClause) ThrowStmt(com.github.javaparser.ast.stmt.ThrowStmt) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with ThrowStmt

use of com.github.javaparser.ast.stmt.ThrowStmt in project kogito-runtimes by kiegroup.

the class RuleUnitContainerGenerator method factoryByIdBody.

private BlockStmt factoryByIdBody() {
    SwitchStmt switchStmt = new SwitchStmt();
    switchStmt.setSelector(new NameExpr("fqcn"));
    for (RuleUnitGenerator ruleUnit : ruleUnits) {
        SwitchEntry switchEntry = new SwitchEntry();
        switchEntry.getLabels().add(new StringLiteralExpr(ruleUnit.getRuleUnitDescription().getCanonicalName()));
        ObjectCreationExpr ruleUnitConstructor = new ObjectCreationExpr().setType(ruleUnit.targetCanonicalName()).addArgument("application");
        switchEntry.getStatements().add(new ReturnStmt(ruleUnitConstructor));
        switchStmt.getEntries().add(switchEntry);
    }
    SwitchEntry defaultEntry = new SwitchEntry();
    defaultEntry.getStatements().add(new ThrowStmt(new ObjectCreationExpr().setType(UnsupportedOperationException.class.getCanonicalName())));
    switchStmt.getEntries().add(defaultEntry);
    return new BlockStmt().addStatement(switchStmt);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ThrowStmt(com.github.javaparser.ast.stmt.ThrowStmt)

Aggregations

ThrowStmt (com.github.javaparser.ast.stmt.ThrowStmt)8 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)5 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 NodeList (com.github.javaparser.ast.NodeList)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 Parameter (com.github.javaparser.ast.body.Parameter)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 Expression (com.github.javaparser.ast.expr.Expression)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)2 CatchClause (com.github.javaparser.ast.stmt.CatchClause)2 IfStmt (com.github.javaparser.ast.stmt.IfStmt)2 SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)2