Skip to content

Commit

Permalink
Add Injector#newInstance and #newInstanceWithFields accepting Constru…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
P3ridot committed Jul 19, 2023
1 parent 0683bef commit 129e43f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public <T> ConstructorInjector<T> forConstructor(Constructor<T> constructor) {
return new ConstructorInjector<>(this.processor, constructor);
}

@Override
public <T> FieldsInjector<T> forFields(Class<T> type) {
return new FieldsInjector<>(processor, forConstructor(type));
}

@Override
public <T> FieldsInjector<T> forFields(Constructor<T> constructor) {
return new FieldsInjector<>(processor, forConstructor(constructor));
}

@Override
public <T> T newInstance(Class<T> type, Object... injectorArgs) throws DependencyInjectionException {
try {
Expand All @@ -75,13 +85,14 @@ public <T> T newInstance(Class<T> type, Object... injectorArgs) throws Dependenc
}

@Override
public <T> FieldsInjector<T> forFields(Class<T> type) {
return new FieldsInjector<>(processor, forConstructor(type));
}

@Override
public <T> FieldsInjector<T> forFields(Constructor<T> constructor) {
return new FieldsInjector<>(processor, forConstructor(constructor));
public <T> T newInstance(Constructor<T> constructor, Object... injectorArgs) throws DependencyInjectionException {
try {
T instance = this.forConstructor(constructor).newInstance(injectorArgs);
this.invokeAnnotatedMethods(PostConstruct.class, instance, injectorArgs);
return instance;
} catch (Exception exception) {
throw new DependencyInjectionException("Cannot create instance of " + constructor.getDeclaringClass().getSimpleName(), exception);
}
}

@Override
Expand All @@ -95,6 +106,27 @@ public <T> T newInstanceWithFields(Class<T> type, Object... injectorArgs) throws
}
}

@Override
public <T> T newInstanceWithFields(Constructor<T> constructor, Object... injectorArgs) throws DependencyInjectionException {
try {
T instance = this.forFields(constructor).newInstance(injectorArgs);
this.invokeAnnotatedMethods(PostConstruct.class, instance, injectorArgs);
return instance;
} catch (Exception exception) {
throw new DependencyInjectionException("Cannot create instance of " + constructor.getDeclaringClass().getSimpleName(), exception);
}
}

@Override
public MethodInjector forMethod(Method method) {
return new DefaultMethodInjector(processor, method);
}

@Override
public MethodInjector forGeneratedMethod(Method method) throws Exception {
return methodInjectorFactory.get().createMethodInjector(processor, method);
}

@Override
public <T> T invokeMethod(Method method, Object instance, Object... injectorArgs) throws DependencyInjectionException {
try {
Expand All @@ -115,16 +147,6 @@ public void invokeAnnotatedMethods(Class<? extends Annotation> annotation, Objec
}
}

@Override
public MethodInjector forMethod(Method method) {
return new DefaultMethodInjector(processor, method);
}

@Override
public MethodInjector forGeneratedMethod(Method method) throws Exception {
return methodInjectorFactory.get().createMethodInjector(processor, method);
}

@Override
public <T> @Nullable T invokeParameter(Parameter parameter, Object... injectorArgs) throws Exception {
return ObjectUtils.cast(processor.fetchValue(new PropertyParameter(parameter), injectorArgs));
Expand Down
84 changes: 52 additions & 32 deletions di/src/main/java/org/panda_lang/utilities/inject/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@

public interface Injector {

/**
* Create a new instance of the specified type using Injector
*
* @param type the class to instantiate
* @param <T> the type
* @return a new instance
* @throws DependencyInjectionException if anything happens during the injection
*/
<T> T newInstance(Class<T> type, Object... injectorArgs) throws DependencyInjectionException;

/**
* Create injector for the given type
*
Expand All @@ -52,16 +42,6 @@ public interface Injector {
*/
<T> ConstructorInjector<T> forConstructor(Constructor<T> constructor);

/**
* Create a new instance of the specified type using Injector with support for field injection
*
* @param type the class to instantiate
* @param <T> the type
* @return a new instance
* @throws DependencyInjectionException if anything happens during the injection
*/
<T> T newInstanceWithFields(Class<T> type, Object... injectorArgs) throws DependencyInjectionException;

/**
* Create injector for fields (and constructor)
*
Expand All @@ -81,24 +61,44 @@ public interface Injector {
<T> FieldsInjector<T> forFields(Constructor<T> constructor);

/**
* Invoke the method using Injector
* Create a new instance of the specified type using Injector
*
* @param method the method to invoke
* @param instance the instance to use (nullable for static context)
* @param <T> the return type
* @return the return value
* @throws DependencyInjectionException if anything happens during execution of the method
* @param type the class to instantiate
* @param <T> the type
* @return a new instance
* @throws DependencyInjectionException if anything happens during the injection
*/
@Nullable <T> T invokeMethod(Method method, @Nullable Object instance, Object... injectorArgs) throws DependencyInjectionException;
<T> T newInstance(Class<T> type, Object... injectorArgs) throws DependencyInjectionException;

/**
* Invoke methods annotated with the given annotation
* Create a new instance of the specified constructor using Injector
*
* @param annotation the annotation to look for
* @param instance the instance to use
* @throws DependencyInjectionException if anything happens during execution of the method
* @param constructor the constructor to instantiate
* @param <T> the type
* @return a new instance
* @throws DependencyInjectionException if anything happens during the injection
*/
void invokeAnnotatedMethods(Class<? extends Annotation> annotation, Object instance, Object... injectorArgs) throws DependencyInjectionException;
<T> T newInstance(Constructor<T> constructor, Object... injectorArgs) throws DependencyInjectionException;

/**
* Create a new instance of the specified type using Injector with support for field injection
*
* @param type the class to instantiate
* @param <T> type of class
* @return a new instance
* @throws DependencyInjectionException if anything happens during the injection
*/
<T> T newInstanceWithFields(Class<T> type, Object... injectorArgs) throws DependencyInjectionException;

/**
* Create a new instance of the specified constructor using Injector with support for field injection
*
* @param constructor the constructor to instantiate
* @param <T> type of class
* @return a new instance
* @throws DependencyInjectionException if anything happens during the injection
*/
<T> T newInstanceWithFields(Constructor<T> constructor, Object... injectorArgs) throws DependencyInjectionException;

/**
* Create injector for the given method
Expand All @@ -117,6 +117,26 @@ public interface Injector {
*/
MethodInjector forGeneratedMethod(Method method) throws Exception;

/**
* Invoke the method using Injector
*
* @param method the method to invoke
* @param instance the instance to use (nullable for static context)
* @param <T> the return type
* @return the return value
* @throws DependencyInjectionException if anything happens during execution of the method
*/
@Nullable <T> T invokeMethod(Method method, @Nullable Object instance, Object... injectorArgs) throws DependencyInjectionException;

/**
* Invoke methods annotated with the given annotation
*
* @param annotation the annotation to look for
* @param instance the instance to use
* @throws DependencyInjectionException if anything happens during execution of the method
*/
void invokeAnnotatedMethods(Class<? extends Annotation> annotation, Object instance, Object... injectorArgs) throws DependencyInjectionException;

/**
* Get value that would be used to inject the given parameter
*
Expand Down

0 comments on commit 129e43f

Please sign in to comment.