-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed errors, added new functionality
- Loading branch information
1 parent
3f4e9f6
commit a326e14
Showing
84 changed files
with
1,526 additions
and
281 deletions.
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Optional { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,79 @@ | ||
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; | ||
} | ||
|
||
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> 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<bindingInfo.getNumber();i++){ | ||
collectionObject.add(buildObject(bindingInfo.getMultipleBindingInfo())); | ||
} | ||
return (T) collectionObject; | ||
} else { | ||
return buildObject(bindingInfo); | ||
} | ||
} | ||
|
||
private <T> 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<Object> arguments = new LinkedList<>(); | ||
|
||
for(BindingInfo argumentBindingInfo:constructorDependency.getArguments()){ | ||
arguments.add(argumentBindingInfo.getScope().getInstance(this)); | ||
} | ||
T outputObject = null; | ||
try { | ||
outputObject = (T)constructor.newInstance(arguments.toArray()); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
injectDependencies(outputObject, bindingInfo); | ||
return outputObject; | ||
} | ||
|
||
private <T> void injectDependencies(T outputObject, BindingInfo bindingInfo) { | ||
logger.log(Level.INFO, "Injecting dependencies for: " + bindingInfo.getClazz().getSimpleName()); | ||
ClassDependencies classDependencies = bindingInfo.getClassDependencies(); | ||
List<MethodDependency> methodDependencies = classDependencies.getMethodsToInject(); | ||
injectMethods(outputObject, methodDependencies); | ||
|
||
List<FieldDependency> fieldDependencies = classDependencies.getFieldsToInject(); | ||
injectFields(outputObject, fieldDependencies); | ||
} | ||
|
||
private <T> void injectMethods(T outputObject, List<MethodDependency> methodDependencies){ | ||
for(MethodDependency methodDependency:methodDependencies){ | ||
methodDependency.getMethod().setAccessible(true); | ||
List<Object> arguments = new LinkedList<>(); | ||
if(methodDependency.canInvoke()){ | ||
for(BindingInfo argumentBindingInfo:methodDependency.getArguments()){ | ||
if(methodDependency.isOptional()){ | ||
try{ | ||
arguments.add(argumentBindingInfo.getScope().getInstance(this)); | ||
} catch (Throwable throwable){ | ||
methodDependency.setCanInvoke(false); } | ||
} else { | ||
arguments.add(argumentBindingInfo.getScope().getInstance(this)); | ||
} | ||
} | ||
|
||
if(methodDependency.canInvoke()){ | ||
try { | ||
methodDependency.getMethod().invoke(outputObject, arguments.toArray()); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private <T> void injectFields(T outputObject, List<FieldDependency> fieldDependencies){ | ||
for(FieldDependency fieldDependency:fieldDependencies){ | ||
fieldDependency.getField().setAccessible(true); | ||
Object createdObject = null; | ||
|
||
if(fieldDependency.canInvoke()){ | ||
if(fieldDependency.isOptional()){ | ||
try{ | ||
createdObject = fieldDependency.getArgument().getScope().getInstance(this); | ||
} catch (Throwable throwable){ | ||
fieldDependency.setCanInvoke(false); } | ||
} else { | ||
createdObject = fieldDependency.getArgument().getScope().getInstance(this); | ||
} | ||
|
||
if(fieldDependency.canInvoke()){ | ||
try { | ||
fieldDependency.getField().set(outputObject, createdObject); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.