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

com.github.henryco.injector.GrInjector Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package com.github.henryco.injector;

import com.github.henryco.injector.meta.ModuleStruct;

public final class GrInjector {

	private static GrInjector ourInstance = new GrInjector();

	private final Container container;

	private GrInjector() {
		container = new Container();
	}


	private void injectToTarget(Object dest, ClassLoader classLoader) {
		for (ModuleStruct module : container.modules) {
			if (classLoader != null) module.setClassLoader(classLoader);

			boolean inject = module.inject(dest);
			if (inject) return;
		}
		throw new RuntimeException("There are no dependencies to inject " + dest.getClass());
	}

	@SuppressWarnings("Duplicates")
	private void injectToTarget(Object dest, ClassLoader classLoader, String... components) {

		for (ModuleStruct module : container.modules) {
			if (classLoader != null) module.setClassLoader(classLoader);

			boolean inject = module.inject(dest, components);
			if (inject) return;
		}
		throw new RuntimeException("There are no dependencies to inject " + dest.getClass());
	}

	@SuppressWarnings("Duplicates")
	private void injectToTarget(Object dest, ClassLoader classLoader, Class... components) {
		for (ModuleStruct module : container.modules) {
			if (classLoader != null) module.setClassLoader(classLoader);

			boolean inject = module.inject(dest, components);
			if (inject) return;
		}
		throw new RuntimeException("There are no dependencies to inject " + dest.getClass());
	}

	private  T getComponentByName(String name, ClassLoader classLoader) {
		for (ModuleStruct module : container.modules) {
			if (classLoader != null) module.setClassLoader(classLoader);

			T t = module.findOrInstanceByName(name);
			if (t != null) return t;
		}
		return null;
	}

	private  T getComponentByType(Class type, ClassLoader classLoader) {
		for (ModuleStruct module : container.modules) {
			if (classLoader != null) module.setClassLoader(classLoader);

			T t = module.findOrInstanceByType(type);
			if (t != null) return t;
		}
		return null;
	}

	private void addRootModules(ClassLoader classLoader, Class... rootModules) {
		container.addModules(classLoader, rootModules);
	}

	private void setRootClassLoader(ClassLoader classLoader) {
		container.setClassLoader(classLoader);
	}

	private void resetModules() {
		container.reset();
	}



	/**
	 * @param dest instance inject to
	 */
	public static void inject(Object dest) {
		inject(dest, (ClassLoader) null);
	}

	/**
	 * @param dest instance inject to
	 */
	public static void inject(Object dest, ClassLoader classLoader) {
		ourInstance.injectToTarget(dest, classLoader);
	}




	/**
	 * @param dest instance inject to
	 * @param components desired components names
	 */
	public static void inject(Object dest, String ... components) {
		inject(dest, null, components);
	}

	/**
	 * @param dest instance inject to
	 * @param components desired components names
	 */
	public static void inject(Object dest, ClassLoader classLoader, String ... components) {
		ourInstance.injectToTarget(dest, classLoader, components);
	}




	/**
	 * @param dest instance inject to
	 * @param components desired components types
	 */
	public static void inject(Object dest, Class ... components) {
		inject(dest, null, components);
	}

	/**
	 * @param dest instance inject to
	 * @param components desired components types
	 */
	public static void inject(Object dest, ClassLoader classLoader, Class ... components) {
		ourInstance.injectToTarget(dest, classLoader, components);
	}




	public static void addModules(Class ... rootModules) {
		addModules(null, rootModules);
	}

	public static void addModules(ClassLoader classLoader, Class ... rootModules) {
		ourInstance.addRootModules(classLoader, rootModules);
	}



	public static  T getComponent(String name) {
		return getComponent(name, null);
	}

	public static  T getComponent(String name, ClassLoader classLoader) {
		return ourInstance.getComponentByName(name, classLoader);
	}


	public static  T getComponent(Class cmpClass) {
		return getComponent(cmpClass, null);
	}

	public static  T getComponent(Class cmpClass, ClassLoader classLoader) {
		return ourInstance.getComponentByType(cmpClass, classLoader);
	}

	public static void setClassLoader(ClassLoader classLoader) {
		ourInstance.setRootClassLoader(classLoader);
	}


	/**
	 * Reset all modules with dependencies
	 */
	public static void reset() {
		ourInstance.resetModules();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy