3232public final class ReflectionUtils {
3333 private ReflectionUtils () {}
3434
35- public static Optional <Constructor <?>> getConstructor (
35+ public static Optional <Constructor <?>> getWorkflowInitConstructor (
3636 Class <?> clazz , List <Method > workflowMethod ) {
3737 // We iterate through all constructors to find the one annotated with @WorkflowInit
3838 // and check if it has the same parameters as the workflow method.
3939 // We check all declared constructors to find any constructors that are annotated with
40- // @WorkflowInit, but not public,
41- // to give a more informative error message.
40+ // @WorkflowInit, but are not public, to give a more informative error message.
4241 Optional <Constructor <?>> workflowInit = Optional .empty ();
43- Constructor <?> defaultConstructors = null ;
4442 for (Constructor <?> ctor : clazz .getDeclaredConstructors ()) {
4543 WorkflowInit wfInit = ctor .getAnnotation (WorkflowInit .class );
4644 if (wfInit == null ) {
47- if (ctor .getParameterCount () == 0 && Modifier .isPublic (ctor .getModifiers ())) {
48- if (workflowInit .isPresent () || defaultConstructors != null ) {
49- throw new IllegalArgumentException (
50- "Multiple constructors annotated with @WorkflowInit or a default constructor found: "
51- + clazz .getName ());
52- }
53- defaultConstructors = ctor ;
54- continue ;
55- }
5645 continue ;
5746 }
5847 if (workflowMethod .size () != 1 ) {
5948 throw new IllegalArgumentException (
6049 "Multiple interfaces implemented while using @WorkflowInit annotation. Only one is allowed: "
6150 + clazz .getName ());
6251 }
63- if (workflowInit .isPresent () || defaultConstructors != null ) {
52+ if (workflowInit .isPresent ()) {
6453 throw new IllegalArgumentException (
65- "Multiple constructors annotated with @WorkflowInit or a default constructor found : "
54+ "Multiple constructors annotated with @WorkflowInit found. Only one is allowed : "
6655 + clazz .getName ());
6756 }
6857 if (!Modifier .isPublic (ctor .getModifiers ())) {
@@ -76,14 +65,25 @@ public static Optional<Constructor<?>> getConstructor(
7665 }
7766 workflowInit = Optional .of (ctor );
7867 }
79- if (!workflowInit .isPresent () && defaultConstructors == null ) {
80- throw new IllegalArgumentException (
81- "No default constructor or constructor annotated with @WorkflowInit found: "
82- + clazz .getName ());
83- }
8468 return workflowInit ;
8569 }
8670
71+ public static Optional <Constructor <?>> getPublicDefaultConstructor (Class <?> clazz ) {
72+ Constructor <?> defaultConstructors = null ;
73+ for (Constructor <?> ctor : clazz .getDeclaredConstructors ()) {
74+ if (ctor .getParameterCount () != 0 ) {
75+ continue ;
76+ }
77+ if (!Modifier .isPublic (ctor .getModifiers ())) {
78+ throw new IllegalArgumentException (
79+ "Default constructor must be public: " + clazz .getName ());
80+ }
81+ defaultConstructors = ctor ;
82+ break ;
83+ }
84+ return Optional .ofNullable (defaultConstructors );
85+ }
86+
8787 public static String getMethodNameForStackTraceCutoff (
8888 Class <?> clazz , String methodName , Class <?>... parameterTypes ) throws RuntimeException {
8989 try {
0 commit comments