|
1 | 1 | package cucumber.io;
|
2 | 2 |
|
3 |
| -import cucumber.runtime.CucumberException; |
4 |
| -import cucumber.runtime.Utils; |
5 |
| - |
6 |
| -import java.io.File; |
7 |
| -import java.lang.annotation.Annotation; |
8 |
| -import java.lang.reflect.InvocationTargetException; |
9 |
| -import java.util.Collection; |
10 |
| -import java.util.HashSet; |
11 |
| - |
12 |
| -public class ResourceLoader { |
13 |
| - public Iterable<Resource> fileResources(String path, String suffix) { |
14 |
| - File root = new File(path); |
15 |
| - return new FileResourceIterable(root, root, suffix); |
16 |
| - } |
17 |
| - |
18 |
| - public Iterable<Resource> classpathResources(String path, String suffix) { |
19 |
| - return new ClasspathIterable(cl(), path, suffix); |
20 |
| - } |
21 |
| - |
22 |
| - public Collection<Class<? extends Annotation>> getAnnotations(String packagePath) { |
23 |
| - return getDescendants(Annotation.class, packagePath); |
24 |
| - } |
25 |
| - |
26 |
| - public <T> T instantiateExactlyOneSubclass(Class<T> parentType, String packagePath, Class[] constructorParams, Object[] constructorArgs) { |
27 |
| - Collection<? extends T> instances = instantiateSubclasses(parentType, packagePath, constructorParams, constructorArgs); |
28 |
| - if (instances.size() == 1) { |
29 |
| - return instances.iterator().next(); |
30 |
| - } else if (instances.size() == 0) { |
31 |
| - throw new CucumberException("Couldn't find a single implementation of " + parentType); |
32 |
| - } else { |
33 |
| - throw new CucumberException("Expected only one instance, but found too many: " + instances); |
34 |
| - } |
35 |
| - } |
36 |
| - |
37 |
| - public <T> Collection<? extends T> instantiateSubclasses(Class<T> parentType, String packagePath, Class[] constructorParams, Object[] constructorArgs) { |
38 |
| - Collection<T> result = new HashSet<T>(); |
39 |
| - for (Class<? extends T> clazz : getDescendants(parentType, packagePath)) { |
40 |
| - if(Utils.isInstantiable(clazz)) { |
41 |
| - result.add(newInstance(constructorParams, constructorArgs, clazz)); |
42 |
| - } |
43 |
| - } |
44 |
| - return result; |
45 |
| - } |
46 |
| - |
47 |
| - public <T> Collection<Class<? extends T>> getDescendants(Class<T> parentType, String packagePath) { |
48 |
| - Collection<Class<? extends T>> result = new HashSet<Class<? extends T>>(); |
49 |
| - for (Resource classResource : classpathResources(packagePath, ".class")) { |
50 |
| - String className = className(classResource.getPath()); |
51 |
| - Class<?> clazz = loadClass(className); |
52 |
| - if (clazz != null && !parentType.equals(clazz) && parentType.isAssignableFrom(clazz)) { |
53 |
| - result.add(clazz.asSubclass(parentType)); |
54 |
| - } |
55 |
| - } |
56 |
| - return result; |
57 |
| - } |
58 |
| - |
59 |
| - private Class<?> loadClass(String className) { |
60 |
| - try { |
61 |
| - return cl().loadClass(className); |
62 |
| - } catch (ClassNotFoundException ignore) { |
63 |
| - return null; |
64 |
| - } catch (NoClassDefFoundError ignore) { |
65 |
| - return null; |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - private <T> T newInstance(Class[] constructorParams, Object[] constructorArgs, Class<? extends T> clazz) { |
70 |
| - try { |
71 |
| - return clazz.getConstructor(constructorParams).newInstance(constructorArgs); |
72 |
| - } catch (InstantiationException e) { |
73 |
| - throw new CucumberException(e); |
74 |
| - } catch (IllegalAccessException e) { |
75 |
| - throw new CucumberException(e); |
76 |
| - } catch (InvocationTargetException e) { |
77 |
| - throw new CucumberException(e); |
78 |
| - } catch (NoSuchMethodException e) { |
79 |
| - throw new CucumberException(e); |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - private String className(String pathToClass) { |
84 |
| - return pathToClass.substring(0, pathToClass.length() - 6).replace("/", "."); |
85 |
| - } |
86 |
| - |
87 |
| - private ClassLoader cl() { |
88 |
| - return Thread.currentThread().getContextClassLoader(); |
89 |
| - } |
| 3 | +public interface ResourceLoader { |
| 4 | + Iterable<Resource> resources(String path, String suffix); |
90 | 5 | }
|
0 commit comments