All Downloads are FREE. Search and download functionalities are using the official Maven repository.

toothpick.registries.MemberInjectorRegistryLocator Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
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> memberInjectorClass =
          (Class>) Class.forName(clazz.getName() + "$$MemberInjector");
      return memberInjectorClass.newInstance();
    } catch (Exception e) {
      return null;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy