Java通用枚举工具类

Java通用枚举工具类

记录一下工作中使用的枚举工具类。

1. 枚举类示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public enum EnumType {

CHECK_BOX(1, "CHECK_BOX", "复选框"),
INPUT(2, "INPUT", "输入框"),
TEXT_AREA(3, "TEXT_AREA", "多行文本框"),
RADIO(4, "RADIO", "单选框");

private final Integer code;
private final String type;
private final String msg;

EnumType(Integer code, String type, String msg) {
this.code = code;
this.type = type;
this.msg = msg;
}

public static String getMsgByCode(Integer code) {
for (EnumType e : values()) {
if (e.code.equals(code)) {
return e.msg;
}
}
return "";
}

public Integer getCode() {
return code;
}

public String getType() {
return type;
}

public String getMsg() {
return this.msg;
}
}

2. 通用的枚举响应对象

通用对象的字段名称与枚举类中的字段属性名称对应。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("通用枚举响应参数")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EnumResponse implements java.io.Serializable {

@ApiModelProperty("名称")
private Object msg;

@ApiModelProperty("类型")
private Object type;

@ApiModelProperty("值")
private Object code;

}

3. 枚举工具类常用方法

3.1 根据枚举的类对象返回通用枚举响应对象里身列表

主要实现是通过反射获取EnumResponse对象中的字段名称,
再通过获取到的字段名称去枚举类中获取对应的值,
最后封装为List<EnumResponse>返回,详情见代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public static <T extends Enum<?>> List<EnumResponse> getTypeEnum(Class<T> enumClass) {
// 获取返回实体所有的字段方法名称
List<String> methodNames = new ArrayList<>();
Field[] fields = EnumResponse.class.getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
StringBuilder builder = new StringBuilder();
methodNames.add(builder.append(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString());
}
// 获取枚举类中和EnumResponse实体中字段名称一致的get方法
List<EnumResponse> list = new ArrayList<>();
Object[] objects = enumClass.getEnumConstants();
Map<String, Method> methods = new HashMap<>();
for (String methodName : methodNames) {
try {
Method method = enumClass.getMethod("get" + methodName);
methods.put(methodName, method);
} catch (NoSuchMethodException ignored) {}
}
// 调用枚举类中的get方法,把值set到EnumResponse实体类中
for (Object object : objects) {
EnumResponse response = new EnumResponse();
methods.forEach((k, v) -> {
try {
Method method = response.getClass().getMethod("set" + k, Object.class);
Object invoke = v.invoke(object);
if (invoke instanceof Enum) {
EnumResponse enumResponse = getEnumResponse((Enum<?>) invoke);
method.invoke(response, enumResponse);
} else {
method.invoke(response, invoke);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
LoggerUtils.error(e, e.getMessage());
}
});
list.add(response);
}
return list;
}

3.2 根据具体的枚举对象获取通用枚举响应对象

实现方式与上述基本一致,
如果枚举中有嵌套枚举,则会递归调用获取内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static EnumResponse getEnumResponse(Enum<?> anEnum) {
EnumResponse response = new EnumResponse();
// 获取返回实体所有的字段方法名称
List<String> methodNames = new ArrayList<>();
Field[] fields = EnumResponse.class.getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
StringBuilder builder = new StringBuilder();
methodNames.add(builder.append(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString());
}
// 获取枚举类中和EnumResponse实体中字段名称一致的get方法
Map<String, Method> methods = new HashMap<>();
for (String methodName : methodNames) {
try {
Method method = anEnum.getClass().getMethod("get" + methodName);
methods.put(methodName, method);
} catch (NoSuchMethodException ignored) {}
}
// 调用枚举类中的get方法,把值set到EnumResponse实体类中
methods.forEach((k, v) -> {
try {
Method method = response.getClass().getMethod("set" + k, Object.class);
Object invoke = v.invoke(anEnum);
if (invoke instanceof Enum) {
EnumResponse enumResponse = getEnumResponse((Enum<?>) invoke);
method.invoke(response, enumResponse);
} else {
method.invoke(response, invoke);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
LoggerUtils.error(e, e.getMessage());
}
});
return response;
}

以上。


Java通用枚举工具类
https://www.magese.com/2021/01/04/Java通用枚举工具类/
作者
Magese
发布于
2021年1月4日
许可协议