Skip to content

Refactoring java executor to avoid instanceof at runtime #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl.executors.func;

import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.executors.CallableTask;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class JavaConsumerCallExecutor implements CallableTask<CallJava.CallJavaConsumer> {

private Consumer consumer;

public void init(
CallJava.CallJavaConsumer task,
Workflow workflow,
WorkflowApplication application,
ResourceLoader loader) {
consumer = task.consumer();
}

@Override
public CompletableFuture<WorkflowModel> apply(
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
consumer.accept(input.asJavaObject());
return CompletableFuture.completedFuture(input);
}

@Override
public boolean accept(Class<? extends TaskBase> clazz) {
return CallJava.CallJavaConsumer.class.isAssignableFrom(clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.serverlessworkflow.impl.executors.func;

import static io.serverlessworkflow.impl.executors.func.JavaCallExecutor.safeObject;
import static io.serverlessworkflow.impl.executors.func.JavaFuncUtils.safeObject;

import io.serverlessworkflow.api.types.ForTask;
import io.serverlessworkflow.api.types.Workflow;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl.executors.func;

import io.serverlessworkflow.impl.WorkflowModel;

public class JavaFuncUtils {

static Object safeObject(Object obj) {
return obj instanceof WorkflowModel model ? model.asJavaObject() : obj;
}

private JavaFuncUtils() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl.executors.func;

import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowModelFactory;
import io.serverlessworkflow.impl.executors.CallableTask;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public class JavaFunctionCallExecutor implements CallableTask<CallJava.CallJavaFunction> {

private Function function;

public void init(
CallJava.CallJavaFunction task,
Workflow workflow,
WorkflowApplication application,
ResourceLoader loader) {
function = task.function();
}

@Override
public CompletableFuture<WorkflowModel> apply(
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
return CompletableFuture.completedFuture(
modelFactory.fromAny(function.apply(input.asJavaObject())));
}

@Override
public boolean accept(Class<? extends TaskBase> clazz) {
return CallJava.CallJavaFunction.class.isAssignableFrom(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl.executors.func;

import static io.serverlessworkflow.impl.executors.func.JavaFuncUtils.safeObject;

import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowModelFactory;
import io.serverlessworkflow.impl.executors.CallableTask;
import io.serverlessworkflow.impl.expressions.LoopFunction;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.concurrent.CompletableFuture;

public class JavaLoopFunctionCallExecutor implements CallableTask<CallJava.CallJavaLoopFunction> {

private LoopFunction function;
private String varName;

public void init(
CallJava.CallJavaLoopFunction task,
Workflow workflow,
WorkflowApplication application,
ResourceLoader loader) {
function = task.function();
varName = task.varName();
}

@Override
public CompletableFuture<WorkflowModel> apply(
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
return CompletableFuture.completedFuture(
modelFactory.fromAny(
function.apply(
input.asJavaObject(), safeObject(taskContext.variables().get(varName)))));
}

@Override
public boolean accept(Class<? extends TaskBase> clazz) {

return CallJava.CallJavaLoopFunction.class.isAssignableFrom(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl.executors.func;

import static io.serverlessworkflow.impl.executors.func.JavaFuncUtils.safeObject;

import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowModelFactory;
import io.serverlessworkflow.impl.executors.CallableTask;
import io.serverlessworkflow.impl.expressions.LoopFunctionIndex;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.concurrent.CompletableFuture;

public class JavaLoopFunctionIndexCallExecutor
implements CallableTask<CallJava.CallJavaLoopFunctionIndex> {

private LoopFunctionIndex function;
private String varName;
private String indexName;

public void init(
CallJava.CallJavaLoopFunctionIndex task,
Workflow workflow,
WorkflowApplication application,
ResourceLoader loader) {
function = task.function();
varName = task.varName();
indexName = task.indexName();
}

@Override
public CompletableFuture<WorkflowModel> apply(
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();

return CompletableFuture.completedFuture(
modelFactory.fromAny(
function.apply(
input.asJavaObject(),
safeObject(taskContext.variables().get(varName)),
(Integer) safeObject(taskContext.variables().get(indexName)))));
}

@Override
public boolean accept(Class<? extends TaskBase> clazz) {
return CallJava.CallJavaLoopFunctionIndex.class.isAssignableFrom(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
io.serverlessworkflow.impl.executors.func.JavaCallExecutor
io.serverlessworkflow.impl.executors.func.JavaLoopFunctionIndexCallExecutor
io.serverlessworkflow.impl.executors.func.JavaLoopFunctionCallExecutor
io.serverlessworkflow.impl.executors.func.JavaFunctionCallExecutor
io.serverlessworkflow.impl.executors.func.JavaConsumerCallExecutor