'How to do special processing on the class with a certain annotation(e.g @TestAnno) when springboot start to scan the class

for example, I have a static util class EnumUtil, like this

public class EnumUtil {
    private static List<?> enumClassList = new ArrayList(4);
    
    public static <E extends Enum<E>> void addEnumClass(Class<E> clazz) {
        enumClassList.add(clazz);
    }
}

then I'd like to develop an Annotation @EnumClassScaned like this

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnumClassScaned{
}

Its role is to call the addEnumClass method and pass the Class<?> of the class marked with the @EnumClassScaned annotation as a parameter when springboot start to run and scan class

e.g there is a enum class

@EnumClassScaned
public enum TestEnum {
    final int code;
    final String desc;
    
    TestEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }
}

when springboot start to run, and begin scan class, the code below will be loaded into SpringBoot (Since I don't know exactly how SpringBoot handles it, I'll just explain the way I imagined here)

    /**
     * This method does not really exist,
     * It just reflects my processing of scanned classes marked with this annotation
     *
     * @param cls cls represents the classes scanned by SpringBoot, 
     * I need to determine whether these classes are marked with @EnumClassScan annotation
     * and check whether it is a subclass of Enum
     */
    public void method(Class<?> cls) {
        Annotation annotation = cls.getAnnotation(EnumClassScaned.class);
        if (annotation != null) {
            EnumUtil.addEnumClass(cls);
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source