Skip to content

Commit

Permalink
fixed errors, added new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
pjamrozowicz committed Feb 21, 2017
1 parent 3f4e9f6 commit a326e14
Show file tree
Hide file tree
Showing 84 changed files with 1,526 additions and 281 deletions.
Binary file removed diagram-klas-draw.io.png
Binary file not shown.
1 change: 0 additions & 1 deletion diagram-klas-draw.io.xml

This file was deleted.

45 changes: 24 additions & 21 deletions src/main/java/myinjector/AbstractSettings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package myinjector;

import myinjector.Exceptions.WrongOutputClassException;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -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<BindingInfo> bindingInfoList;
if(bindings.containsKey(sourceClass)){
bindingInfoList = bindings.get(sourceClass);
} else {
bindingInfoList = new LinkedList<BindingInfo>();
}
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<BindingInfo> bindingInfoList;
if(bindings.containsKey(sourceClass)){
bindingInfoList = bindings.get(sourceClass);
} else {
bindingInfoList = new LinkedList<BindingInfo>();
}
bindingInfoList.add(bindingInfo);
this.bindings.put(sourceClass, bindingInfoList);
return bindingInfo;
}

public BindingInfo getBindingInfo(Class sourceClass){
Expand All @@ -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<BindingInfo> 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<BindingInfo> getAllBindingInfos(Class sourceClass){
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/myinjector/Annotations/Multiple.java
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();
}
1 change: 1 addition & 0 deletions src/main/java/myinjector/Annotations/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

@Retention(RetentionPolicy.RUNTIME)
public @interface Optional {

}
54 changes: 47 additions & 7 deletions src/main/java/myinjector/BindingInfo.java
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;
}
}
68 changes: 0 additions & 68 deletions src/main/java/myinjector/Builder.java

This file was deleted.

121 changes: 121 additions & 0 deletions src/main/java/myinjector/ComponentBuilder.java
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();
}
}
}
}
}
}
Loading

0 comments on commit a326e14

Please sign in to comment.