From 145f337aa627ae6e6043593c1e6a2bf22dce4d54 Mon Sep 17 00:00:00 2001 From: QwQ-dev Date: Fri, 20 Dec 2024 17:14:57 +0800 Subject: [PATCH] chore: documentation and some naming changes. --- annotation/README.md | 2 +- .../service/AnnotationProcessingService.java | 134 +++++++++++++----- .../AnnotationProcessingServiceImpl.java | 123 ---------------- .../AnnotationProcessingServiceInterface.java | 57 ++++++++ cache/README.md | 2 +- ...erface.java => CacheServiceInterface.java} | 2 +- .../library/cache/service/L1CacheService.java | 2 +- .../configuration/ConfigurationLauncher.java | 6 +- 8 files changed, 164 insertions(+), 164 deletions(-) delete mode 100644 annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceImpl.java create mode 100644 annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceInterface.java rename cache/src/main/java/me/qwqdev/library/cache/service/{CacheInterface.java => CacheServiceInterface.java} (92%) diff --git a/annotation/README.md b/annotation/README.md index 5313fbf..ec832b8 100644 --- a/annotation/README.md +++ b/annotation/README.md @@ -57,7 +57,7 @@ This is very simple, right? But now that the definition is complete, how does th @InjectableComponent public class AnnotationLauncher extends Plugin { @Autowired - private AnnotationProcessingService annotationProcessingService; + private AnnotationProcessingServiceInterface annotationProcessingService; @Override public void onPluginEnable() { diff --git a/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingService.java b/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingService.java index 1fbe278..1383c0c 100644 --- a/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingService.java +++ b/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingService.java @@ -1,57 +1,123 @@ package me.qwqdev.library.annotation.service; +import io.fairyproject.container.Containers; +import io.fairyproject.container.InjectableComponent; +import io.fairyproject.log.Log; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import me.qwqdev.library.annotation.utils.AnnotationScanner; +import org.reflections.util.ClasspathHelper; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.util.Collection; +import java.util.Set; /** - * Service interface for processing annotations using custom annotation processors. + * Service implementation for processing annotations using custom annotation processors. * - *

This interface defines methods for processing annotations within classes in the specified base package or - * from a collection of URLs. It allows for processing using default or custom annotation processors. + *

This class provides the core functionality for scanning and processing annotations + * within a specified package or set of URLs. It implements the {@link AnnotationProcessingServiceInterface} + * interface and uses reflection to dynamically instantiate and execute custom annotation processors. * * @author qwq-dev * @version 1.1 + * @see AnnotationProcessingServiceInterface + * @see CustomAnnotationProcessor + * @see AnnotationProcessor * @since 2024-12-19 17:00 */ -public interface AnnotationProcessingService { +@InjectableComponent +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class AnnotationProcessingService implements AnnotationProcessingServiceInterface { /** - * Processes annotations within the specified base package using the default annotation processors. - * - *

This method scans the specified base package and its sub-packages for classes annotated with specific - * annotations, and then processes them using default annotation processors. + * {@inheritDoc} * - * @param basePackage the base package to scan for annotated classes - * @param fromFairyIoCSingleton whether handlerClass should be injected into a singleton by the Fairy framework - * If false, it will be created by reflection without parameters, - * but it still supports setter injection with Fairy {@link io.fairyproject.container.Autowired} annotation - * @param classLoader optional class loaders to use for classpath scanning; if not provided, the default class loader is used + * @param basePackage {@inheritDoc} + * @param classLoader {@inheritDoc} + * @param fromFairyIoCSingleton {@inheritDoc} */ - void processAnnotations(String basePackage, boolean fromFairyIoCSingleton, ClassLoader... classLoader); + @Override + public void processAnnotations(String basePackage, boolean fromFairyIoCSingleton, ClassLoader... classLoader) { + processAnnotations(ClasspathHelper.forPackage(basePackage, classLoader), fromFairyIoCSingleton); + } /** - * Processes annotations within the specified collection of URLs using default annotation processors. - * - *

This method scans the classes located at the provided URLs for annotations and processes them using - * default annotation processors. The URLs can represent locations like JAR files, directories, or classpath entries. + * {@inheritDoc} * - * @param urls the collection of URLs to scan for annotated classes - * @param fromFairyIoCSingleton whether handlerClass should be injected into a singleton by the Fairy framework - * If false, it will be created by reflection without parameters, - * but it still supports setter injection with Fairy {@link io.fairyproject.container.Autowired} annotation + * @param urls {@inheritDoc} + * @param fromFairyIoCSingleton {@inheritDoc} */ - void processAnnotations(Collection urls, boolean fromFairyIoCSingleton); + @Override + public void processAnnotations(Collection urls, boolean fromFairyIoCSingleton) { + Set> annotatedClasses = AnnotationScanner.findAnnotatedClasses(urls, AnnotationProcessor.class); + + annotatedClasses.stream() + .filter(CustomAnnotationProcessor.class::isAssignableFrom) + .forEach(handlerClass -> processAnnotations(urls, handlerClass.asSubclass(CustomAnnotationProcessor.class), fromFairyIoCSingleton)); + } /** - * Processes annotations within the specified collection of URLs using a specific custom annotation processor. - * - *

This method allows for processing annotations in the classes located at the specified URLs using a custom - * annotation processor, which allows for more fine-grained control over the processing logic. + * {@inheritDoc} * - * @param urls the collection of URLs to scan for annotated classes - * @param handlerClass the class of the custom annotation processor to handle annotation processing - * @param fromFairyIoCSingleton whether handlerClass should be injected into a singleton by the Fairy framework - * If false, it will be created by reflection without parameters, - * but it still supports setter injection with Fairy {@link io.fairyproject.container.Autowired} annotation + * @param urls {@inheritDoc} + * @param handlerClass {@inheritDoc} + * @param fromFairyIoCSingleton {@inheritDoc} */ - void processAnnotations(Collection urls, Class handlerClass, boolean fromFairyIoCSingleton); -} + @Override + public void processAnnotations(Collection urls, Class handlerClass, boolean fromFairyIoCSingleton) { + CustomAnnotationProcessor customAnnotationProcessor; + + if (fromFairyIoCSingleton) { + try { + customAnnotationProcessor = Containers.get(handlerClass); + } catch (Exception exception) { + Log.error("An exception occurred when Fairy injected the singleton pattern", exception); + return; + } + } else { + try { + customAnnotationProcessor = handlerClass.getDeclaredConstructor().newInstance(); + } catch (InvocationTargetException exception) { + Log.error("An invocation target exception occurred while processing annotations", exception); + return; + } catch (InstantiationException exception) { + Log.error("An instance of the annotation processor could not be created", exception); + return; + } catch (IllegalAccessException exception) { + Log.error("An illegal access exception occurred while processing annotations", exception); + return; + } catch (NoSuchMethodException exception) { + Log.error("The default constructor of the annotation processor could not be found", exception); + return; + } + } + + AnnotationProcessor annotationProcessingService = handlerClass.getAnnotation(AnnotationProcessor.class); + + if (annotationProcessingService == null) { + Log.error("AnnotationProcessor annotation not found on handlerClass: " + handlerClass.getName()); + return; + } + + Class annotationClazz = annotationProcessingService.value(); + + if (annotationClazz == null) { + Log.error("Annotation class is null"); + return; + } + + for (Class aClass : AnnotationScanner.findAnnotatedClasses(urls, annotationClazz)) { + try { + customAnnotationProcessor.after(annotationClazz); + customAnnotationProcessor.process(aClass); + customAnnotationProcessor.before(annotationClazz); + } catch (Exception exception) { + customAnnotationProcessor.exception(aClass, exception); + } finally { + customAnnotationProcessor.finallyAfter(annotationClazz); + } + } + } +} \ No newline at end of file diff --git a/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceImpl.java b/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceImpl.java deleted file mode 100644 index df83e6b..0000000 --- a/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceImpl.java +++ /dev/null @@ -1,123 +0,0 @@ -package me.qwqdev.library.annotation.service; - -import io.fairyproject.container.Containers; -import io.fairyproject.container.InjectableComponent; -import io.fairyproject.log.Log; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import me.qwqdev.library.annotation.utils.AnnotationScanner; -import org.reflections.util.ClasspathHelper; - -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; -import java.net.URL; -import java.util.Collection; -import java.util.Set; - -/** - * Service implementation for processing annotations using custom annotation processors. - * - *

This class provides the core functionality for scanning and processing annotations - * within a specified package or set of URLs. It implements the {@link AnnotationProcessingService} - * interface and uses reflection to dynamically instantiate and execute custom annotation processors. - * - * @author qwq-dev - * @version 1.1 - * @see AnnotationProcessingService - * @see CustomAnnotationProcessor - * @see AnnotationProcessor - * @since 2024-12-19 17:00 - */ -@InjectableComponent -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class AnnotationProcessingServiceImpl implements AnnotationProcessingService { - /** - * {@inheritDoc} - * - * @param basePackage {@inheritDoc} - * @param classLoader {@inheritDoc} - * @param fromFairyIoCSingleton {@inheritDoc} - */ - @Override - public void processAnnotations(String basePackage, boolean fromFairyIoCSingleton, ClassLoader... classLoader) { - processAnnotations(ClasspathHelper.forPackage(basePackage, classLoader), fromFairyIoCSingleton); - } - - /** - * {@inheritDoc} - * - * @param urls {@inheritDoc} - * @param fromFairyIoCSingleton {@inheritDoc} - */ - @Override - public void processAnnotations(Collection urls, boolean fromFairyIoCSingleton) { - Set> annotatedClasses = AnnotationScanner.findAnnotatedClasses(urls, AnnotationProcessor.class); - - annotatedClasses.stream() - .filter(CustomAnnotationProcessor.class::isAssignableFrom) - .forEach(handlerClass -> processAnnotations(urls, handlerClass.asSubclass(CustomAnnotationProcessor.class), fromFairyIoCSingleton)); - } - - /** - * {@inheritDoc} - * - * @param urls {@inheritDoc} - * @param handlerClass {@inheritDoc} - * @param fromFairyIoCSingleton {@inheritDoc} - */ - @Override - public void processAnnotations(Collection urls, Class handlerClass, boolean fromFairyIoCSingleton) { - CustomAnnotationProcessor customAnnotationProcessor; - - if (fromFairyIoCSingleton) { - try { - customAnnotationProcessor = Containers.get(handlerClass); - } catch (Exception exception) { - Log.error("An exception occurred when Fairy injected the singleton pattern", exception); - return; - } - } else { - try { - customAnnotationProcessor = handlerClass.getDeclaredConstructor().newInstance(); - } catch (InvocationTargetException exception) { - Log.error("An invocation target exception occurred while processing annotations", exception); - return; - } catch (InstantiationException exception) { - Log.error("An instance of the annotation processor could not be created", exception); - return; - } catch (IllegalAccessException exception) { - Log.error("An illegal access exception occurred while processing annotations", exception); - return; - } catch (NoSuchMethodException exception) { - Log.error("The default constructor of the annotation processor could not be found", exception); - return; - } - } - - AnnotationProcessor annotationProcessingService = handlerClass.getAnnotation(AnnotationProcessor.class); - - if (annotationProcessingService == null) { - Log.error("AnnotationProcessor annotation not found on handlerClass: " + handlerClass.getName()); - return; - } - - Class annotationClazz = annotationProcessingService.value(); - - if (annotationClazz == null) { - Log.error("Annotation class is null"); - return; - } - - for (Class aClass : AnnotationScanner.findAnnotatedClasses(urls, annotationClazz)) { - try { - customAnnotationProcessor.after(annotationClazz); - customAnnotationProcessor.process(aClass); - customAnnotationProcessor.before(annotationClazz); - } catch (Exception exception) { - customAnnotationProcessor.exception(aClass, exception); - } finally { - customAnnotationProcessor.finallyAfter(annotationClazz); - } - } - } -} \ No newline at end of file diff --git a/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceInterface.java b/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceInterface.java new file mode 100644 index 0000000..fcb5b56 --- /dev/null +++ b/annotation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceInterface.java @@ -0,0 +1,57 @@ +package me.qwqdev.library.annotation.service; + +import java.net.URL; +import java.util.Collection; + +/** + * Service interface for processing annotations using custom annotation processors. + * + *

This interface defines methods for processing annotations within classes in the specified base package or + * from a collection of URLs. It allows for processing using default or custom annotation processors. + * + * @author qwq-dev + * @version 1.1 + * @since 2024-12-19 17:00 + */ +public interface AnnotationProcessingServiceInterface { + /** + * Processes annotations within the specified base package using the default annotation processors. + * + *

This method scans the specified base package and its sub-packages for classes annotated with specific + * annotations, and then processes them using default annotation processors. + * + * @param basePackage the base package to scan for annotated classes + * @param fromFairyIoCSingleton whether handlerClass should be injected into a singleton by the Fairy framework + * If false, it will be created by reflection without parameters, + * but it still supports setter injection with Fairy {@link io.fairyproject.container.Autowired} annotation + * @param classLoader optional class loaders to use for classpath scanning; if not provided, the default class loader is used + */ + void processAnnotations(String basePackage, boolean fromFairyIoCSingleton, ClassLoader... classLoader); + + /** + * Processes annotations within the specified collection of URLs using default annotation processors. + * + *

This method scans the classes located at the provided URLs for annotations and processes them using + * default annotation processors. The URLs can represent locations like JAR files, directories, or classpath entries. + * + * @param urls the collection of URLs to scan for annotated classes + * @param fromFairyIoCSingleton whether handlerClass should be injected into a singleton by the Fairy framework + * If false, it will be created by reflection without parameters, + * but it still supports setter injection with Fairy {@link io.fairyproject.container.Autowired} annotation + */ + void processAnnotations(Collection urls, boolean fromFairyIoCSingleton); + + /** + * Processes annotations within the specified collection of URLs using a specific custom annotation processor. + * + *

This method allows for processing annotations in the classes located at the specified URLs using a custom + * annotation processor, which allows for more fine-grained control over the processing logic. + * + * @param urls the collection of URLs to scan for annotated classes + * @param handlerClass the class of the custom annotation processor to handle annotation processing + * @param fromFairyIoCSingleton whether handlerClass should be injected into a singleton by the Fairy framework + * If false, it will be created by reflection without parameters, + * but it still supports setter injection with Fairy {@link io.fairyproject.container.Autowired} annotation + */ + void processAnnotations(Collection urls, Class handlerClass, boolean fromFairyIoCSingleton); +} diff --git a/cache/README.md b/cache/README.md index 76aeeab..d822a39 100644 --- a/cache/README.md +++ b/cache/README.md @@ -98,6 +98,6 @@ such as `MemoryQueryCacheInterface`. When the L2 cache is developed, it only nee ### scalability -For more levels of cache, we only need to implement `QueryCacheInterface` to implement something similar to `MemoryQueryCache`. Then create a new Service class and implement `QueryCacheInterface`. +For more levels of cache, we only need to implement `QueryCacheInterface` to implement something similar to `MemoryQueryCache`. Then create a new Service class and implement `CacheServiceInterface`. If it is java memory based, we recommend using `MemoryQueryCacheInterface` (L1 cache already exists, so this is actually not necessary). \ No newline at end of file diff --git a/cache/src/main/java/me/qwqdev/library/cache/service/CacheInterface.java b/cache/src/main/java/me/qwqdev/library/cache/service/CacheServiceInterface.java similarity index 92% rename from cache/src/main/java/me/qwqdev/library/cache/service/CacheInterface.java rename to cache/src/main/java/me/qwqdev/library/cache/service/CacheServiceInterface.java index 41cfcb6..3a57b96 100644 --- a/cache/src/main/java/me/qwqdev/library/cache/service/CacheInterface.java +++ b/cache/src/main/java/me/qwqdev/library/cache/service/CacheServiceInterface.java @@ -13,7 +13,7 @@ * @see QueryCacheInterface * @since 2024-12-20 */ -public interface CacheInterface extends QueryCacheInterface { +public interface CacheServiceInterface extends QueryCacheInterface { /** * Retrieves the value associated with the specified key from the cache. * diff --git a/cache/src/main/java/me/qwqdev/library/cache/service/L1CacheService.java b/cache/src/main/java/me/qwqdev/library/cache/service/L1CacheService.java index eb360c2..8781bf8 100644 --- a/cache/src/main/java/me/qwqdev/library/cache/service/L1CacheService.java +++ b/cache/src/main/java/me/qwqdev/library/cache/service/L1CacheService.java @@ -21,7 +21,7 @@ */ @Data @RequiredArgsConstructor -public class L1CacheService implements CacheInterface { +public class L1CacheService implements CacheServiceInterface { private final QueryCacheInterface queryCacheInterface; /** diff --git a/configuration/src/main/java/me/qwqdev/library/configuration/ConfigurationLauncher.java b/configuration/src/main/java/me/qwqdev/library/configuration/ConfigurationLauncher.java index 07361f7..ebd6da7 100644 --- a/configuration/src/main/java/me/qwqdev/library/configuration/ConfigurationLauncher.java +++ b/configuration/src/main/java/me/qwqdev/library/configuration/ConfigurationLauncher.java @@ -4,7 +4,7 @@ import io.fairyproject.container.Autowired; import io.fairyproject.container.InjectableComponent; import io.fairyproject.plugin.Plugin; -import me.qwqdev.library.annotation.service.AnnotationProcessingService; +import me.qwqdev.library.annotation.service.AnnotationProcessingServiceInterface; /** * The type Configuration launcher. @@ -16,12 +16,12 @@ @InjectableComponent public class ConfigurationLauncher extends Plugin { @Autowired - private AnnotationProcessingService annotationProcessingService; + private AnnotationProcessingServiceInterface annotationProcessingServiceInterface; @Override public void onPluginEnable() { // We need to process serializable annotations String basePackage = this.getClass().getPackageName(); - annotationProcessingService.processAnnotations(basePackage, false, this.getClassLoader()); + annotationProcessingServiceInterface.processAnnotations(basePackage, false, this.getClassLoader()); } }