Skip to content

Commit

Permalink
Remove org.reflections dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <[email protected]>
  • Loading branch information
tadam50 committed Feb 6, 2024
1 parent d2d4067 commit dda21b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
8 changes: 0 additions & 8 deletions diagram-viewer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,5 @@
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<!-- __________________________________________________________________________ -->
<!-- Reflection -->
<!-- __________________________________________________________________________ -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import javafx.stage.FileChooser;
import javafx.util.StringConverter;
import org.apache.commons.lang3.StringUtils;
import org.reflections.*;
import org.reflections.scanners.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;
import java.util.function.Function;
import java.util.jar.*;
import java.util.prefs.Preferences;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -98,7 +98,11 @@ public class MainViewController {

@FXML
private void initialize() {
initializeNetworkFactories();
try {
initializeNetworkFactories();
} catch (IOException | ClassNotFoundException e) {
LOGGER.error(e.toString(), e);
}

sldJsHandler = new SingleLineDiagramJsHandler(vlTree);

Expand Down Expand Up @@ -159,14 +163,37 @@ public TreeItem<Container<?>> fromString(String string) {
sldViewController.addListener((observable, oldValue, newValue) -> updateSldDiagrams());
}

private void initializeNetworkFactories() {
private void initializeNetworkFactories() throws IOException, ClassNotFoundException {
// Add menu item for all classes from powsybl-iidm-test
String packageName = "com.powsybl.iidm.network.test";
// Get all classes using reflections
Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));
Set<Class<?>> testClasses = reflections.getSubTypesOf(Object.class);
// Get actual classloader
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// Get Maven URL dependencies
Enumeration<URL> resources = classLoader.getResources(packageName.replace(".", "/"));
List<String> classNames = new ArrayList<>();
// List all classname for each URL for specified package name
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
if (url.getProtocol().equals("jar")) {
JarURLConnection jarConn = (JarURLConnection) url.openConnection();
try (JarFile jarFile = jarConn.getJarFile()) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class") && name.startsWith(jarConn.getEntryName())) {
classNames.add(name.replace("/", ".").substring(0, name.length() - 6));
}
}
}
}
}
// Sort in alpha order
Collections.sort(classNames);
// Populate Networks list
List<MenuItem> items = new ArrayList<>();
for (Class<?> clazz : testClasses) {
for (String className : classNames) {
Class<?> clazz = Class.forName(className);
// Keep only classes with create() method
if (Arrays.stream(clazz.getDeclaredMethods()).anyMatch(method -> method.getName().equals("create") && method.getParameterCount() == 0)) {
// Build menu item
Expand Down

0 comments on commit dda21b9

Please sign in to comment.