diff --git a/diagram-klas-draw.io.png b/diagram-klas-draw.io.png
deleted file mode 100644
index 45837ff..0000000
Binary files a/diagram-klas-draw.io.png and /dev/null differ
diff --git a/diagram-klas-draw.io.xml b/diagram-klas-draw.io.xml
deleted file mode 100644
index ae310e9..0000000
--- a/diagram-klas-draw.io.xml
+++ /dev/null
@@ -1 +0,0 @@
-7VxRc5s6Fv41nnEf0jHGYOcxdtPdzKS7maYzu/dRBtlWKyMvyEncX79HSMKAREKM7Nw7l06nhYMkJJ1P53znSHjgL7Yv/0jRbvONxZgOxqP4ZeB/GYzH3sQfwX9CcpCS6ehaCtYpiVWho+CR/MZKqOqt9yTGWaUgZ4xysqsKI5YkOOIVGUpT9lwttmK0+tYdWmND8Bghakr/Q2K+UdIwGB8f/BOT9Ua/2gvVAJco+rVO2T5RLxyM/VX+Rz7eIt2YGmm2QTF7Lon824G/SBnj8mr7ssBUTK6eN1nva8PTouMpTnibCkGg+sEPevA4hrlQtyzlG7ZmCaK3R+k8Hx8WLYzgbsO3FC49uISXpof/CvnnQN/+UTyLb4Rq4DYmaMvEDAnhV0J19Z+Y84NCA9pzBqJjB+4Z26lyssuin42jVqKM7dNIlVK64yhdY1VqWsw4QBmzLYb+QpEUU8TJU7V1pDC1LsqpqjAqdCgV2DGS8Kw863BRav8oynVh14vq7BOie6yRFFLo9pzAxVpc3CwznqKIP8KskWSd6QLQdFHGUG72TLYUJbiqtxVLuFa5uEeUrBO4jmA+cQqCJ5xyAuvjRj3gQhfzaENofI8ObC/mM+OAfX0337CU/IZmkX4HPE65Uu44rJR4FDUVmlKcQZkHrUqvEN2jjKsyEaMU7TKyLDq8BaWSZM44Z1tVSI9U4GvBKEvzCdCrUU+NBTNirPilJDIBop6Op6Gsoszc2J/I++ej0RhfK9mmbC9mYTOoKqh5BSK+AZGrJUniHAr+DTz5hnaAA7QVqsqRsaAoA5gs7glMZeXJXFa8S1aseKAAVL2rwQkmilehBJhkv7Ce8ITlUFuVdKBEGmIUr3gjwLIdiqB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5iCIxJ+VWuLfFsUnomsHGl1CyeKWZStPnK8Q6FOy2obh/C4Jb1orZjHyDb1WUbI4BxN171TAL6rcnISxlTAcz0DhCGkgiXD4WYIV7XsK06N3XCuUFi4/ZLmp9VLi/u0rJa5TzduvhqS/sMrG0aBi0DIgekLZieKSDyZ9VTx59HM3/QsOERo2yTN1fbe1lSJnh4p8jpvSeT9bFRM5S6QOTkm6cBi03HPnS6ZOh0HVgW4cgWOjnRu7lNDfY0xSg+LOBfLhaHMIpLxihGiYX7RLLYv5c/c7smCqvrnueczHOK5dgpxrKhxncQY/nmTlkfY3XUn23Hy6Y/FzGWmSWT2etzk6R+9Z8NPZYox4aeiYtMtuMDLwVxm772tVg3RlbheafRMz11H0LP9MstkWVMnkTjCvrH4OetuLAc7Mm1myyz3d/yfvQAy5vxw5vBcD7XJ5/eHnVmuxZT+MEE2H1Qeh3U+bDl5PbU5hkdBKUTWwand41/HtdYGMJOJ7Zt8HGx+aA3il27xkHlAGcwcL3J+25fODuDLzzppGYwq6WwroOywt5bvvNJzdA/Ezl6jRs5OKnpAgHB7FJsyNzvne/Br/a7/Gf+0r2eL56YYYfv2VJVLuKOibmze0X08Vz5PVRxWLd3sCc72FlrVDQ7WCsIXHyzrrlhn3lypz/LV+tW/bn4at2zqG8uM8hVht1hM35FkljO1z4/yVluryS31CTZ7cuOkohwaUlAM5WGjteiueaUuOjB3Va21NCA1K5o5tUu9ZbskpbMkoO1rwQHkWbo+CeXPpIMdiZ+dq5e3xkLZjUW0fC11wm0PTAZZf/lT0tKaawvC16aKWUt2xN4vrEGx7Zw3dO7GJ38kYVS9h/yuDDEb4CiyWK8splpA4ELShn2lNK5/mw/hGRN2TpQn5VSyrjwCwZfCICPCM6G8lDCq+cwVTj5DYNnjE+s/JVg+u66RY9v0vV+C7OcDW9fcLTPtfupcqhCqrC3Rhe0RrateRuap++nhXB7/JVPyVuOv6Xq3/4f
\ No newline at end of file
diff --git a/src/main/java/myinjector/AbstractSettings.java b/src/main/java/myinjector/AbstractSettings.java
index 77949a8..162d748 100644
--- a/src/main/java/myinjector/AbstractSettings.java
+++ b/src/main/java/myinjector/AbstractSettings.java
@@ -1,5 +1,7 @@
package myinjector;
+import myinjector.Exceptions.WrongOutputClassException;
+
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -16,18 +18,21 @@ public AbstractSettings() {
public abstract void load();
- protected BindingInfo addBinding(Class sourceClass, Class outputClass){
- // @TODO: check if outputClass isn't abstract or interface
- BindingInfo bindingInfo = new BindingInfo(outputClass);
- List bindingInfoList;
- if(bindings.containsKey(sourceClass)){
- bindingInfoList = bindings.get(sourceClass);
- } else {
- bindingInfoList = new LinkedList();
- }
- bindingInfoList.add(bindingInfo);
- this.bindings.put(sourceClass, bindingInfoList);
- return bindingInfo;
+ protected BindingInfo addBinding(Class sourceClass, Class outputClass) throws WrongOutputClassException {
+ if(outputClass.isInterface()){
+ throw new WrongOutputClassException(String.format("Class %s cannot be created because it is " +
+ "an interface.", outputClass.toGenericString()));
+ }
+ BindingInfo bindingInfo = new BindingInfo(outputClass);
+ List bindingInfoList;
+ if(bindings.containsKey(sourceClass)){
+ bindingInfoList = bindings.get(sourceClass);
+ } else {
+ bindingInfoList = new LinkedList();
+ }
+ bindingInfoList.add(bindingInfo);
+ this.bindings.put(sourceClass, bindingInfoList);
+ return bindingInfo;
}
public BindingInfo getBindingInfo(Class sourceClass){
@@ -39,19 +44,17 @@ public BindingInfo getBindingInfo(Class sourceClass){
}
public BindingInfo getNamedBindingInfo(Class sourceClass, String name){
- System.out.println("Getting named binding info for class " + sourceClass.getName() + " and string " + name);
List bindingInfoList = getAllBindingInfos(sourceClass);
- for (BindingInfo bindingInfo:bindingInfoList) {
- System.out.println("Checking binding info: " + bindingInfo.getName());
- if(bindingInfo.getName() != null){
- if(bindingInfo.getName().equals(name)){
- System.out.println("I found binding!");
- return bindingInfo;
+ if(bindingInfoList != null){
+ for (BindingInfo bindingInfo:bindingInfoList) {
+ if(bindingInfo.getBindingName() != null){
+ if(bindingInfo.getBindingName().equals(name)){
+ return bindingInfo;
+ }
}
}
}
- System.out.println("Returning null");
- return null; //@TODO replace with: display warning there is no such binding
+ return null;
}
private List getAllBindingInfos(Class sourceClass){
diff --git a/src/main/java/myinjector/Annotations/Multiple.java b/src/main/java/myinjector/Annotations/Multiple.java
new file mode 100644
index 0000000..85b23e9
--- /dev/null
+++ b/src/main/java/myinjector/Annotations/Multiple.java
@@ -0,0 +1,9 @@
+package myinjector.Annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Multiple {
+ int number();
+}
diff --git a/src/main/java/myinjector/Annotations/Optional.java b/src/main/java/myinjector/Annotations/Optional.java
index 1b9a26e..f07bfbf 100644
--- a/src/main/java/myinjector/Annotations/Optional.java
+++ b/src/main/java/myinjector/Annotations/Optional.java
@@ -5,4 +5,5 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Optional {
+
}
diff --git a/src/main/java/myinjector/BindingInfo.java b/src/main/java/myinjector/BindingInfo.java
index 24920ef..b7f3974 100644
--- a/src/main/java/myinjector/BindingInfo.java
+++ b/src/main/java/myinjector/BindingInfo.java
@@ -1,27 +1,43 @@
package myinjector;
+import myinjector.Dependencies.ClassDependencies;
import myinjector.Scopes.IScope;
import myinjector.Scopes.Prototype;
import myinjector.Scopes.Singleton;
+
public class BindingInfo {
private Class clazz;
- private String name;
+ private ClassDependencies classDependencies;
+ private String bindingName;
private IScope scope;
+ private Object instance;
+ private int number = 1;
+ private BindingInfo multipleBindingInfo;
public BindingInfo(Class clazz){
this.clazz = clazz;
- this.scope = new Prototype();
+ this.scope = new Prototype(this);
}
public BindingInfo setSingleton(){
- this.scope = new Singleton();
+ this.scope = new Singleton(this);
+ return this;
+ }
+
+ public BindingInfo setBindingName(String bindingName){
+ this.bindingName = bindingName;
+ return this;
+ }
+
+ public BindingInfo toInstance(Object instance){
+ this.instance = instance;
return this;
}
- public BindingInfo setName(String name){
- this.name = name;
+ public BindingInfo setMultiple(int number){
+ this.number = number;
return this;
}
@@ -29,11 +45,35 @@ public Class getClazz() {
return clazz;
}
- public String getName() {
- return name;
+ public String getBindingName() {
+ return bindingName;
}
public IScope getScope() {
return scope;
}
+
+ public void setClassDependencies(ClassDependencies classDependencies) {
+ this.classDependencies = classDependencies;
+ }
+
+ public ClassDependencies getClassDependencies(){
+ return classDependencies;
+ }
+
+ public Object getInstance() {
+ return instance;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+
+ public BindingInfo getMultipleBindingInfo() {
+ return multipleBindingInfo;
+ }
+
+ public void setMultipleBindingInfo(BindingInfo multipleBindingInfo) {
+ this.multipleBindingInfo = multipleBindingInfo;
+ }
}
diff --git a/src/main/java/myinjector/Builder.java b/src/main/java/myinjector/Builder.java
deleted file mode 100644
index 90fc30b..0000000
--- a/src/main/java/myinjector/Builder.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package myinjector;
-
-import myinjector.Annotations.Inject;
-
-import java.lang.reflect.*;
-import java.util.Arrays;
-import java.util.Comparator;
-
-public class Builder{
-
- private Injector injector;
-
- public Builder(AbstractSettings settings){
- this.injector = new Injector(settings);
- }
-
- public T createInstance(Class clazz) {
- System.out.println("Creating object: " + clazz.getName());
- Constructor constructor = findConstructor(clazz);
- Object[] arguments = injector.injectArguments(constructor);
- T outputObject = null;
- try {
- outputObject = (T)constructor.newInstance(arguments);
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
- e.printStackTrace();
- }
- injector.injectDependencies(outputObject);
- return outputObject;
- }
-
-
- private Constructor findConstructor(Class clazz) {
- Constructor[] allConstructors = clazz.getConstructors();
- if(allConstructors.length == 0){
- Constructor constructor = null;
- try {
- constructor = clazz.getConstructor();
- } catch (NoSuchMethodException e) {
- //@TODO: display info that clazz can not be created
- }
- return constructor;
- }
- for (Constructor constructor: allConstructors) {
- if(isExplicitInjectionConstructor(constructor)){
- return constructor;
- }
- }
-
- return findImplicitConstructor(allConstructors);
- }
-
- private boolean isExplicitInjectionConstructor(Constructor constructor)
- {
- return (constructor.getAnnotation(Inject.class) != null);
- }
-
- private Constructor findImplicitConstructor(Constructor[] constructors) {
- Arrays.sort(constructors, new Comparator() {
- public int compare(Constructor constr1, Constructor constr2){
- if(constr1.getParameterTypes().length >= constr2.getParameterTypes().length)
- return -1;
- else return 1;
- }
- });
-
- return constructors[0];
- }
-}
diff --git a/src/main/java/myinjector/ComponentBuilder.java b/src/main/java/myinjector/ComponentBuilder.java
new file mode 100644
index 0000000..b37a5d2
--- /dev/null
+++ b/src/main/java/myinjector/ComponentBuilder.java
@@ -0,0 +1,121 @@
+package myinjector;
+
+import myinjector.Dependencies.ClassDependencies;
+import myinjector.Dependencies.ConstructorDependency;
+import myinjector.Dependencies.FieldDependency;
+import myinjector.Dependencies.MethodDependency;
+
+import java.lang.reflect.*;
+import java.util.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class ComponentBuilder {
+ private static final Logger logger = Logger.getLogger("myinjector");
+
+ public T createInstance(BindingInfo bindingInfo) {
+ if(bindingInfo.getInstance() != null){
+ return (T) bindingInfo.getInstance();
+ }
+
+ if(bindingInfo.getNumber() != 1){
+ Collection> collectionObject = null;
+ try {
+ collectionObject = (Collection>) bindingInfo.getClazz().newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+
+ for(int i=0; i T buildObject(BindingInfo bindingInfo){
+ logger.log(Level.INFO, "Building object: " + bindingInfo.getClazz().getSimpleName());
+ ClassDependencies classDependencies = bindingInfo.getClassDependencies();
+ ConstructorDependency constructorDependency = classDependencies.getConstructorDependency();
+ Constructor constructor = constructorDependency.getConstructor();
+ List