Skip to content

Commit 097489c

Browse files
saikatcse03saikat chakraborty
andauthored
Bael 4241 (eugenp#11365)
* Added all class loaded listing * Added all class loaded listing Signed-off-by: saikatcse03 <[email protected]> * Updated class count * Updated class count test and fixed tabs * Removed spaces * Updated version through Properties Co-authored-by: saikat chakraborty <[email protected]>
1 parent 6f32e03 commit 097489c

3 files changed

Lines changed: 89 additions & 1 deletion

File tree

core-java-modules/core-java-jvm-2/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,23 @@
2626
<artifactId>jol-core</artifactId>
2727
<version>${jol-core.version}</version>
2828
</dependency>
29+
<dependency>
30+
<groupId>org.reflections</groupId>
31+
<artifactId>reflections</artifactId>
32+
<version>${reflections.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.google.guava</groupId>
36+
<artifactId>guava</artifactId>
37+
<version>${guava.version}</version>
38+
</dependency>
2939
</dependencies>
3040

3141
<properties>
3242
<assertj.version>3.6.1</assertj.version>
3343
<jol-core.version>0.10</jol-core.version>
44+
<reflections.version>0.10.2</reflections.version>
45+
<guava.version>31.0.1-jre</guava.version>
3446
</properties>
3547

36-
</project>
48+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.loadedclasslisting;
2+
3+
import java.io.IOException;
4+
import java.util.Set;
5+
import java.util.stream.Collectors;
6+
7+
import org.reflections.Reflections;
8+
import org.reflections.scanners.SubTypesScanner;
9+
10+
import com.google.common.collect.ImmutableSet;
11+
import com.google.common.reflect.ClassPath;
12+
import com.google.common.reflect.ClassPath.ClassInfo;
13+
14+
public class ListLoadedClass {
15+
16+
public ImmutableSet<ClassInfo> listClassLoaded() throws IOException {
17+
return ClassPath.from(ListLoadedClass.class.getClassLoader())
18+
.getAllClasses();
19+
}
20+
21+
public Set<Class> listClassLoaded(String packageName) throws IOException {
22+
return ClassPath.from(ClassLoader.getSystemClassLoader()).getAllClasses().stream()
23+
.filter(clazz -> clazz.getPackageName().equals(packageName))
24+
.map(ClassInfo::load)
25+
.collect(Collectors.toSet());
26+
}
27+
28+
public Set<Class> findAllClassesUsingReflectionsLibrary(String packageName) {
29+
Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));
30+
return reflections.getSubTypesOf(Object.class)
31+
.stream()
32+
.collect(Collectors.toSet());
33+
}
34+
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.loadedclasslisting;
2+
3+
import java.io.IOException;
4+
import java.util.Set;
5+
6+
import org.junit.Test;
7+
import org.junit.jupiter.api.Assertions;
8+
9+
import com.baeldung.loadedclasslisting.ListLoadedClass;
10+
import com.google.common.collect.ImmutableSet;
11+
import com.google.common.reflect.ClassPath;
12+
import com.google.common.reflect.ClassPath.ClassInfo;
13+
14+
public class ListLoadedClassUnitTest {
15+
16+
private static final String PACKAGE_NAME = "com.baeldung.loadedclasslisting";
17+
18+
@Test
19+
public void when_findAllClassesUsingReflectionsLibrary_thenSuccess() {
20+
ListLoadedClass instance = new ListLoadedClass();
21+
Set<Class> classes = instance.findAllClassesUsingReflectionsLibrary(PACKAGE_NAME);
22+
23+
Assertions.assertEquals(4, classes.size());
24+
}
25+
26+
@Test
27+
public void when_findAllClassesUsingGuavaLibrary_InPackage_thenSuccess() throws IOException {
28+
ListLoadedClass instance = new ListLoadedClass();
29+
Set<Class> classes = instance.listClassLoaded(PACKAGE_NAME);
30+
31+
Assertions.assertEquals(4, classes.size());
32+
}
33+
34+
@Test
35+
public void when_findAllClassesUsingGuavaLibrary_thenSuccess() throws IOException {
36+
ListLoadedClass instance = new ListLoadedClass();
37+
Set<ClassInfo> classes = instance.listClassLoaded();
38+
39+
Assertions.assertTrue(4<classes.size());
40+
}
41+
}

0 commit comments

Comments
 (0)