定义枚举接口
public interface BaseEnum {
/**
* 获取枚举值
* @returnint
*/
Integer getValue();
/**
* 获取枚举名称
* @return
*/
String getName();
}
枚举类
@Getter
public enum TestEnum implements BaseEnum {
TEST1("name1",1),
TEST2("name1",2),
TEST3("name1",3);
private String name;
private Integer value;
private TestEnum(String name,Integer value){
this.name = name;
this.value = value;
}
}
获取所有枚举
public static void test4(){
Reflections reflections = new Reflections("com.xxx.modules");
Set<Class<? extends BaseEnum>> monitorClasses = reflections.getSubTypesOf(BaseEnum.class);
System.out.println("size:"+monitorClasses.size());
monitorClasses.forEach(m->{
try {
System.out.println("name:"+m.getSimpleName());
BaseEnum[] enumConstants = m.getEnumConstants();
for (BaseEnum anEnum : enumConstants) {
System.out.println("enumName:"+anEnum+" value:"+anEnum.getValue()+" name:"+anEnum.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
输出