Skip to content

Commit

Permalink
added util class for finding all classes in the current class path
Browse files Browse the repository at this point in the history
  • Loading branch information
krizleebear committed Nov 7, 2022
1 parent afbaa0b commit 30ed4a1
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/de/christianleberfinger/melodies2go/utils/Classes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package de.christianleberfinger.melodies2go.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;

public class Classes {

public static Set<String> findAllClassNamesInClassPath() {
Set<Path> classPathElements = getClassPaths();
Set<String> classes = new LinkedHashSet<>();
classPathElements.forEach(classPathElement -> addAllClassesFrom(classPathElement, classes));
return classes;
}

protected static void addAllClassesFrom(Path p, Set<String> classes) {
if (Files.isDirectory(p)) {
addClassNamesFromDir(p, classes);
} else {
if (isClassFile(p)) {
String className = classNameFromFilePath(p.toString());
classes.add(className);
} else {
addClassNamesFromJar(p, classes);
}
}
}

private static void addClassNamesFromDir(Path dir, Set<String> classNames) {
try (Stream<Path> children = Files.walk(dir)) {
children.filter(Classes::isClassFile)
.forEach(child -> {
Path relativePath = dir.relativize(child);
String className = classNameFromFilePath(relativePath.toString());
classNames.add(className);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void addClassNamesFromJar(Path p, Set<String> classes) {
try (JarFile jar = new JarFile(p.toFile())) {
jar.stream()
.filter(Classes::isClassEntry)
.map(JarEntry::getName)
.map(Classes::classNameFromFilePath)
.forEach(classes::add);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

protected static String classNameFromFilePath(String path) {
path = path.substring(0, path.lastIndexOf(".class"));
return path.replaceAll("/", ".");
}

protected static Set<Path> getClassPaths() {
String classpathProp = System.getProperty("java.class.path");
String[] classPathElements = classpathProp.split(File.pathSeparator);
Set<Path> classPaths = new HashSet<>(classPathElements.length);
for (String element : classPathElements) {
Path path = Paths.get(element);
if (Files.exists(path)) {
classPaths.add(path);
}
}

return classPaths;
}

private static boolean isClassEntry(JarEntry entry) {
return entry.getName().endsWith(".class");
}

private static boolean isClassFile(Path p) {
return Files.isRegularFile(p) &&
p.getFileName().toString().endsWith(".class");
}
}
33 changes: 33 additions & 0 deletions test/de/christianleberfinger/melodies2go/utils/ClassesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.christianleberfinger.melodies2go.utils;

import de.christianleberfinger.melodies2go.M3UWriter;
import org.junit.jupiter.api.Test;

import java.nio.file.Path;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;


public class ClassesTest {

@Test
public void getClassPaths() {
Set<Path> paths = Classes.getClassPaths();
assertNotNull(paths);
assertNotEquals(0, paths.size());
}

@Test
public void getClassNames() {
Set<String> classNames = Classes.findAllClassNamesInClassPath();
assertNotNull(classNames);
assertNotEquals(0, classNames.size());

assertTrue(classNames.contains(Object.class.getName()));
assertTrue(classNames.contains(M3UWriter.class.getName()));
assertTrue(classNames.contains(Classes.class.getName()));
assertFalse(classNames.contains(""));
}

}

0 comments on commit 30ed4a1

Please sign in to comment.