-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: documentation and some naming changes.
- Loading branch information
Showing
8 changed files
with
164 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 100 additions & 34 deletions
134
...ation/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* | ||
* <p>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. | ||
* <p>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. | ||
* | ||
* <p>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. | ||
* | ||
* <p>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<URL> urls, boolean fromFairyIoCSingleton); | ||
@Override | ||
public void processAnnotations(Collection<URL> urls, boolean fromFairyIoCSingleton) { | ||
Set<Class<?>> 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. | ||
* | ||
* <p>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<URL> urls, Class<? extends CustomAnnotationProcessor> handlerClass, boolean fromFairyIoCSingleton); | ||
} | ||
@Override | ||
public void processAnnotations(Collection<URL> urls, Class<? extends CustomAnnotationProcessor> 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<? extends Annotation> 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); | ||
} | ||
} | ||
} | ||
} |
123 changes: 0 additions & 123 deletions
123
...n/src/main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceImpl.java
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
.../main/java/me/qwqdev/library/annotation/service/AnnotationProcessingServiceInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* | ||
* <p>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. | ||
* | ||
* <p>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. | ||
* | ||
* <p>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<URL> urls, boolean fromFairyIoCSingleton); | ||
|
||
/** | ||
* Processes annotations within the specified collection of URLs using a specific custom annotation processor. | ||
* | ||
* <p>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<URL> urls, Class<? extends CustomAnnotationProcessor> handlerClass, boolean fromFairyIoCSingleton); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.