toothpick.registries.MemberInjectorRegistryLocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toothpick-runtime Show documentation
Show all versions of toothpick-runtime Show documentation
'Main Toothpick implementation'
package toothpick.registries;
import toothpick.MemberInjector;
import toothpick.configuration.ConfigurationHolder;
/**
* Locates the {@link MemberInjectorRegistry} instances.
* Works in the same way as {@link FactoryRegistry}, except that is no {@link MemberInjector} is found,
* we simply return {@code null}. This is required to fully support polymorphism when injecting dependencies.
*
* @see FactoryRegistry
* @see MemberInjectorRegistry
* @see MemberInjector
*/
public class MemberInjectorRegistryLocator {
private MemberInjectorRegistryLocator() {
}
private static MemberInjectorRegistry registry;
public static void setRootRegistry(MemberInjectorRegistry registry) {
MemberInjectorRegistryLocator.registry = registry;
}
public static MemberInjector getMemberInjector(Class clazz) {
return ConfigurationHolder.configuration.getMemberInjector(clazz);
}
public static MemberInjector getMemberInjectorUsingRegistries(Class clazz) {
MemberInjector memberInjector;
if (registry != null) {
memberInjector = registry.getMemberInjector(clazz);
if (memberInjector != null) {
return memberInjector;
}
}
return null;
}
public static MemberInjector getMemberInjectorUsingReflection(Class clazz) {
try {
Class extends MemberInjector> memberInjectorClass =
(Class extends MemberInjector>) Class.forName(clazz.getName() + "$$MemberInjector");
return memberInjectorClass.newInstance();
} catch (Exception e) {
return null;
}
}
}