From eee06dad50de165adabfe8abb812571f0b60f5cb Mon Sep 17 00:00:00 2001 From: ljacqu Date: Fri, 27 May 2016 23:25:11 +0200 Subject: [PATCH] Dependency graph - allow to summarize dependencies by super type --- .../tools/dependencygraph/DrawDependency.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/test/java/tools/dependencygraph/DrawDependency.java b/src/test/java/tools/dependencygraph/DrawDependency.java index b1fe02435..706a734e7 100644 --- a/src/test/java/tools/dependencygraph/DrawDependency.java +++ b/src/test/java/tools/dependencygraph/DrawDependency.java @@ -1,10 +1,16 @@ package tools.dependencygraph; -import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Multimap; +import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.converter.Converter; import fr.xephi.authme.initialization.ConstructorInjection; import fr.xephi.authme.initialization.FieldInjection; import fr.xephi.authme.initialization.Injection; +import fr.xephi.authme.process.AsynchronousProcess; +import fr.xephi.authme.process.SynchronousProcess; +import fr.xephi.authme.security.crypts.EncryptionMethod; import tools.utils.ToolTask; import tools.utils.ToolsConstants; @@ -28,8 +34,12 @@ public class DrawDependency implements ToolTask { // Package root private static final String ROOT_PACKAGE = "fr.xephi.authme"; + private static final List> SUPER_TYPES = ImmutableList.of(ExecutableCommand.class, + SynchronousProcess.class, AsynchronousProcess.class, EncryptionMethod.class, Converter.class); + + private boolean mapToSupertype; // Map with the graph's nodes: value is one of the key's dependencies - private Multimap, String> foundDependencies = ArrayListMultimap.create(); + private Multimap, String> foundDependencies = HashMultimap.create(); @Override public String getTaskName() { @@ -38,6 +48,9 @@ public class DrawDependency implements ToolTask { @Override public void execute(Scanner scanner) { + System.out.println("Summarize classes to their generic super type where applicable?"); + mapToSupertype = "y".equalsIgnoreCase(scanner.nextLine()); + // Gather all connections readAndProcessFiles(new File(ToolsConstants.MAIN_SOURCE_ROOT)); @@ -98,10 +111,22 @@ public class DrawDependency implements ToolTask { private void processClass(Class clazz) { List dependencies = getDependencies(clazz); if (dependencies != null) { - foundDependencies.putAll(clazz, dependencies); + foundDependencies.putAll(mapToSuper(clazz), dependencies); } } + private Class mapToSuper(Class clazz) { + if (!mapToSupertype || clazz == null) { + return clazz; + } + for (Class parent : SUPER_TYPES) { + if (parent.isAssignableFrom(clazz)) { + return parent; + } + } + return clazz; + } + // Load Class object for the class in the given file private static Class loadClass(File file) { final String fileName = file.getPath().replace(File.separator, "."); @@ -117,7 +142,7 @@ public class DrawDependency implements ToolTask { } } - private static List getDependencies(Class clazz) { + private List getDependencies(Class clazz) { Injection injection = ConstructorInjection.provide(clazz).get(); if (injection != null) { return formatInjectionDependencies(injection); @@ -134,7 +159,7 @@ public class DrawDependency implements ToolTask { * @param injection the injection whose dependencies should be formatted * @return list of dependencies in a friendly format */ - private static List formatInjectionDependencies(Injection injection) { + private List formatInjectionDependencies(Injection injection) { Class[] dependencies = injection.getDependencies(); Class[] annotations = injection.getDependencyAnnotations(); @@ -143,7 +168,7 @@ public class DrawDependency implements ToolTask { if (annotations[i] != null) { result.add("@" + annotations[i].getSimpleName()); } else { - result.add(dependencies[i].getSimpleName()); + result.add(mapToSuper(dependencies[i]).getSimpleName()); } } return result;