11
11
import java .util .Collection ;
12
12
import java .util .List ;
13
13
14
- public class ClasspathMethodScanner {
14
+ class ClasspathMethodScanner {
15
15
16
16
private final ClasspathResourceLoader resourceLoader ;
17
+ private final Collection <Class <? extends Annotation >> cucumberAnnotationClasses ;
17
18
18
19
public ClasspathMethodScanner (ClasspathResourceLoader resourceLoader ) {
19
20
this .resourceLoader = resourceLoader ;
21
+ cucumberAnnotationClasses = findCucumberAnnotationClasses ();
20
22
}
21
23
24
+ /**
25
+ * Registers step definitions and hooks.
26
+ *
27
+ * @param javaBackend the backend where stepdefs and hooks will be registered
28
+ * @param gluePaths where to look
29
+ */
22
30
public void scan (JavaBackend javaBackend , List <String > gluePaths ) {
23
- Collection <Class <? extends Annotation >> cucumberAnnotationClasses = findCucumberAnnotationClasses ();
24
31
for (String gluePath : gluePaths ) {
25
32
String packageName = gluePath .replace ('/' , '.' ).replace ('\\' , '.' ); // Sometimes the gluePath will be a path, not a package
26
33
for (Class <?> glueCodeClass : resourceLoader .getDescendants (Object .class , packageName )) {
@@ -30,30 +37,36 @@ public void scan(JavaBackend javaBackend, List<String> gluePaths) {
30
37
}
31
38
if (glueCodeClass != null ) {
32
39
for (Method method : glueCodeClass .getMethods ()) {
33
- scan (glueCodeClass , method , cucumberAnnotationClasses , javaBackend );
40
+ scan (javaBackend , method );
34
41
}
35
42
}
36
43
}
37
44
}
38
45
}
39
46
40
- private Collection <Class <? extends Annotation >> findCucumberAnnotationClasses () {
41
- return resourceLoader .getAnnotations ("cucumber.annotation" );
42
- }
43
-
44
- private void scan (Class <?> glueCodeClass , Method method , Collection <Class <? extends Annotation >> cucumberAnnotationClasses , JavaBackend javaBackend ) {
47
+ /**
48
+ * Registers step definitions and hooks.
49
+ *
50
+ * @param javaBackend the backend where stepdefs and hooks will be registered
51
+ * @param method a candidate for being a stepdef or hook
52
+ */
53
+ public void scan (JavaBackend javaBackend , Method method ) {
45
54
for (Class <? extends Annotation > cucumberAnnotationClass : cucumberAnnotationClasses ) {
46
55
Annotation annotation = method .getAnnotation (cucumberAnnotationClass );
47
56
if (annotation != null && !annotation .annotationType ().equals (Order .class )) {
48
57
if (isHookAnnotation (annotation )) {
49
- javaBackend .addHook (annotation , glueCodeClass , method );
58
+ javaBackend .addHook (annotation , method );
50
59
} else if (isStepdefAnnotation (annotation )) {
51
- javaBackend .addStepDefinition (annotation , glueCodeClass , method );
60
+ javaBackend .addStepDefinition (annotation , method );
52
61
}
53
62
}
54
63
}
55
64
}
56
65
66
+ private Collection <Class <? extends Annotation >> findCucumberAnnotationClasses () {
67
+ return resourceLoader .getAnnotations ("cucumber.annotation" );
68
+ }
69
+
57
70
private boolean isHookAnnotation (Annotation annotation ) {
58
71
Class <? extends Annotation > annotationClass = annotation .annotationType ();
59
72
return annotationClass .equals (Before .class ) || annotationClass .equals (After .class );
0 commit comments