Skip to content

Commit

Permalink
Port static and jacoco synthetic methods fix from Activity interfaces…
Browse files Browse the repository at this point in the history
… to Workflows interfaces (temporalio#1356)

Issue temporalio#1331
  • Loading branch information
Spikhalskiy authored Aug 10, 2022
1 parent 3a2eab5 commit 38559eb
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import kotlin.reflect.jvm.javaMethod
* ```
*/
fun workflowName(workflowClass: Class<*>): String {
val workflowInterfaceMetadata = POJOWorkflowInterfaceMetadata.newInstance(workflowClass)
val workflowInterfaceMetadata = POJOWorkflowInterfaceMetadata.newStubInstance(workflowClass)
return workflowInterfaceMetadata.workflowType.orElse(null)
?: throw IllegalArgumentException("$workflowClass does not define a workflow method")
}
Expand Down Expand Up @@ -72,7 +72,7 @@ fun workflowQueryType(method: KFunction<*>): String {
private fun workflowMethodName(method: KFunction<*>, type: WorkflowMethodType): String {
val javaMethod = method.javaMethod
?: throw IllegalArgumentException("Invalid method reference $method")
val interfaceMetadata = POJOWorkflowInterfaceMetadata.newInstance(javaMethod.declaringClass)
val interfaceMetadata = POJOWorkflowInterfaceMetadata.newStubInstance(javaMethod.declaringClass)
val methodMetadata = interfaceMetadata.methodsMetadata.find { it.workflowMethod == javaMethod }
?: throw IllegalArgumentException("Not a workflow method reference $method")
if (methodMetadata.type != type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,14 @@ private static Set<Method> getActivityInterfaceMethods(
}

Method[] declaredMethods = current.getDeclaredMethods();
result.addAll(Arrays.asList(declaredMethods));
for (Method declaredMethod : declaredMethods) {
if (validateAndQualifiedForActivityMethod(declaredMethod)) {
result.add(declaredMethod);
}
}

if (isCurrentAnActivityInterface) {
result.stream()
.filter(POJOActivityInterfaceMetadata::validateAndQualifiedForActivityMethod)
.map(method -> new POJOActivityMethodMetadata(method, current, annotation))
.forEach(
methodMetadata ->
Expand Down Expand Up @@ -217,28 +220,32 @@ private static void validateModifierAccess(Class<?> activityInterface) {
/**
* @return true if the method should be used as an activity method, false if it shouldn't
* @throws IllegalArgumentException if the method is incorrectly configured (for example, a
* combination of ActivityMethod and a static modifier)
* combination of {@link ActivityMethod} and a {@code static} modifier)
*/
private static boolean validateAndQualifiedForActivityMethod(Method method) {
boolean isAnnotatedActivityMethod = method.getAnnotation(ActivityMethod.class) != null;

if (Modifier.isStatic(method.getModifiers())) {
if (method.getAnnotation(ActivityMethod.class) != null) {
if (isAnnotatedActivityMethod) {
throw new IllegalArgumentException(
"Method with @ActivityMethod annotation can't be static: " + method);
} else {
return false;
}
}

if (method.getAnnotation(ActivityMethod.class) != null) {
if (isAnnotatedActivityMethod) {
// all methods explicitly marked with ActivityMethod qualify
return true;
}

if (method.isSynthetic()) {
// if method is synthetic and not explicitly marked as an ActivityMethod,
// it's not qualified as an activity method.
// https://github.com/temporalio/sdk-java/issues/977
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ private POJOWorkflowImplMetadata(Class<?> implClass, boolean listener) {
POJOWorkflowInterfaceMetadata interfaceMetadata;
if (listener) {
interfaceMetadata =
POJOWorkflowInterfaceMetadata.newInstanceSkipWorkflowAnnotationCheck(anInterface);
POJOWorkflowInterfaceMetadata.newStubInstanceSkipWorkflowAnnotationCheck(anInterface);
} else {
interfaceMetadata = POJOWorkflowInterfaceMetadata.newImplementationInterface(anInterface);
interfaceMetadata = POJOWorkflowInterfaceMetadata.newImplementationInstance(anInterface);
}
workflowInterfaces.add(interfaceMetadata);
List<POJOWorkflowMethodMetadata> methods = interfaceMetadata.getMethodsMetadata();
Expand Down
Loading

0 comments on commit 38559eb

Please sign in to comment.