#932 Create class collector and use it where applicable

- Extract logic for walking through a directory and loading its classes into a separate class
- Replace all implementations with the new ClassCollector
This commit is contained in:
ljacqu
2016-09-07 22:50:12 +02:00
parent f63871600a
commit 10493a3fa3
7 changed files with 210 additions and 250 deletions
@@ -5,6 +5,8 @@ import ch.jalu.injector.handlers.instantiation.Instantiation;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import fr.xephi.authme.ClassCollector;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.converter.Converter;
import fr.xephi.authme.initialization.DataFolder;
@@ -16,7 +18,6 @@ import tools.utils.InjectorUtils;
import tools.utils.ToolTask;
import tools.utils.ToolsConstants;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
@@ -34,15 +35,12 @@ import java.util.Scanner;
public class DrawDependency implements ToolTask {
private static final String DOT_FILE = ToolsConstants.TOOLS_SOURCE_ROOT + "dependencygraph/graph.dot";
// Package root
private static final String ROOT_PACKAGE = "fr.xephi.authme";
private static final List<Class<?>> SUPER_TYPES = ImmutableList.of(ExecutableCommand.class,
SynchronousProcess.class, AsynchronousProcess.class, EncryptionMethod.class, Converter.class, Listener.class);
/** Annotation types by which dependencies are identified. */
private static final List<Class<? extends Annotation>> ANNOTATION_TYPES =
ImmutableList.<Class<? extends Annotation>>of(DataFolder.class);
private static final List<Class<? extends Annotation>> ANNOTATION_TYPES = ImmutableList.of(DataFolder.class);
private boolean mapToSupertype;
// Map with the graph's nodes: value is one of the key's dependencies
@@ -59,7 +57,10 @@ public class DrawDependency implements ToolTask {
mapToSupertype = "y".equalsIgnoreCase(scanner.nextLine());
// Gather all connections
readAndProcessFiles(new File(ToolsConstants.MAIN_SOURCE_ROOT));
ClassCollector collector = new ClassCollector(TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT);
for (Class<?> clazz : collector.collectClasses()) {
processClass(clazz);
}
// Prompt user for simplification of graph
System.out.println("Do you want to remove classes that are not used as dependency elsewhere?");
@@ -93,28 +94,6 @@ public class DrawDependency implements ToolTask {
System.out.format("Run 'dot -Tpng %s -o graph.png' to generate image (requires GraphViz)%n", DOT_FILE);
}
/**
* Recursively reads the given directory and processes the files.
*
* @param dir the directory to read
*/
private void readAndProcessFiles(File dir) {
File[] files = dir.listFiles();
if (files == null) {
throw new IllegalStateException("Cannot read folder '" + dir + "'");
}
for (File file : files) {
if (file.isDirectory()) {
readAndProcessFiles(file);
} else if (file.isFile()) {
Class<?> clazz = loadClass(file);
if (clazz != null) {
processClass(clazz);
}
}
}
}
private void processClass(Class<?> clazz) {
List<String> dependencies = getDependencies(clazz);
if (dependencies != null) {
@@ -134,21 +113,6 @@ public class DrawDependency implements ToolTask {
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, ".");
if (!fileName.endsWith(".java")) {
return null;
}
final String className = fileName
.substring(fileName.indexOf(ROOT_PACKAGE), fileName.length() - ".java".length());
try {
return DrawDependency.class.getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
private List<String> getDependencies(Class<?> clazz) {
Instantiation<?> instantiation = InjectorUtils.getInstantiationMethod(clazz);
return instantiation == null ? null : formatInjectionDependencies(instantiation);