-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added util class for finding all classes in the current class path
- Loading branch information
1 parent
afbaa0b
commit 30ed4a1
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/de/christianleberfinger/melodies2go/utils/Classes.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,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
33
test/de/christianleberfinger/melodies2go/utils/ClassesTest.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,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("")); | ||
} | ||
|
||
} |