添加注解扫描器;

This commit is contained in:
huangchengxing
2022-06-14 15:26:51 +08:00
parent ef289dc676
commit 0cb498bf6e
10 changed files with 663 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
package cn.hutool.core.annotation.scanner;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author huangchengxing
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@interface AnnotationForScannerTest {
}

View File

@@ -0,0 +1,33 @@
package cn.hutool.core.annotation.scanner;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;
/**
* @author huangchengxing
*/
public class FieldAnnotationScannerTest {
@Test
public void testFieldAnnotationScanner() {
FieldAnnotationScanner scanner = new FieldAnnotationScanner();
Field field = ReflectUtil.getField(Example.class, "id");
Assert.assertNotNull(field);
Assert.assertTrue(scanner.support(field));
List<Annotation> annotations = scanner.getAnnotations(field);
Assert.assertEquals(1, annotations.size());
Assert.assertEquals(AnnotationForScannerTest.class, CollUtil.getFirst(annotations).annotationType());
}
public static class Example {
@AnnotationForScannerTest
private Integer id;
}
}

View File

@@ -0,0 +1,55 @@
package cn.hutool.core.annotation.scanner;
import cn.hutool.core.collection.CollUtil;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author huangchengxing
* @date 2022/06/10 16:51
*/
public class MateAnnotationScannerTest {
@Test
public void testMateAnnotationScanner() {
AnnotationScanner scanner = new MateAnnotationScanner();
Assert.assertTrue(scanner.support(AnnotationForScannerTest3.class));
Map<Class<? extends Annotation>, Annotation> annotations = CollUtil.toMap(scanner.getAnnotations(AnnotationForScannerTest3.class), new HashMap<>(), Annotation::annotationType);
Assert.assertEquals(3, annotations.size());
Assert.assertTrue(annotations.containsKey(AnnotationForScannerTest.class));
Assert.assertTrue(annotations.containsKey(AnnotationForScannerTest1.class));
Assert.assertTrue(annotations.containsKey(AnnotationForScannerTest2.class));
Assert.assertFalse(annotations.containsKey(AnnotationForScannerTest3.class));
scanner = new MateAnnotationScanner(false);
Assert.assertTrue(scanner.support(AnnotationForScannerTest3.class));
annotations = CollUtil.toMap(scanner.getAnnotations(AnnotationForScannerTest3.class), new HashMap<>(), Annotation::annotationType);
Assert.assertEquals(1, annotations.size());
Assert.assertTrue(annotations.containsKey(AnnotationForScannerTest2.class));
Assert.assertFalse(annotations.containsKey(AnnotationForScannerTest.class));
Assert.assertFalse(annotations.containsKey(AnnotationForScannerTest1.class));
Assert.assertFalse(annotations.containsKey(AnnotationForScannerTest3.class));
}
@AnnotationForScannerTest3
static class Example {}
@AnnotationForScannerTest
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@interface AnnotationForScannerTest1 {}
@AnnotationForScannerTest1
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@interface AnnotationForScannerTest2 {}
@AnnotationForScannerTest2
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@interface AnnotationForScannerTest3 {}
}

View File

@@ -0,0 +1,35 @@
package cn.hutool.core.annotation.scanner;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
/**
* @author huangchengxing
*/
public class MethodAnnotationScannerTest {
@Test
public void testMethodAnnotationScanner() {
AnnotationScanner scanner = new MethodAnnotationScanner();
Method method = ReflectUtil.getMethod(Example.class, "test");
Assert.assertNotNull(method);
Assert.assertTrue(scanner.support(method));
List<Annotation> annotations = scanner.getAnnotations(method);
Assert.assertEquals(1, annotations.size());
Assert.assertEquals(CollUtil.getFirst(annotations).annotationType(), AnnotationForScannerTest.class);
}
static class Example {
@AnnotationForScannerTest
public void test() {
}
}
}

View File

@@ -0,0 +1,62 @@
package cn.hutool.core.annotation.scanner;
import cn.hutool.core.util.ClassUtil;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.util.List;
/**
* @author huangchengxing
* @date 2022/06/10 16:51
*/
public class TypeAnnotationScannerTest {
@Test
public void testTypeAnnotationScanner() {
AnnotationScanner scanner = new TypeAnnotationScanner();
Assert.assertTrue(scanner.support(Example.class));
List<Annotation> annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(3, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 不查找父接口
scanner = new TypeAnnotationScanner().setIncludeInterfaces(false);
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(2, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 不查找父类
scanner = new TypeAnnotationScanner().setIncludeSupperClass(false);
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(1, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 不查找ExampleSupplerClass.class
scanner = new TypeAnnotationScanner().addExcludeTypes(ExampleSupplerClass.class);
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(1, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 只查找ExampleSupplerClass.class
scanner = new TypeAnnotationScanner().setFilter(t -> ClassUtil.isAssignable(ExampleSupplerClass.class, t));
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(2, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
}
@AnnotationForScannerTest
static class ExampleSupplerClass implements ExampleInterface {}
@AnnotationForScannerTest
interface ExampleInterface {}
@AnnotationForScannerTest
static class Example extends ExampleSupplerClass {}
}