diff --git a/build.gradle b/build.gradle index baf5167..965e1ff 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'fr.sandro642.github' -version = '0.3.3-STABLE' +version = '0.3.4-STABLE' tasks.register('printVersion') { doLast { diff --git a/readme.md b/readme.md index 5ac44bc..3314cd9 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ And if you thought APIs were complicated, think again! With ConnectLib, it's lik --- ```java -Stable Version: 0.3.3-STABLE +Stable Version: 0.3.4-STABLE ``` --- @@ -58,6 +58,7 @@ Changelog: - [0.2.6.4-STABLE]: Added asynchronous job execution, allowing you to run tasks in the background without blocking your main application thread. - [0.2.7.2-STABLE]: Remove implementation Project Reactor - [0.2.9-STABLE]: Added support query variables in routes, allowing you to pass parameters directly in the URL. + - [0.3.3-STABLE]: Minecraft Version 1.16 - Latest Version support + LangType 2.0 - Remove AnnotHandler, you can use logger.showLogs(); to display logs. ``` --- diff --git a/src/main/java/fr/sandro642/github/annotations/AnnotHandler.java b/src/main/java/fr/sandro642/github/annotations/AnnotHandler.java deleted file mode 100644 index d33fe2c..0000000 --- a/src/main/java/fr/sandro642/github/annotations/AnnotHandler.java +++ /dev/null @@ -1,145 +0,0 @@ -package fr.sandro642.github.annotations; - -import fr.sandro642.github.ConnectLib; -import fr.sandro642.github.enums.lang.CategoriesType; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.LinkedList; -import java.util.List; -import java.io.File; -import java.net.URL; -import java.util.ArrayList; -import java.util.Map; - -/** - * AnnotHandler is a class that manages the discovery and execution of listeners annotated with @AnnotConnect. - * It scans the package for classes that implement the ConnectListener interface and invokes methods annotated with @AnnotConnect. - * - * @author Sandro642 - * @version 1.0 - * @see ConnectListener - * @see AnnotConnect - */ - -public class AnnotHandler { - - private ConnectLib connectLib = new ConnectLib(); - - /** - * AnnotConnect is an annotation used to mark methods that should be executed when a connection event occurs. - * Methods annotated with this annotation will be invoked by the AnnotHandler when it discovers them. - */ - @Retention(RetentionPolicy.RUNTIME) - @Target(ElementType.METHOD) - public @interface AnnotConnect { } - - /** - * ConnectListener is an interface that should be implemented by classes that want to listen for connection events. - * Classes implementing this interface can have methods annotated with @AnnotConnect to handle specific connection events. - */ - public interface ConnectListener {} - - /** - * listeners is a list that holds instances of classes implementing the ConnectListener interface. - * It is populated by the discoverListeners method, which scans the package for classes that implement ConnectListener. - * The methods of these classes can be annotated with @AnnotConnect to indicate that they should be executed when a connection event occurs. - */ - private final List listeners = new LinkedList<>(); - - /** - * discoverListeners is a method that scans the package of the AnnotHandler class for classes that implement the ConnectListener interface. - * It creates instances of these classes and adds them to the listeners list. - */ - private void discoverListeners() { - try { - String packageName = this.getClass().getPackage().getName(); - List> classes = getClassesInPackage(packageName); - - for (Class clazz : classes) { - if (ConnectListener.class.isAssignableFrom(clazz) && !clazz.isInterface()) { - try { - Object instance = clazz.getDeclaredConstructor().newInstance(); - listeners.add((ConnectListener) instance); - } catch (Exception e) { - } - } - } - } catch (Exception e) { - connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.ANNOTATION_PACKAGE, "discoverlistener.error")); - } - } - - /** - * getClassesInPackage is a helper method that retrieves all classes in a specified package. - * It uses the class loader to find resources in the package and loads them as Class objects. - * - * @param packageName The name of the package to scan for classes. - * @return A list of Class objects found in the specified package. - */ - private List> getClassesInPackage(String packageName) { - List> classes = new ArrayList<>(); - try { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - String path = packageName.replace('.', '/'); - URL resource = classLoader.getResource(path); - - if (resource != null) { - File directory = new File(resource.getFile()); - if (directory.exists()) { - for (File file : directory.listFiles()) { - if (file.getName().endsWith(".class")) { - String className = packageName + "." + file.getName().substring(0, file.getName().length() - 6); - try { - classes.add(Class.forName(className)); - } catch (ClassNotFoundException e) { - } - } - } - } - } - } catch (Exception e) { - connectLib.Logger().WARN(connectLib.LangManager().getMessage(CategoriesType.ANNOTATION_PACKAGE, "getclassesinpackage.error", Map.of("package", packageName, "exception", e.getMessage()))); - } - return classes; - } - -/** - * execListener is a method that executes all methods annotated with @AnnotConnect in the discovered listeners. - * It first checks if the listeners list is empty and calls discoverListeners if it is. - * Then, it iterates through each listener and invokes the annotated methods. - */ - public void execListener(){ - if (listeners.isEmpty()) { - discoverListeners(); - } - - for(ConnectListener listener : listeners){ - for(Method method : listener.getClass().getDeclaredMethods()){ - if(method.isAnnotationPresent(AnnotConnect.class)) { - try { - method.setAccessible(true); - method.invoke(listener); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - } - } - } - - /** - * Main method to execute the AnnotHandler and trigger the execution of annotated methods. - * This method serves as an entry point for testing the AnnotHandler functionality. - * - * @param args Command line arguments (not used). - */ - public static void main(String[] args) { - AnnotHandler annotLogic = new AnnotHandler(); - annotLogic.execListener(); - } -} \ No newline at end of file diff --git a/src/main/java/fr/sandro642/github/misc/Logger.java b/src/main/java/fr/sandro642/github/misc/Logger.java index 79494dd..51f7768 100644 --- a/src/main/java/fr/sandro642/github/misc/Logger.java +++ b/src/main/java/fr/sandro642/github/misc/Logger.java @@ -5,63 +5,89 @@ * It provides methods to log messages with different severity levels: * INFO, WARN, ERROR, and CRITICAL. * + * Use setShowLogs(true/false) to control console output. + * Logs are always saved to file regardless of the setting. + * * @author Sandro642 * @version 1.0 */ public class Logger { - private Logs logs = new Logs(); + private Logs logs = new Logs(); + private static boolean showLogs = false; // Default: logs are hidden + + /** + * Enable console output for logs. + * Call this method once to display all logs in console. + * By default, logs are only saved to file without console output. + * Logs are always saved to file regardless of this setting. + */ + public void showLogs() { + showLogs = true; + } - /** - * Method to display an informational message in the console. - * This method prints the message in green color and logs it. - * @param msg - */ - public void INFO(String msg) { - // Green - String INFO = "\u001B[32m[INFO] \u001B[0m"; - System.out.println(INFO + msg); + /** + * Method to display an informational message in the console. + * This method prints the message in green color if showLogs is enabled, + * and always logs it to file. + * @param msg The message to log + */ + public void INFO(String msg) { + if (showLogs) { + // Green + String INFO = "\u001B[32m[INFO] \u001B[0m"; + System.out.println(INFO + msg); + } - logs.MakeALog(msg, "INFO"); - } + logs.MakeALog(msg, "INFO"); + } - /** - * Method to display a warning message in the console. - * This method prints the message in yellow color and logs it. - * @param msg - */ - public void WARN(String msg) { - // Yellow - String WARN = "\u001B[33m[WARN] \u001B[0m"; - System.out.println(WARN + msg); + /** + * Method to display a warning message in the console. + * This method prints the message in yellow color if showLogs is enabled, + * and always logs it to file. + * @param msg The message to log + */ + public void WARN(String msg) { + if (showLogs) { + // Yellow + String WARN = "\u001B[33m[WARN] \u001B[0m"; + System.out.println(WARN + msg); + } - logs.MakeALog(msg, "WARN"); - } + logs.MakeALog(msg, "WARN"); + } - /** - * Method to display an error message in the console. - * This method prints the message in red color and logs it. - * @param msg - */ - public void ERROR(String msg) { - // Red - String ERROR = "\u001B[31m[ERROR] \u001B[0m"; - System.out.println(ERROR + msg); + /** + * Method to display an error message in the console. + * This method prints the message in red color if showLogs is enabled, + * and always logs it to file. + * @param msg The message to log + */ + public void ERROR(String msg) { + if (showLogs) { + // Red + String ERROR = "\u001B[31m[ERROR] \u001B[0m"; + System.out.println(ERROR + msg); + } - logs.MakeALog(msg, "ERROR"); - } + logs.MakeALog(msg, "ERROR"); + } - /** - * Method to display a critical message in the console. - * This method prints the message in magenta color and logs it. - * @param msg - */ - public void CRITICAL(String msg) { - // Magenta - String CRITICAL = "\u001B[35m[CRITICAL] \u001B[0m"; - System.out.println(CRITICAL + msg); + /** + * Method to display a critical message in the console. + * This method prints the message in magenta color if showLogs is enabled, + * and always logs it to file. + * @param msg The message to log + */ + public void CRITICAL(String msg) { + if (showLogs) { + // Magenta + String CRITICAL = "\u001B[35m[CRITICAL] \u001B[0m"; + System.out.println(CRITICAL + msg); + } - logs.MakeALog(msg, "CRITICAL"); - } -} + logs.MakeALog(msg, "CRITICAL"); + } +} \ No newline at end of file diff --git a/src/test/java/fr/sandro642/github/test/MainTest.java b/src/test/java/fr/sandro642/github/test/MainTest.java index b256c3a..09e5c2b 100644 --- a/src/test/java/fr/sandro642/github/test/MainTest.java +++ b/src/test/java/fr/sandro642/github/test/MainTest.java @@ -8,6 +8,7 @@ import fr.sandro642.github.enums.ResourceType; import fr.sandro642.github.enums.VersionType; import fr.sandro642.github.misc.EnumLoader; +import fr.sandro642.github.misc.Logger; import org.junit.jupiter.api.Test; import java.util.Map; @@ -49,12 +50,12 @@ public void initializeCAPI() { public static void main(String[] args) { - connectLib.Init(ResourceType.TEST_RESOURCES, LangType.ENGLISH, TestRoutes.class); - + connectLib.Init(ResourceType.TEST_RESOURCES, LangType.FRENCH, TestRoutes.class); try { + connectLib.Logger().showLogs(); CompletableFuture apiFactoryCompletableFuture = connectLib.JobGetInfos() - .getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.HELLO) + .getRoutes( MethodType.GET, TestRoutes.HELLO) .getResponse(); ApiFactory response = apiFactoryCompletableFuture.get(5, TimeUnit.SECONDS); @@ -67,6 +68,7 @@ public static void main(String[] args) { } } + @Test public void testUseFullRoute() { connectLib.Init(ResourceType.TEST_RESOURCES, LangType.FRENCH, TestRoutes.class);