Skip to content

Commit

Permalink
chore: documentation and some naming changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
QwQ-dev committed Dec 20, 2024
1 parent 121150b commit 145f337
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 164 deletions.
2 changes: 1 addition & 1 deletion annotation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
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);
}
}
}
}

This file was deleted.

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);
}
2 changes: 1 addition & 1 deletion cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @see QueryCacheInterface
* @since 2024-12-20
*/
public interface CacheInterface<K, V> extends QueryCacheInterface<K, V> {
public interface CacheServiceInterface<K, V> extends QueryCacheInterface<K, V> {
/**
* Retrieves the value associated with the specified key from the cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
@Data
@RequiredArgsConstructor
public class L1CacheService<K, V> implements CacheInterface<K, V> {
public class L1CacheService<K, V> implements CacheServiceInterface<K, V> {
private final QueryCacheInterface<K, V> queryCacheInterface;

/**
Expand Down
Loading

0 comments on commit 145f337

Please sign in to comment.