File tree Expand file tree Collapse file tree
core-java-modules/core-java-jvm-2
main/java/com/baeldung/loadedclasslisting
test/java/com/baeldung/loadedclasslisting Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments