* Added all class loaded listing

* Added all class loaded listing

Signed-off-by: saikatcse03 <saikatcse03@gmail.com>

* Updated class count

* Updated class count test and fixed tabs

* Removed spaces

* Updated version through Properties

Co-authored-by: saikat chakraborty <saikat.chakraborty@tesco.com>
This commit is contained in:
saikatcse03 2021-11-09 07:23:28 +05:30 committed by GitHub
parent 4786fb5cf6
commit d3033fbcb5
3 changed files with 89 additions and 1 deletions

View File

@ -26,11 +26,23 @@
<artifactId>jol-core</artifactId> <artifactId>jol-core</artifactId>
<version>${jol-core.version}</version> <version>${jol-core.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>
<jol-core.version>0.10</jol-core.version> <jol-core.version>0.10</jol-core.version>
<reflections.version>0.10.2</reflections.version>
<guava.version>31.0.1-jre</guava.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,35 @@
package com.baeldung.loadedclasslisting;
import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;
public class ListLoadedClass {
public ImmutableSet<ClassInfo> listClassLoaded() throws IOException {
return ClassPath.from(ListLoadedClass.class.getClassLoader())
.getAllClasses();
}
public Set<Class> listClassLoaded(String packageName) throws IOException {
return ClassPath.from(ClassLoader.getSystemClassLoader()).getAllClasses().stream()
.filter(clazz -> clazz.getPackageName().equals(packageName))
.map(ClassInfo::load)
.collect(Collectors.toSet());
}
public Set<Class> findAllClassesUsingReflectionsLibrary(String packageName) {
Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));
return reflections.getSubTypesOf(Object.class)
.stream()
.collect(Collectors.toSet());
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.loadedclasslisting;
import java.io.IOException;
import java.util.Set;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import com.baeldung.loadedclasslisting.ListLoadedClass;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;
public class ListLoadedClassUnitTest {
private static final String PACKAGE_NAME = "com.baeldung.loadedclasslisting";
@Test
public void when_findAllClassesUsingReflectionsLibrary_thenSuccess() {
ListLoadedClass instance = new ListLoadedClass();
Set<Class> classes = instance.findAllClassesUsingReflectionsLibrary(PACKAGE_NAME);
Assertions.assertEquals(4, classes.size());
}
@Test
public void when_findAllClassesUsingGuavaLibrary_InPackage_thenSuccess() throws IOException {
ListLoadedClass instance = new ListLoadedClass();
Set<Class> classes = instance.listClassLoaded(PACKAGE_NAME);
Assertions.assertEquals(4, classes.size());
}
@Test
public void when_findAllClassesUsingGuavaLibrary_thenSuccess() throws IOException {
ListLoadedClass instance = new ListLoadedClass();
Set<ClassInfo> classes = instance.listClassLoaded();
Assertions.assertTrue(4<classes.size());
}
}