Skip to content

Commit

Permalink
- Creating Child containers based on parent container
Browse files Browse the repository at this point in the history
- Types resolver as build in feature
  • Loading branch information
jwdeveloper committed Jun 3, 2024
1 parent ea8f0c1 commit 8150e3d
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 31 deletions.
53 changes: 53 additions & 0 deletions CheckOutExamples/src/main/java/tutorial/_11_ResolveParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tutorial;

import io.github.jwdeveloper.dependance.Dependance;
import io.github.jwdeveloper.dependance.api.DependanceContainer;

public class _11_ResolveParameters {
public static void main(String[] args) throws Exception {
DependanceContainer container = Dependance.newContainer()
.registerTransient(_11_ResolveParameters.ExampleWithGenerics.class)
.registerTransient(_11_ResolveParameters.ExampleClass.class)
.configure(config ->
{
config.onInjection(onInjectionEvent ->
{
if (!onInjectionEvent.input().equals(ExampleWithGenerics.class)) {
return onInjectionEvent.output();
}

var geneicsTyles = onInjectionEvent.inputGenericParameters()[0];
if (!String.class.equals(geneicsTyles)) {
return onInjectionEvent.output();
}
return new ExampleWithGenerics<String>();
});
})
.build();

var method = _11_ResolveParameters.class.getDeclaredMethod(
"sayHello",
_11_ResolveParameters.ExampleWithGenerics.class,
_11_ResolveParameters.ExampleClass.class);

var parameters = container.resolveParameters(method);

method.invoke(null, parameters);
}


public static void sayHello(_11_ResolveParameters.ExampleWithGenerics<String> exampleService,
_11_ResolveParameters.ExampleClass exampleClass) {

System.out.println("Hello world");
}


public static class ExampleWithGenerics<T> {

}

public static class ExampleClass {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,43 @@
*/
package io.github.jwdeveloper.dependance.api;

import io.github.jwdeveloper.dependance.implementation.DependanceContainerBuilder;
import io.github.jwdeveloper.dependance.injector.api.containers.Container;

public interface DependanceContainer extends Container
{
<T> T find(Class<T> type);
import java.lang.reflect.Executable;
import java.lang.reflect.Type;

public interface DependanceContainer extends Container {

/**
* Finds an instance of the specified type that was previously registered in the container.
*
* @param <T> the type of the instance to be retrieved
* @param type the Class object of the type to be retrieved
* @return an instance of the specified type, or null if not found
*/
<T> T find(Class<T> type);

/**
* Resolves an array of object instances based on the provided parameter types.
*
* @param parametersTypes an array of Type objects representing the types of the parameters to be resolved
* @return an array of object instances corresponding to the specified parameter types
*/
Object[] resolveParameters(Type[] parametersTypes);

/**
* Resolves and returns the parameters required for the given method or constructor.
*
* @param method an Executable object representing the method or constructor whose parameters are to be resolved
* @return an array of object instances representing the resolved parameters for the specified method or constructor
*/
Object[] resolveParameters(Executable method);

/**
* Creates a builder for a sub-container that will be a child of this container.
*
* @return a DependanceContainerBuilder for building a sub-container
*/
DependanceContainerBuilder createChildContainer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,69 @@
*/
package io.github.jwdeveloper.dependance.implementation;

import io.github.jwdeveloper.dependance.Dependance;
import io.github.jwdeveloper.dependance.api.DependanceContainer;
import io.github.jwdeveloper.dependance.injector.api.containers.Container;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Collection;

public class DepenDanceContainerImpl implements DependanceContainer
{
public class DepenDanceContainerImpl implements DependanceContainer {

private final Container container;

public DepenDanceContainerImpl(Container container) {
this.container = container;
}

public <T> T find(Class<T> type)
{
return (T)container.find(type);
public <T> T find(Class<T> type) {
return (T) container.find(type);
}

@Override
public Object[] resolveParameters(Type[] types) {
var output = new Object[types.length];
var index = 0;
for (var type : types) {
if (type instanceof ParameterizedType parameterizedType) {
output[index] = find((Class<?>) parameterizedType.getRawType(), parameterizedType.getActualTypeArguments());
} else {
output[index] = find((Class<?>) type);
}
index++;
}
return output;
}

@Override
public Object[] resolveParameters(Executable executable) {
var parameters = executable.getParameters();
var types = Arrays.stream(parameters)
.map(Parameter::getParameterizedType)
.toArray(Type[]::new);
return resolveParameters(types);
}

@Override
public DependanceContainerBuilder createChildContainer() {
return Dependance.newContainer()
.configure(config ->
{
config.onInjection(onInjectionEvent ->
{
if (onInjectionEvent.hasOutput()) {
return onInjectionEvent.output();
}
return this.find(onInjectionEvent.input(), onInjectionEvent.inputGenericParameters());
});
});
}

@Override
public Object find(Class<?> type, Type... genericParameters) {
return container.find(type,genericParameters);
return container.find(type, genericParameters);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public List<Class<?>> scanAndRegister() {

var containerConfig = containerBuilder.getDependanceContainerConfiguration();
var config = (ContainerConfigurationImpl) containerConfig.getConfiguration();
var registeredTypes = config.getRegisterdTypes();
var registeredTypes = config.getRegisteredTypes();
var autoScanEvents = containerConfig.getAutoScanEvents();
for (var _class : classes) {
if (registeredTypes.contains(_class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import io.github.jwdeveloper.dependance.injector.api.models.RegistrationInfo;
import io.github.jwdeveloper.dependance.injector.api.search.ContainerSearch;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

public interface Container extends Cloneable, ContainerSearch
{
Object find(Class<?> type, Type... genericParameters);


}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
public class ContainerConfigurationImpl implements ContainerConfiguration {
private final List<RegistrationInfo> registrations = new ArrayList<>();

private final Set<Class<?>> registerdTypes = new HashSet<>();
private final Set<Class<?>> registeredTypes = new HashSet<>();

private final List<ContainerEvents> events = new ArrayList<>();

private final List<RegistrationInfo> registerAsList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.github.jwdeveloper.dependance.injector.api.models.InjectionInfo;
import io.github.jwdeveloper.dependance.injector.api.models.RegistrationInfo;
import io.github.jwdeveloper.dependance.injector.api.provider.InstanceProvider;
import io.github.jwdeveloper.dependance.injector.api.search.SearchAgent;
import io.github.jwdeveloper.dependance.injector.implementation.utilites.Messages;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -94,21 +93,27 @@ public Object find(Class<?> _injection, Type... genericParameters) {
}



if (genericParameters == null || genericParameters.length == 0) {
var lastInjectionInfo = injectionInfos.get(injectionInfos.size() - 1);
return find(lastInjectionInfo, genericParameters);
}

var genericsType = genericParameters[0];
var optional = injectionInfos.stream().filter(e -> e.getInjectionValueType().equals(genericsType)).findFirst();
if (optional.isEmpty())
{
if(_injection.equals(genericsType))
{
if (optional.isEmpty()) {
if (_injection.equals(genericsType)) {
var lastInjectionInfo = injectionInfos.get(injectionInfos.size() - 1);
return find(lastInjectionInfo, null);
}
var onInjectionEvent = new OnInjectionEvent(_injection,
genericParameters,
null,
null,
injections,
this);
var result = eventHandler.OnInjection(onInjectionEvent);
if (result != null) {
return result;
}
throw new InjectionNotFoundException(Messages.INJECTION_NOT_FOUND_GENERICS_TYPE, _injection.getSimpleName(), genericsType.getTypeName());
}
return find(optional.get(), genericParameters);
Expand All @@ -134,7 +139,6 @@ private Object find(InjectionInfo injectionInfo, Type... genericParameters) {

@Override
public <T> Collection<T> findAllByInterface(Class<T> _interface) {

Class _searchedInterface = _interface;
Object temp = null;
var result = new ArrayList<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private Builder builder() {
}

private void addRegisteredType(Class<?> type) {
config.getRegisterdTypes().add(type);
config.getRegisteredTypes().add(type);
}

public Container build() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public EventHandlerImpl(List<ContainerEvents> events) {

@Override
public boolean OnRegistration(OnRegistrationEvent event) {
for (var handler : events) {
for (int i = events.size() - 1; i >= 0; i--) {
var handler = events.get(i);
var res = handler.OnRegistration(event);
if (!res) {
return false;
Expand All @@ -50,7 +51,8 @@ public boolean OnRegistration(OnRegistrationEvent event) {
@Override
public Object OnInjection(OnInjectionEvent event) {
var output = event.output();
for (var handler : events) {
for (int i = events.size() - 1; i >= 0; i--) {
var handler = events.get(i);
output = handler.OnInjection(new OnInjectionEvent(event.input(),
event.inputGenericParameters(),
event.injectionInfo(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,33 @@

import io.github.jwdeveloper.dependance.ContainerBenchmark;
import io.github.jwdeveloper.dependance.Dependance;
import io.github.jwdeveloper.dependance.exampleClasses.ExampleClass;
import io.github.jwdeveloper.dependance.exampleClasses.ExampleClass2;
import io.github.jwdeveloper.dependance.exampleClasses.*;

public class DepenDanceContainer
{
public static void run()
{
public class DepenDanceContainer {
public static void run() {
var container = Dependance.newContainer()
.registerTransient(ExampleClass.class)
.registerTransient(ExampleClass2.class)
.registerTransient(ExampleClass3.class)
.registerTransient(ExampleClass4.class)
.registerTransient(ExampleClass5.class)
.registerTransient(ExampleClass6.class)
.build();
container.find(ExampleClass.class);
}

public static void main(String[] args) throws InterruptedException {

var now = System.nanoTime();
var iteration = 0;
while (true) {
now = System.nanoTime();
run();
var endTime = System.nanoTime();
var delta = endTime - now;
System.out.println(iteration + " Action performed in " + delta / 1_000_000f + " MS");
Thread.sleep(10);
iteration++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import io.github.jwdeveloper.dependance.exampleClasses.ExampleClass;
import io.github.jwdeveloper.dependance.exampleClasses.ExampleClass2;
import io.github.jwdeveloper.dependance.exampleClasses.*;

public class GuiceContainer extends AbstractModule
{
public static void run()
{
Injector injector = Guice.createInjector(new GuiceContainer());
injector.getInstance(ExampleClass.class);

}


@Override
protected void configure() {
bind(ExampleClass.class);
bind(ExampleClass2.class);
bind(ExampleClass3.class);
bind(ExampleClass4.class);
bind(ExampleClass5.class);
bind(ExampleClass6.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@
public class ExampleClass2
{

private ExampleClass3 exampleClass3;

public ExampleClass2(ExampleClass3 exampleClass3) {
this.exampleClass3 = exampleClass3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.jwdeveloper.dependance.exampleClasses;

public class ExampleClass3
{
ExampleClass4 exampleClass4;

public ExampleClass3(ExampleClass4 exampleClass4) {
this.exampleClass4 = exampleClass4;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.jwdeveloper.dependance.exampleClasses;

public class ExampleClass4 {

ExampleClass5 exampleClass5;

public ExampleClass4(ExampleClass5 exampleClass5) {
this.exampleClass5 = exampleClass5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.jwdeveloper.dependance.exampleClasses;

public class ExampleClass5 {

ExampleClass6 exampleClass6;

public ExampleClass5(ExampleClass6 exampleClass6) {
this.exampleClass6 = exampleClass6;
}
}
Loading

0 comments on commit 8150e3d

Please sign in to comment.