44import  io .cucumber .core .backend .Container ;
55import  io .cucumber .core .backend .Glue ;
66import  io .cucumber .core .backend .Lookup ;
7- import  io .cucumber .core .exception .CucumberException ;
87import  io .cucumber .core .io .ClassFinder ;
98import  io .cucumber .core .io .ResourceLoader ;
109import  io .cucumber .core .io .ResourceLoaderClassFinder ;
11- import  io .cucumber .core .runtime .Invoker ;
1210import  io .cucumber .core .snippets .Snippet ;
1311
14- import  java .lang .annotation .Annotation ;
15- import  java .lang .reflect .Method ;
1612import  java .net .URI ;
1713import  java .util .List ;
1814
@@ -22,9 +18,7 @@ final class JavaBackend implements Backend {
2218
2319    private  final  Lookup  lookup ;
2420    private  final  Container  container ;
25- 
26-     private  final  MethodScanner  methodScanner ;
27-     private  Glue  glue ;
21+     private  final  ClassFinder  classFinder ;
2822
2923    JavaBackend (Lookup  lookup , Container  container , ResourceLoader  resourceLoader ) {
3024        this (lookup , container , new  ResourceLoaderClassFinder (resourceLoader , currentThread ().getContextClassLoader ()));
@@ -33,14 +27,20 @@ final class JavaBackend implements Backend {
3327    JavaBackend (Lookup  lookup , Container  container , ClassFinder  classFinder ) {
3428        this .lookup  = lookup ;
3529        this .container  = container ;
36-         this .methodScanner  = new   MethodScanner ( classFinder ) ;
30+         this .classFinder  = classFinder ;
3731    }
3832
3933    @ Override 
4034    public  void  loadGlue (Glue  glue , List <URI > gluePaths ) {
41-         this .glue  = glue ;
42-         // Scan for Java7 style glue (annotated methods) 
43-         methodScanner .scan (this , gluePaths );
35+         GlueAdaptor  glueAdaptor  = new  GlueAdaptor (lookup , glue );
36+         for  (URI  gluePath  : gluePaths ) {
37+             for  (Class <?> glueCodeClass  : classFinder .getDescendants (Object .class , gluePath )) {
38+                 MethodScanner .scan (glueCodeClass , (method , annotation ) -> {
39+                     container .addClass (method .getDeclaringClass ());
40+                     glueAdaptor .addDefinition (method , annotation );
41+                 });
42+             }
43+         }
4444    }
4545
4646    @ Override 
@@ -57,71 +57,4 @@ public void disposeWorld() {
5757    public  Snippet  getSnippet () {
5858        return  new  JavaSnippet ();
5959    }
60- 
61-     void  addStepDefinition (Annotation  annotation , Method  method ) {
62-         String  expression  = expression (annotation );
63-         long  timeoutMillis  = timeoutMillis (annotation );
64-         container .addClass (method .getDeclaringClass ());
65-         glue .addStepDefinition (new  JavaStepDefinition (method , expression , timeoutMillis , lookup ));
66-     }
67- 
68-     void  addHook (Annotation  annotation , Method  method ) {
69-         if  (container .addClass (method .getDeclaringClass ())) {
70-             if  (annotation .annotationType ().equals (Before .class )) {
71-                 Before  before  = (Before ) annotation ;
72-                 String  tagExpression  = before .value ();
73-                 long  timeout  = before .timeout ();
74-                 glue .addBeforeHook (new  JavaHookDefinition (method , tagExpression , before .order (), timeout , lookup ));
75-             } else  if  (annotation .annotationType ().equals (After .class )) {
76-                 After  after  = (After ) annotation ;
77-                 String  tagExpression  = after .value ();
78-                 long  timeout  = after .timeout ();
79-                 glue .addAfterHook (new  JavaHookDefinition (method , tagExpression , after .order (), timeout , lookup ));
80-             } else  if  (annotation .annotationType ().equals (BeforeStep .class )) {
81-                 BeforeStep  beforeStep  = (BeforeStep ) annotation ;
82-                 String  tagExpression  = beforeStep .value ();
83-                 long  timeout  = beforeStep .timeout ();
84-                 glue .addBeforeStepHook (new  JavaHookDefinition (method , tagExpression , beforeStep .order (), timeout , lookup ));
85-             } else  if  (annotation .annotationType ().equals (AfterStep .class )) {
86-                 AfterStep  afterStep  = (AfterStep ) annotation ;
87-                 String  tagExpression  = afterStep .value ();
88-                 long  timeout  = afterStep .timeout ();
89-                 glue .addAfterStepHook (new  JavaHookDefinition (method , tagExpression , afterStep .order (), timeout , lookup ));
90-             } else  if  (annotation .annotationType ().equals (ParameterType .class )) {
91-                 ParameterType  parameterType  = (ParameterType ) annotation ;
92-                 String  pattern  = parameterType .value ();
93-                 String  name  = parameterType .name ();
94-                 boolean  useForSnippets  = parameterType .useForSnippets ();
95-                 boolean  preferForRegexMatch  = parameterType .preferForRegexMatch ();
96-                 glue .addParameterType (new  JavaParameterTypeDefinition (name , pattern , method , useForSnippets , preferForRegexMatch , lookup ));
97-             } else  if  (annotation .annotationType ().equals (DataTableType .class )) {
98-                 glue .addDataTableType (new  JavaDataTableTypeDefinition (method , lookup ));
99-             } else  if  (annotation .annotationType ().equals (DefaultParameterTransformer .class )) {
100-                 glue .addDefaultParameterTransformer (new  JavaDefaultParameterTransformerDefinition (method , lookup ));
101-             } else  if  (annotation .annotationType ().equals (DefaultDataTableEntryTransformer .class )) {
102-                 glue .addDefaultDataTableEntryTransformer (new  JavaDefaultDataTableEntryTransformerDefinition (method , lookup ));
103-             } else  if  (annotation .annotationType ().equals (DefaultDataTableCellTransformer .class )) {
104-                 glue .addDefaultDataTableCellTransformer (new  JavaDefaultDataTableCellTransformerDefinition (method , lookup ));
105-             }
106-         }
107-     }
108- 
109-     private  String  expression (Annotation  annotation ) {
110-         try  {
111-             Method  expressionMethod  = annotation .getClass ().getMethod ("value" );
112-             return  (String ) Invoker .invoke (annotation , expressionMethod , 0 );
113-         } catch  (Throwable  e ) {
114-             throw  new  CucumberException (e );
115-         }
116-     }
117- 
118-     private  long  timeoutMillis (Annotation  annotation ) {
119-         try  {
120-             Method  regexpMethod  = annotation .getClass ().getMethod ("timeout" );
121-             return  (Long ) Invoker .invoke (annotation , regexpMethod , 0 );
122-         } catch  (Throwable  throwable ) {
123-             throw  new  CucumberException (throwable );
124-         }
125-     }
126- 
12760}
0 commit comments