32
32
public final class ReflectionUtils {
33
33
private ReflectionUtils () {}
34
34
35
- public static Optional <Constructor <?>> getConstructor (
35
+ public static Optional <Constructor <?>> getWorkflowInitConstructor (
36
36
Class <?> clazz , List <Method > workflowMethod ) {
37
37
// We iterate through all constructors to find the one annotated with @WorkflowInit
38
38
// and check if it has the same parameters as the workflow method.
39
39
// 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.
42
41
Optional <Constructor <?>> workflowInit = Optional .empty ();
43
- Constructor <?> defaultConstructors = null ;
44
42
for (Constructor <?> ctor : clazz .getDeclaredConstructors ()) {
45
43
WorkflowInit wfInit = ctor .getAnnotation (WorkflowInit .class );
46
44
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
- }
56
45
continue ;
57
46
}
58
47
if (workflowMethod .size () != 1 ) {
59
48
throw new IllegalArgumentException (
60
49
"Multiple interfaces implemented while using @WorkflowInit annotation. Only one is allowed: "
61
50
+ clazz .getName ());
62
51
}
63
- if (workflowInit .isPresent () || defaultConstructors != null ) {
52
+ if (workflowInit .isPresent ()) {
64
53
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 : "
66
55
+ clazz .getName ());
67
56
}
68
57
if (!Modifier .isPublic (ctor .getModifiers ())) {
@@ -76,14 +65,25 @@ public static Optional<Constructor<?>> getConstructor(
76
65
}
77
66
workflowInit = Optional .of (ctor );
78
67
}
79
- if (!workflowInit .isPresent () && defaultConstructors == null ) {
80
- throw new IllegalArgumentException (
81
- "No default constructor or constructor annotated with @WorkflowInit found: "
82
- + clazz .getName ());
83
- }
84
68
return workflowInit ;
85
69
}
86
70
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
+
87
87
public static String getMethodNameForStackTraceCutoff (
88
88
Class <?> clazz , String methodName , Class <?>... parameterTypes ) throws RuntimeException {
89
89
try {
0 commit comments