Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down
76 changes: 74 additions & 2 deletions src/main/java/ru/spbau/mit/Injector.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package ru.spbau.mit;

import java.util.List;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;


public final class Injector {
private static Map<String, Object> alreadyCreatedObjects = new HashMap<>();
private static Set<String> enqueued = new HashSet<>();

private Injector() {
}

Expand All @@ -12,6 +19,71 @@ private Injector() {
* `implementationClassNames` for concrete dependencies.
*/
public static Object initialize(String rootClassName, List<String> implementationClassNames) throws Exception {
throw new IllegalStateException();
try {
return initialize0(rootClassName, implementationClassNames);
} finally {
deinitializeStructures();
}
}

private static void deinitializeStructures() {
alreadyCreatedObjects.clear();
enqueued.clear();
}

private static Object initialize0(String rootClassName, List<String> implementationClassNames)
throws ImplementationNotFoundException,
ClassNotFoundException,
AmbiguousImplementationException,
InjectionCycleException {
if (alreadyCreatedObjects.containsKey(rootClassName)) {
return alreadyCreatedObjects.get(rootClassName);
}
if (enqueued.contains(rootClassName)) {
return new InjectionCycleException();
}
enqueued.add(rootClassName);

Class rootClass = Class.forName(rootClassName);
Constructor rootConstructor = rootClass.getConstructors()[0];
List<Object> args = new ArrayList<>();
for (Class<?> dependencyClass : rootConstructor.getParameterTypes()) {
Predicate<String> predicate = (implName) -> {
try {
Class implementationClass = Class.forName(implName);
return dependencyClass.isAssignableFrom(implementationClass);
} catch (ClassNotFoundException e) {
throw new RuntimeException();
}
};
List<String> allowedClasses = implementationClassNames
.stream()
.filter(predicate)
.collect(Collectors.toList());

if (allowedClasses.size() == 1) {
String allowedClass = allowedClasses.get(0);
Object arg = initialize0(allowedClass, implementationClassNames);
args.add(arg);
} else if (allowedClasses.size() > 1) {
throw new AmbiguousImplementationException();
} else {
if (enqueued.stream().anyMatch(predicate)) {
throw new InjectionCycleException();
} else {
throw new ImplementationNotFoundException();
}
}
}

Object rootObject;
try {
rootObject = rootConstructor.newInstance(args.toArray());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException();
}
enqueued.remove(rootClassName);
alreadyCreatedObjects.put(rootClassName, rootObject);
return rootObject;
}
}