forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove duplication in Reflections implementations.
Renamed Reflections to ClassFinder and removed generic methods. Created a new Reflections class that delegates to a ClassFinder.
- Loading branch information
1 parent
022f12f
commit cb3a4cd
Showing
17 changed files
with
180 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package cucumber.runtime; | ||
|
||
import java.util.Collection; | ||
|
||
public interface ClassFinder { | ||
<T> Collection<Class<? extends T>> getDescendants(Class<T> parentType, String packageName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cucumber.runtime; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
|
||
public class Reflections { | ||
private final ClassFinder classFinder; | ||
|
||
public Reflections(ClassFinder classFinder) { | ||
this.classFinder = classFinder; | ||
} | ||
|
||
public <T> T instantiateExactlyOneSubclass(Class<T> parentType, String packageName, Class[] constructorParams, Object[] constructorArgs) { | ||
Collection<? extends T> instances = instantiateSubclasses(parentType, packageName, constructorParams, constructorArgs); | ||
if (instances.size() == 1) { | ||
return instances.iterator().next(); | ||
} else if (instances.size() == 0) { | ||
throw new CucumberException("Couldn't find a single implementation of " + parentType); | ||
} else { | ||
throw new CucumberException("Expected only one instance, but found too many: " + instances); | ||
} | ||
} | ||
|
||
public <T> Collection<? extends T> instantiateSubclasses(Class<T> parentType, String packageName, Class[] constructorParams, Object[] constructorArgs) { | ||
Collection<T> result = new HashSet<T>(); | ||
for (Class<? extends T> clazz : classFinder.getDescendants(parentType, packageName)) { | ||
if (Utils.isInstantiable(clazz) && hasConstructor(clazz, constructorParams)) { | ||
result.add(newInstance(constructorParams, constructorArgs, clazz)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public <T> T newInstance(Class[] constructorParams, Object[] constructorArgs, Class<? extends T> clazz) { | ||
try { | ||
return clazz.getConstructor(constructorParams).newInstance(constructorArgs); | ||
} catch (InstantiationException e) { | ||
throw new CucumberException(e); | ||
} catch (IllegalAccessException e) { | ||
throw new CucumberException(e); | ||
} catch (InvocationTargetException e) { | ||
throw new CucumberException(e); | ||
} catch (NoSuchMethodException e) { | ||
throw new CucumberException(e); | ||
} | ||
} | ||
|
||
private boolean hasConstructor(Class<?> clazz, Class[] paramTypes) { | ||
try { | ||
clazz.getConstructor(paramTypes); | ||
return true; | ||
} catch (NoSuchMethodException e) { | ||
return false; | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
core/src/main/java/cucumber/runtime/io/ResourceLoaderClassFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cucumber.runtime.io; | ||
|
||
import cucumber.runtime.ClassFinder; | ||
|
||
import java.io.File; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
|
||
public class ResourceLoaderClassFinder implements ClassFinder { | ||
private final ResourceLoader resourceLoader; | ||
private final ClassLoader classLoader; | ||
|
||
public ResourceLoaderClassFinder(ResourceLoader resourceLoader, ClassLoader classLoader) { | ||
this.resourceLoader = resourceLoader; | ||
this.classLoader = classLoader; | ||
} | ||
|
||
@Override | ||
public <T> Collection<Class<? extends T>> getDescendants(Class<T> parentType, String packageName) { | ||
Collection<Class<? extends T>> result = new HashSet<Class<? extends T>>(); | ||
String packagePath = "classpath:" + packageName.replace('.', '/').replace(File.separatorChar, '/'); | ||
for (Resource classResource : resourceLoader.resources(packagePath, ".class")) { | ||
String className = classResource.getClassName(); | ||
Class<?> clazz = loadClass(className, classLoader); | ||
if (clazz != null && !parentType.equals(clazz) && parentType.isAssignableFrom(clazz)) { | ||
result.add(clazz.asSubclass(parentType)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private Class<?> loadClass(String className, ClassLoader classLoader) { | ||
try { | ||
return classLoader.loadClass(className); | ||
} catch (ClassNotFoundException ignore) { | ||
return null; | ||
} catch (NoClassDefFoundError ignore) { | ||
return null; | ||
} | ||
} | ||
} |
86 changes: 0 additions & 86 deletions
86
core/src/main/java/cucumber/runtime/io/ResourceLoaderReflections.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.