Explorar o código

支持类注解组合.

nieqiurong hai 1 ano
pai
achega
b1b024d0ca

+ 4 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/AnnotationHandler.java

@@ -36,7 +36,7 @@ public interface AnnotationHandler {
      * @return 注解
      */
     default <T extends Annotation> T getAnnotation(Class<?> beanClass, Class<T> annotationClass) {
-        return beanClass.getAnnotation(annotationClass);
+        return AnnotationUtils.findFirstAnnotation(annotationClass, beanClass);
     }
 
     /**
@@ -48,7 +48,7 @@ public interface AnnotationHandler {
      * @return 是否包含该注解
      */
     default <T extends Annotation> boolean isAnnotationPresent(Class<?> beanClass, Class<T> annotationClass) {
-        return beanClass.isAnnotationPresent(annotationClass);
+        return AnnotationUtils.findFirstAnnotation(annotationClass, beanClass) != null;
     }
 
     /**
@@ -84,7 +84,7 @@ public interface AnnotationHandler {
      * @return 注解
      */
     default <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) {
-        return method.getAnnotation(annotationClass);
+        return AnnotationUtils.findFirstAnnotation(annotationClass, method);
     }
 
     /**
@@ -96,6 +96,6 @@ public interface AnnotationHandler {
      * @return 是否包含该注解
      */
     default <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) {
-        return method.isAnnotationPresent(annotationClass);
+        return AnnotationUtils.findFirstAnnotation(annotationClass, method) != null;
     }
 }

+ 34 - 13
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/AnnotationUtils.java

@@ -19,6 +19,9 @@ import lombok.experimental.UtilityClass;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
  * @author nieqiurong
@@ -27,24 +30,42 @@ import java.lang.reflect.Field;
 @UtilityClass
 public class AnnotationUtils {
 
-    private static final String javaPackage = Override.class.getPackage().getName();
-
     public <T extends Annotation> T findFirstAnnotation(Class<T> annotationClazz, Field field) {
-        return getAnnotation(annotationClazz, field.getDeclaredAnnotations());
+        return getAnnotation(annotationClazz, new HashSet<>(), field.getDeclaredAnnotations());
+    }
+
+    public <T extends Annotation> T findFirstAnnotation(Class<T> annotationClazz, Class<?> clz) {
+        Set<Class<? extends Annotation>> hashSet = new HashSet<>();
+        T annotation = getAnnotation(annotationClazz, hashSet, clz.getDeclaredAnnotations());
+        if (annotation != null) {
+            return annotation;
+        }
+        Class<?> currentClass = clz.getSuperclass();
+        while (currentClass != null) {
+            annotation = getAnnotation(annotationClazz, hashSet, currentClass.getDeclaredAnnotations());
+            if (annotation != null) {
+                return annotation;
+            }
+            currentClass = currentClass.getSuperclass();
+        }
+        return null;
+    }
+
+    public <T extends Annotation> T findFirstAnnotation(Class<T> annotationClazz, Method method) {
+        return getAnnotation(annotationClazz, new HashSet<>(), method.getDeclaredAnnotations());
     }
 
     @SuppressWarnings("unchecked")
-    private <T extends Annotation> T getAnnotation(Class<T> annotationClazz, Annotation... annotations) {
+    private <T extends Annotation> T getAnnotation(Class<T> annotationClazz, Set<Class<? extends Annotation>> annotationSet, Annotation... annotations) {
         for (Annotation annotation : annotations) {
-            if (annotation.annotationType().getPackage().getName().startsWith(javaPackage)) {
-                continue;
-            }
-            if (annotationClazz.isAssignableFrom(annotation.annotationType())) {
-                return (T) annotation;
-            }
-            annotation = getAnnotation(annotationClazz, annotation.annotationType().getDeclaredAnnotations());
-            if (annotation != null) {
-                return (T) annotation;
+            if (annotationSet.add(annotation.annotationType())) {
+                if (annotationClazz.isAssignableFrom(annotation.annotationType())) {
+                    return (T) annotation;
+                }
+                annotation = getAnnotation(annotationClazz, annotationSet, annotation.annotationType().getDeclaredAnnotations());
+                if (annotation != null) {
+                    return (T) annotation;
+                }
             }
         }
         return null;

+ 28 - 2
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/metadata/TableInfoHelperTest.java

@@ -93,8 +93,25 @@ class TableInfoHelperTest {
 
     }
 
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
+    @TableName(schema = "test")
+    @interface MyTable {
+
+    }
+
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
+    @TableLogic(value = "false", delval = "true")
+    @interface MyTableLogic {
+
+    }
+
 
     @Data
+    @MyTable
     private static class CustomAnnotation {
 
         @MyId
@@ -104,15 +121,24 @@ class TableInfoHelperTest {
 
         @NotExistsField
         private String test;
+
+        @MyTableLogic
+        private Boolean del;
     }
 
     @Test
     void testCustomAnnotation() {
         MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
-        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(mybatisConfiguration, ""), CustomAnnotation.class);
+        TableInfo tableInfo = TableInfoHelper.initTableInfo(new MapperBuilderAssistant(mybatisConfiguration, ""), CustomAnnotation.class);
         List<Field> fieldList = TableInfoHelper.getAllFields(CustomAnnotation.class);
-        Assertions.assertThat(fieldList.size()).isEqualTo(2);
+        Assertions.assertThat(fieldList.size()).isEqualTo(3);
         Assertions.assertThat(TableInfoHelper.isExistTableId(CustomAnnotation.class, Arrays.asList(CustomAnnotation.class.getDeclaredFields()))).isTrue();
+        Assertions.assertThat(TableInfoHelper.isExistTableLogic(CustomAnnotation.class, Arrays.asList(CustomAnnotation.class.getDeclaredFields()))).isTrue();
+        TableFieldInfo logicDeleteFieldInfo = TableInfoHelper.getTableInfo(CustomAnnotation.class).getLogicDeleteFieldInfo();
+        Assertions.assertThat(logicDeleteFieldInfo).isNotNull();
+        Assertions.assertThat(logicDeleteFieldInfo.getLogicDeleteValue()).isNotNull().isEqualTo("true");
+        Assertions.assertThat(logicDeleteFieldInfo.getLogicNotDeleteValue()).isNotNull().isEqualTo("false");
+        Assertions.assertThat(tableInfo.getTableName()).isEqualTo("test.custom_annotation");
     }
 
 

+ 18 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/toolkit/AnnotationUtilsTest.java

@@ -3,6 +3,7 @@ package com.baomidou.mybatisplus.test.toolkit;
 import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
 import com.baomidou.mybatisplus.core.toolkit.AnnotationUtils;
 import lombok.Data;
 import org.junit.jupiter.api.Assertions;
@@ -43,7 +44,17 @@ public class AnnotationUtilsTest {
 
     }
 
+    @Documented
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
+    @TableName(schema = "test")
+    @interface MyTable {
+
+    }
+
+
     @Data
+    @MyTable
     public static class Demo {
 
         private String id;
@@ -131,4 +142,11 @@ public class AnnotationUtilsTest {
         Assertions.assertNotNull(tableLogic);
     }
 
+    @Test
+    void test8() {
+        TableName tableName = AnnotationUtils.findFirstAnnotation(TableName.class, Demo.class);
+        Assertions.assertNotNull(tableName);
+        Assertions.assertEquals(tableName.schema(),"test");
+    }
+
 }