diff --git a/di/src/main/java/org/panda_lang/utilities/inject/DefaultInjector.java b/di/src/main/java/org/panda_lang/utilities/inject/DefaultInjector.java index 0e69f79..593559b 100644 --- a/di/src/main/java/org/panda_lang/utilities/inject/DefaultInjector.java +++ b/di/src/main/java/org/panda_lang/utilities/inject/DefaultInjector.java @@ -63,6 +63,16 @@ public ConstructorInjector forConstructor(Constructor constructor) { return new ConstructorInjector<>(this.processor, constructor); } + @Override + public FieldsInjector forFields(Class type) { + return new FieldsInjector<>(processor, forConstructor(type)); + } + + @Override + public FieldsInjector forFields(Constructor constructor) { + return new FieldsInjector<>(processor, forConstructor(constructor)); + } + @Override public T newInstance(Class type, Object... injectorArgs) throws DependencyInjectionException { try { @@ -75,13 +85,14 @@ public T newInstance(Class type, Object... injectorArgs) throws Dependenc } @Override - public FieldsInjector forFields(Class type) { - return new FieldsInjector<>(processor, forConstructor(type)); - } - - @Override - public FieldsInjector forFields(Constructor constructor) { - return new FieldsInjector<>(processor, forConstructor(constructor)); + public T newInstance(Constructor 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 @@ -95,6 +106,27 @@ public T newInstanceWithFields(Class type, Object... injectorArgs) throws } } + @Override + public T newInstanceWithFields(Constructor 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 invokeMethod(Method method, Object instance, Object... injectorArgs) throws DependencyInjectionException { try { @@ -115,16 +147,6 @@ public void invokeAnnotatedMethods(Class 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 @Nullable T invokeParameter(Parameter parameter, Object... injectorArgs) throws Exception { return ObjectUtils.cast(processor.fetchValue(new PropertyParameter(parameter), injectorArgs)); diff --git a/di/src/main/java/org/panda_lang/utilities/inject/Injector.java b/di/src/main/java/org/panda_lang/utilities/inject/Injector.java index 458c796..df01f7a 100644 --- a/di/src/main/java/org/panda_lang/utilities/inject/Injector.java +++ b/di/src/main/java/org/panda_lang/utilities/inject/Injector.java @@ -24,16 +24,6 @@ public interface Injector { - /** - * Create a new instance of the specified type using Injector - * - * @param type the class to instantiate - * @param the type - * @return a new instance - * @throws DependencyInjectionException if anything happens during the injection - */ - T newInstance(Class type, Object... injectorArgs) throws DependencyInjectionException; - /** * Create injector for the given type * @@ -52,16 +42,6 @@ public interface Injector { */ ConstructorInjector forConstructor(Constructor constructor); - /** - * Create a new instance of the specified type using Injector with support for field injection - * - * @param type the class to instantiate - * @param the type - * @return a new instance - * @throws DependencyInjectionException if anything happens during the injection - */ - T newInstanceWithFields(Class type, Object... injectorArgs) throws DependencyInjectionException; - /** * Create injector for fields (and constructor) * @@ -81,24 +61,44 @@ public interface Injector { FieldsInjector forFields(Constructor 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 the return type - * @return the return value - * @throws DependencyInjectionException if anything happens during execution of the method + * @param type the class to instantiate + * @param the type + * @return a new instance + * @throws DependencyInjectionException if anything happens during the injection */ - @Nullable T invokeMethod(Method method, @Nullable Object instance, Object... injectorArgs) throws DependencyInjectionException; + T newInstance(Class 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 the type + * @return a new instance + * @throws DependencyInjectionException if anything happens during the injection */ - void invokeAnnotatedMethods(Class annotation, Object instance, Object... injectorArgs) throws DependencyInjectionException; + T newInstance(Constructor 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 type of class + * @return a new instance + * @throws DependencyInjectionException if anything happens during the injection + */ + T newInstanceWithFields(Class 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 type of class + * @return a new instance + * @throws DependencyInjectionException if anything happens during the injection + */ + T newInstanceWithFields(Constructor constructor, Object... injectorArgs) throws DependencyInjectionException; /** * Create injector for the given method @@ -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 the return type + * @return the return value + * @throws DependencyInjectionException if anything happens during execution of the method + */ + @Nullable 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 annotation, Object instance, Object... injectorArgs) throws DependencyInjectionException; + /** * Get value that would be used to inject the given parameter *