Skip to content

Commit b517080

Browse files
Lucas Almeidalucasandre22
authored andcommitted
Add maven project and readme
0 parents  commit b517080

13 files changed

Lines changed: 366 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Java dependency injection container

container/.classpath

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
<attribute name="test" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
<attribute name="test" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
28+
<attributes>
29+
<attribute name="maven.pomderived" value="true"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
33+
<attributes>
34+
<attribute name="maven.pomderived" value="true"/>
35+
</attributes>
36+
</classpathentry>
37+
<classpathentry kind="output" path="target/classes"/>
38+
</classpath>

container/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

container/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>container</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding//src/test/java=UTF-8
5+
encoding//src/test/resources=UTF-8
6+
encoding/<project>=UTF-8
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
5+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
6+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
7+
org.eclipse.jdt.core.compiler.release=disabled
8+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

container/pom.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.dicontainer</groupId>
7+
<artifactId>container</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>com.google.code.gson</groupId>
26+
<artifactId>gson</artifactId>
27+
<version>2.9.0</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.reflections</groupId>
32+
<artifactId>reflections</artifactId>
33+
<version>0.10.2</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.projectlombok</groupId>
38+
<artifactId>lombok</artifactId>
39+
<version>1.18.22</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-javadoc-plugin</artifactId>
49+
<version>2.10.4</version>
50+
<executions>
51+
<execution>
52+
<id>attach-javadocs</id>
53+
<goals>
54+
<goal>jar</goal>
55+
</goals>
56+
</execution>
57+
</executions>
58+
</plugin>
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-source-plugin</artifactId>
62+
<version>3.0.1</version>
63+
<executions>
64+
<execution>
65+
<id>attach-sources</id>
66+
<goals>
67+
<goal>jar</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-assembly-plugin</artifactId>
75+
<version>3.0.0</version>
76+
<executions>
77+
<execution>
78+
<phase>package</phase>
79+
<goals>
80+
<goal>single</goal>
81+
</goals>
82+
</execution>
83+
</executions>
84+
<configuration>
85+
<descriptorRefs>
86+
<descriptorRef>jar-with-dependencies</descriptorRef>
87+
</descriptorRefs>
88+
<archive>
89+
<manifest>
90+
<mainClass>com.dicontainer.App</mainClass>
91+
</manifest>
92+
</archive>
93+
</configuration>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
98+
</project>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.dicontainer;
2+
3+
import java.io.IOException;
4+
import java.lang.reflect.Constructor;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.util.Map;
8+
9+
import org.reflections.Reflections;
10+
11+
import com.dicontainer.annotations.Dependency;
12+
import com.dicontainer.annotations.ToInject;
13+
import com.google.gson.Gson;
14+
import com.google.gson.GsonBuilder;
15+
16+
import lombok.Getter;
17+
import lombok.Setter;
18+
19+
public class Container {
20+
21+
private static Container INSTANCE;
22+
public static final String CONFIG_FILE = "dicontainer.config";
23+
private static final Gson GSON = new Gson();
24+
private static final GsonBuilder GSON_BUILDER = new GsonBuilder();
25+
// Interface -> dependency
26+
private Map<Class, DependencyToInject> dependenciesToInjectByInterface;
27+
private Map<Class, Constructor> constructorsToInject;
28+
private Map<Class, Constructor> dependenciesConstructor;
29+
private Reflections reflections = new Reflections();
30+
private ConfigHolder configHolder;
31+
32+
//procurar classe, procurar metodo com @toinject annotation
33+
static {
34+
GSON_BUILDER.setPrettyPrinting();
35+
//public <T> T fromJson(String json, Class<T> classOfT)
36+
}
37+
38+
private class ConfigHolder {
39+
//interface -> class
40+
private Map<String, String> config;
41+
}
42+
43+
@Getter @Setter
44+
private class DependencyToInject <T> {
45+
private Constructor<T> constructorToCall; //Constructor of the dependency to call.
46+
private Class<T> dependencyClass; //Class to be instantiated.
47+
private String annotatedValue; //Value of the Dependency annotation.
48+
49+
public DependencyToInject(Constructor<T> constructor, Class<T> clazz, String annotation) {
50+
constructorToCall = constructor;
51+
dependencyClass = clazz;
52+
annotatedValue = annotation;
53+
}
54+
55+
public T instantiate() {
56+
T instance = null;
57+
try {
58+
instance = constructorToCall.newInstance();
59+
} catch(Exception e) {
60+
61+
}
62+
return instance;
63+
}
64+
}
65+
66+
private Container() {
67+
//loadConfig();
68+
loadDependencies();
69+
loadConstructorsToInject();
70+
}
71+
72+
private Constructor<?> getConstructorToInstantiateDependency(Class<?> dependency) {
73+
for(Constructor<?> constructor : dependency.getConstructors()) {
74+
if(constructor.getDeclaredAnnotation(ToInject.class) != null)
75+
return constructor;
76+
}
77+
return null;
78+
}
79+
80+
private <V> void loadDependencies() {
81+
for(Class<?> clazz : reflections.getTypesAnnotatedWith(Dependency.class)) {
82+
Constructor<?> constructorToCall = getConstructorToInstantiateDependency(clazz);
83+
String annotatedValue = clazz.getDeclaredAnnotation(Dependency.class).to();
84+
Class referredInterface;
85+
try {
86+
referredInterface = Class.forName(annotatedValue);
87+
} catch(Exception e) {
88+
continue;
89+
}
90+
test(constructorToCall, clazz, annotatedValue);
91+
dependenciesToInjectByInterface.put(referredInterface, new DependencyToInject(constructorToCall, clazz, annotatedValue));
92+
}
93+
}
94+
95+
// Test if it can work
96+
private <V> void test(Constructor<V> a, Class<V> b, String c) {
97+
DependencyToInject a = new DependencyToInject<V>(a, b, c);
98+
}
99+
100+
private void loadConstructorsToInject() {
101+
for(Constructor<?> constructor : reflections.getConstructorsAnnotatedWith(ToInject.class)) {
102+
constructorsToInject.put(constructor.getClass(), constructor);
103+
}
104+
}
105+
106+
private void loadConfig() {
107+
String fileContent = null;
108+
try {
109+
fileContent = getFileContent(CONFIG_FILE);
110+
} catch(IOException e) {
111+
//throw e;
112+
}
113+
configHolder = GSON.fromJson(fileContent, ConfigHolder.class);
114+
}
115+
116+
private String getFileContent(String filename) throws IOException {
117+
Path filePath = Path.of(filename);
118+
return Files.readString(filePath);
119+
}
120+
121+
private synchronized static Container getInstance() {
122+
if(INSTANCE == null)
123+
INSTANCE = new Container();
124+
return INSTANCE;
125+
}
126+
127+
public Object getNewInstanceFor(Class dependencyReceiver) throws Exception {
128+
if(dependenciesToInjectByInterface.containsKey(dependencyReceiver)) {
129+
DependencyToInject toInject = dependenciesToInjectByInterface.get(dependencyReceiver);
130+
return toInject.getConstructorToCall().invoke(dependenciesToInjectByInterface.get(dependencyReceiver), (Object[]) null);
131+
}
132+
return null;
133+
}
134+
135+
public static Object getInstanceFor(Class classToInstatiate) throws Exception
136+
{
137+
return Container.getInstance().getNewInstanceFor(classToInstatiate);
138+
}
139+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.dicontainer.annotations;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
//On class
7+
@Retention(RetentionPolicy.RUNTIME)
8+
public @interface Dependency {
9+
//The dependency will tell which class it is going to be injected.
10+
public String to();
11+
}

0 commit comments

Comments
 (0)