Merge pull request #10171 from sk1418/clsNamesInJar

[BAEL-4533] get class names from a jar
This commit is contained in:
Eric Martin 2020-10-26 22:25:44 -05:00 committed by GitHub
commit 023d307f51
4 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1 @@
!src/test/resources/example-jar/stripe-0.0.1-SNAPSHOT.jar

View File

@ -0,0 +1,43 @@
package com.baeldung.jar;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class GetClassNamesFromJar {
public static Set<String> getClassNamesFromJarFile(File givenFile) throws IOException {
Set<String> classNames = new HashSet<>();
try (JarFile jarFile = new JarFile(givenFile)) {
Enumeration<JarEntry> e = jarFile.entries();
while (e.hasMoreElements()) {
JarEntry jarEntry = e.nextElement();
if (jarEntry.getName().endsWith(".class")) {
String className = jarEntry.getName()
.replace("/", ".")
.replace(".class", "");
classNames.add(className);
}
}
return classNames;
}
}
public static Set<Class> getClassesFromJarFile(File jarFile) throws IOException, ClassNotFoundException {
Set<String> classNames = getClassNamesFromJarFile(jarFile);
Set<Class> classes = new HashSet<>(classNames.size());
try (URLClassLoader cl = URLClassLoader.newInstance(new URL[] { new URL("jar:file:" + jarFile + "!/") })) {
for (String name : classNames) {
Class clazz = cl.loadClass(name); // Loading the class by its name
classes.add(clazz);
}
}
return classes;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.jar;
import com.google.common.collect.Sets;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class GetClassNamesFromJarUnitTest {
private static final String JAR_PATH = "example-jar/stripe-0.0.1-SNAPSHOT.jar";
private static final Set<String> EXPECTED_CLASS_NAMES = Sets.newHashSet(
"com.baeldung.stripe.StripeApplication",
"com.baeldung.stripe.ChargeRequest",
"com.baeldung.stripe.StripeService",
"com.baeldung.stripe.ChargeRequest$Currency",
"com.baeldung.stripe.ChargeController",
"com.baeldung.stripe.CheckoutController");
@Test
public void givenJarFilePath_whenLoadClassNames_thenGetClassNames() throws IOException, URISyntaxException {
File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI());
Set<String> classNames = GetClassNamesFromJar.getClassNamesFromJarFile(jarFile);
Assert.assertEquals(EXPECTED_CLASS_NAMES, classNames);
}
@Test
public void givenJarFilePath_whenLoadClass_thenGetClassObjects() throws IOException, ClassNotFoundException, URISyntaxException {
File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI());
Set<Class> classes = GetClassNamesFromJar.getClassesFromJarFile(jarFile);
Set<String> names = classes.stream().map(Class::getName).collect(Collectors.toSet());
Assert.assertEquals(EXPECTED_CLASS_NAMES, names);
}
}