Skip to content

Commit

Permalink
Refactoring, new interface for Scopes, added Named Annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
pjamrozowicz committed Dec 11, 2016
1 parent c5753ae commit 6630a48
Show file tree
Hide file tree
Showing 53 changed files with 557 additions and 513 deletions.
2 changes: 1 addition & 1 deletion PROJEKT.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
2 changes: 1 addition & 1 deletion diagram-klas-draw.io.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<mxfile userAgent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0" version="6.0.1.9" editor="www.draw.io" type="device"><diagram name="Page-1">7Vrbbts4EP0aA+6DC118y6PlpO0C7iJYB9jdR1qiJbaUaFB0HOfrdyiSkmjJXiVW2hcXAUoOh0Ny5vBwNMnAX6YvXznaJd9ZhOnAc6KXgX8/8Dx37Dvwn5QclWTm3ClBzEmklSrBmrxiLdTz4j2JcG4pCsaoIDtbGLIsw6GwZIhzdrDVtozaq+5QjBuCdYhoU/o3iUSipHNvWsm/YRInZmV3qs+3QeHPmLN9ptcbeP62+KeGU2Rs6YPmCYrYoSbyHwb+kjMmVCt9WWIqfWvcpuZ9OTNa7pvjTHSaoGfk4mjOjiNwhe4yLhIWswzRh0oaFOfD0oIDvUSkFJouNGFRfvxHyj9PTPdfPfYDC3HUkUZ7wUBUWV8xtittRAsZQehGBKUsi54SkqmBL4SapZoHNSdhex7qo3gaOojHWGvNlEgesjZNO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Y/oox7jNz4dRbmq6BWWfq/TeMr635wzlHbuqNNcKhj6w0PyAuOV8V8avrTjXFr8+kr62lD3QHD48vcYdcj+gTEafsVURswtQT6dlJ+fpkqWiVlYWyKzq2Dvsjqz9gcFacvl+q5+7ihwIUMIi+Um9lW+kE0pPF1/PDtv8wKHQVcAcIw6PXDGgNCMM2RXQT0iwnI5F2HnVW13vd7JxW2Gv7Ta707eXc6Bb/S5d5W/VHyz4D/8B</diagram></mxfile>
<mxfile userAgent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0" version="6.0.2.1" editor="www.draw.io" type="device"><diagram name="Page-1">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/IZmkX4HPE65Uu44rJR4FDUVmlKcQZkHrUqvEN2jjKsyEaMU7TKyLDq8BaWSZM44Z1tVSI9U4GvBKEvzCdCrU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SyrjwCwZfCICPCM6G8lDCq+cwVTj5DYNnjE+s/JVg+u66RY9v0vV+C7OcDW9fcLTPtfupcqhCqrC3Rhe0RrateRuap++nhXB7/JVPyVuOv6Xq3/4f</diagram></mxfile>
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<modelVersion>4.0.0</modelVersion>

<groupId>ioc.MyInjector</groupId>
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/myinjector/AbstractSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,23 @@ 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) {
if(bindingInfo.getName().equals(name)){
return bindingInfo;
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;
}
}
}
System.out.println("Returning null");
return null; //@TODO replace with: display warning there is no such binding
}

public List<BindingInfo> getAllBindingInfos(Class sourceClass){
private List<BindingInfo> getAllBindingInfos(Class sourceClass){
return bindings.get(sourceClass);
}

}
9 changes: 9 additions & 0 deletions src/main/java/myinjector/Annotations/Named.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 Named {
String name();
}
4 changes: 4 additions & 0 deletions src/main/java/myinjector/Annotations/Optional.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package myinjector.Annotations;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Optional {
}
13 changes: 9 additions & 4 deletions src/main/java/myinjector/BindingInfo.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package myinjector;

import myinjector.Scopes.IScope;
import myinjector.Scopes.Prototype;
import myinjector.Scopes.Singleton;

public class BindingInfo {
private Class clazz;
private String name;
private boolean isSingleton = false;
private IScope scope;


public BindingInfo(Class clazz){
this.clazz = clazz;
this.scope = new Prototype();
}

public BindingInfo setSingleton(){
this.isSingleton = true;
this.scope = new Singleton();
return this;
}

Expand All @@ -28,7 +33,7 @@ public String getName() {
return name;
}

public boolean isSingleton(){
return this.isSingleton;
public IScope getScope() {
return scope;
}
}
68 changes: 68 additions & 0 deletions src/main/java/myinjector/Builder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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> 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<Constructor>() {
public int compare(Constructor constr1, Constructor constr2){
if(constr1.getParameterTypes().length >= constr2.getParameterTypes().length)
return -1;
else return 1;
}
});

return constructors[0];
}
}
196 changes: 0 additions & 196 deletions src/main/java/myinjector/Creator.java

This file was deleted.

Loading

0 comments on commit 6630a48

Please sign in to comment.