Skip to content

Commit

Permalink
Added reflection testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Voinea-Radu committed Sep 5, 2024
1 parent d4458b7 commit cd60fd0
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 33 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ dependencies {
// Tests
testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testImplementation(libs.slf4j.log4j)
testImplementation(libs.log4j)
}

tasks {
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ jetbrains_annotations = { module = "org.jetbrains:annotations", version = "24.1.
# Dependencies
gson = { module = "com.google.code.gson:gson", version = "2.11.0" }
jedis = { module = "redis.clients:jedis", version = "5.1.5" }
slf4j = { module = "org.slf4j:slf4j-api", version = "2.0.16" }
apache_commons_compress = { module = "org.apache.commons:commons-compress", version = "1.27.1" }
apache_commons_lang3 = { module = "org.apache.commons:commons-lang3", version = "3.16.0" }
apache_commons_pool2 = { module = "org.apache.commons:commons-pool2", version = "2.12.0" }
apache_commons_io = { module = "commons-io:commons-io", version = "2.16.1" }
slf4j = { module = "org.slf4j:slf4j-api", version = "2.0.16" }
slf4j_log4j = { module = "org.slf4j:slf4j-log4j12", version = "2.0.16" }
log4j = { module = "org.apache.logging.log4j:log4j-core", version = "2.23.1" }


5 changes: 5 additions & 0 deletions src/main/java/com.voinearadu/logger/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public static void debug(Object object) {
log(Level.DEBUG, object, ConsoleColor.WHITE, 1);
}

@SuppressWarnings("unused")
public static void debug(Object object, ConsoleColor color) {
log(Level.DEBUG, object, color, 1);
}

public static void good(Object object) {
log(Level.INFO, object, ConsoleColor.GREEN, 1);
}
Expand Down
99 changes: 68 additions & 31 deletions src/main/java/com.voinearadu/reflections/Reflections.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.voinearadu.logger.Logger;
import lombok.Getter;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -21,11 +22,23 @@
public class Reflections {

private final List<File> zipFiles;
private final List<File> folders;
private final ClassLoader classLoader;
private final List<String> searchDomain;

public Reflections(List<File> zipFiles, ClassLoader classLoader, String... searchDomain) {
public Reflections(@NotNull List<File> zipFiles, @NotNull List<File> folders, @NotNull ClassLoader classLoader, @NotNull String... searchDomain) {
this.zipFiles = zipFiles;
this.folders = folders;

String[] classPathEntries = System.getProperty("java.class.path").split(File.pathSeparator);
for (String classPathEntry : classPathEntries) {
if (classPathEntry.endsWith(".jar") || classPathEntry.endsWith(".zip")) {
zipFiles.add(new File(classPathEntry));
} else {
folders.add(new File(classPathEntry));
}
}

this.classLoader = classLoader;
this.searchDomain = Arrays.stream(searchDomain).map(domain -> {
if (!domain.endsWith(".")) {
Expand All @@ -35,7 +48,6 @@ public Reflections(List<File> zipFiles, ClassLoader classLoader, String... searc
}).toList();
}

@SuppressWarnings("unused")
public static @Nullable Field getField(@NotNull Class<?> clazz, String fieldName) {
Field field = null;

Expand Down Expand Up @@ -84,20 +96,24 @@ public Reflections(List<File> zipFiles, ClassLoader classLoader, String... searc

@SuppressWarnings("unused")
public Reflections from(String... searchDomain) {
return new Reflections(zipFiles, classLoader, searchDomain);
return new Reflections(zipFiles, folders, classLoader, searchDomain);
}

public @NotNull Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();

for (File zipFile : zipFiles) {
classes.addAll(getClasses(zipFile));
classes.addAll(getClassesFromZip(zipFile));
}

for (File zipFile : folders) {
classes.addAll(getClassesFromFolder(zipFile));
}

return classes;
}

private @NotNull Set<Class<?>> getClasses(File zipFile) {
private @NotNull Set<Class<?>> getClassesFromZip(File zipFile) {
Set<Class<?>> classes = new HashSet<>();

try (ZipFile zip = new ZipFile(zipFile)) {
Expand All @@ -106,39 +122,32 @@ public Reflections from(String... searchDomain) {
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();

if (entry == null)
break;

if (entry.isDirectory()) {
if (entry == null || entry.isDirectory()) {
continue;
}

String name = entry.getName();
Class<?> clazz = processFile(entry.getName());

if (!name.endsWith(".class")) {
continue;
}

name = name.replace("/", ".");
name = name.replace(".class", "");

if (searchDomain.stream().noneMatch(name::startsWith)) {
continue;
if (clazz != null) {
classes.add(clazz);
}
}
} catch (Exception error) {
Logger.error(error);
}

String simpleClassName = name.substring(name.lastIndexOf('.') + 1);
return classes;
}

// Skip Mixin classes
if (simpleClassName.contains("Mixin")) {
continue;
}
private @NotNull Set<Class<?>> getClassesFromFolder(File folder) {
Set<Class<?>> classes = new HashSet<>();

try {
for (File file : FileUtils.listFiles(folder, new String[]{"class"}, true)) {
Class<?> clazz = processFile(file.getAbsolutePath().replace(folder.getAbsolutePath() + File.separator, ""));

try {
classes.add(classLoader.loadClass(name));
} catch (Throwable throwable) {
Logger.error("Failed to load class " + name + " from " + zipFile.getName());
Logger.error(throwable);
if (clazz != null) {
classes.add(clazz);
}
}
} catch (Exception error) {
Expand All @@ -148,7 +157,36 @@ public Reflections from(String... searchDomain) {
return classes;
}

@SuppressWarnings("unused")
private @Nullable Class<?> processFile(String fileName) {
if (!fileName.endsWith(".class")) {
return null;
}

fileName = fileName.replace("/", ".");
fileName = fileName.replace("\\", ".");
fileName = fileName.replace(".class", "");

if (searchDomain.stream().noneMatch(fileName::startsWith)) {
return null;
}

String simpleClassName = fileName.substring(fileName.lastIndexOf('.') + 1);

// Skip Mixin classes
if (simpleClassName.contains("Mixin")) {
return null;
}

try {
return classLoader.loadClass(fileName);
} catch (Throwable throwable) {
Logger.error("Failed to load class " + fileName + " from " + fileName);
Logger.error(throwable);
}

return null;
}

public @NotNull Set<Class<?>> getTypesAnnotatedWith(@NotNull Class<? extends Annotation> annotation) {
Set<Class<?>> classes = new HashSet<>();

Expand All @@ -161,7 +199,6 @@ public Reflections from(String... searchDomain) {
return classes;
}

@SuppressWarnings("unused")
public @NotNull Set<Method> getMethodsAnnotatedWith(@NotNull Class<? extends Annotation> annotation) {
Set<Method> methods = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.voinearadu.event_manager.dto.TestComplexEvent;
import com.voinearadu.event_manager.dto.TestEvent;
import com.voinearadu.event_manager.manager.TestEventListener;
import org.apache.log4j.BasicConfigurator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -17,6 +18,7 @@ public class EventManagerTests {

@BeforeAll
public static void setup() {
BasicConfigurator.configure();
eventManager.register(new TestEventListener());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.GsonBuilder;
import com.voinearadu.file_manager.dto.files.FileObject;
import com.voinearadu.file_manager.manager.FileManager;
import org.apache.log4j.BasicConfigurator;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -20,6 +21,7 @@ public class FileManagerTests {

@BeforeAll
public static void init() {
BasicConfigurator.configure();
Gson gson = new GsonBuilder() //NOPMD - suppressed GsonCreatedForEachMethodCall
.create();
fileManager = new FileManager(() -> gson, "tmp");
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/voinearadu/file_manager/GsonTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.voinearadu.file_manager.dto.serializable.SerializableMap;
import com.voinearadu.file_manager.dto.serializable.SerializableObject;
import lombok.Getter;
import org.apache.log4j.BasicConfigurator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -32,6 +33,7 @@ public class GsonTests {

@BeforeAll
public static void init() {
BasicConfigurator.configure();
ClassLoader classLoader = GsonTests.class.getClassLoader();

SerializableListGsonTypeAdapter serializableListGsonTypeAdapter = new SerializableListGsonTypeAdapter(classLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.voinearadu.lambda.lambda.*;
import lombok.SneakyThrows;
import org.apache.log4j.BasicConfigurator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -17,8 +18,8 @@ public class LambdaRunnableExecutorTest {

@BeforeAll
public static void init() {
BasicConfigurator.configure();
}

@Test
public void testLambdaExecutors() {
ArgLambdaExecutor<List<String>> addEmpty = (list) -> list.add("empty");
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/voinearadu/logger/LoggerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.voinearadu.logger;

import lombok.SneakyThrows;
import org.apache.log4j.BasicConfigurator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
Expand All @@ -14,6 +16,11 @@
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoggerTest {

@BeforeAll
public static void init() {
BasicConfigurator.configure();
}

@SneakyThrows
@Test
public void testDebugLogger() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.voinearadu.message_builder;


import org.apache.log4j.BasicConfigurator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -11,6 +13,11 @@

public class MessageBuilderTests {

@BeforeAll
public static void init() {
BasicConfigurator.configure();
}

@Test
public void testMessageBuilder() {
MessageBuilder builder1 = new MessageBuilder("This is a %placeholder%");
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/voinearadu/redis_manager/RedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.voinearadu.redis_manager.manager.RedisManager;
import com.voinearadu.redis_manager.manager.TestListener;
import lombok.Getter;
import org.apache.log4j.BasicConfigurator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -32,6 +33,7 @@ public class RedisTest {

@BeforeAll
public static void init() {
BasicConfigurator.configure();
MessageBuilderManager.init(false);

ClassLoader classLoader = RedisTest.class.getClassLoader();
Expand Down
Loading

0 comments on commit cd60fd0

Please sign in to comment.