-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#48 Extract cyclic dependency check into a handler, fix optional requ…
…est type
- Loading branch information
Showing
12 changed files
with
215 additions
and
94 deletions.
There are no files selected for viewing
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
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
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
50 changes: 50 additions & 0 deletions
50
injector/src/main/java/ch/jalu/injector/handlers/dependency/CyclicDependenciesDetector.java
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,50 @@ | ||
package ch.jalu.injector.handlers.dependency; | ||
|
||
import ch.jalu.injector.context.ObjectIdentifier; | ||
import ch.jalu.injector.context.ResolutionContext; | ||
import ch.jalu.injector.exceptions.InjectorException; | ||
import ch.jalu.injector.handlers.Handler; | ||
import ch.jalu.injector.handlers.instantiation.Resolution; | ||
|
||
import javax.annotation.Nullable; | ||
import java.lang.reflect.Type; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Detects cycles in the dependencies based on the context's parents. | ||
* This handler should come at the start of the chain so it can stop it with an appropriate error message. | ||
* If not stopped by this handler, cyclic dependencies will cause a StackOverflowException. | ||
*/ | ||
public class CyclicDependenciesDetector implements Handler { | ||
|
||
@Override | ||
public Resolution<?> resolve(ResolutionContext context) { | ||
ObjectIdentifier duplicateIdentifier = findRepeatedIdentifier(context); | ||
if (duplicateIdentifier != null) { | ||
String traversalList = buildParentsList(context); | ||
throw new InjectorException("Found cyclic dependency' - already traversed '" + duplicateIdentifier | ||
+ "' (full traversal list: " + traversalList + " -> " + context.getIdentifier() + ")"); | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
private static ObjectIdentifier findRepeatedIdentifier(ResolutionContext context) { | ||
Set<Type> types = new HashSet<>(); | ||
types.add(context.getIdentifier().getType()); | ||
for (ResolutionContext parent : context.getParents()) { | ||
if (!types.add(parent.getIdentifier().getType())) { | ||
return parent.getIdentifier(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static String buildParentsList(ResolutionContext context) { | ||
return context.getParents().stream() | ||
.map(ctx -> ctx.getIdentifier().getType().getTypeName()) | ||
.collect(Collectors.joining(" -> ")); | ||
} | ||
} |
Oops, something went wrong.