//调用
judgeDingModel(model, DingTalkInfo.class, talkInfo, heard, workOrderInfo);
judgeDingModel(model, MsgSendInfo.class, msgSendInfo, heard, workOrderInfo);
//公共方法
private static <U> void judgeDingModel(String model, Class<U> clazz, U cla, ConstantValue.HEARD heard, WorkOrderInfo workOrderInfo) throws IllegalAccessException {
U cast = clazz.cast(cla);
for (Field declaredField : cast.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);
if ("context".equals(declaredField.getName())) {
if (model.equals(ConstantValue.WORK_LOG)) {
//工作日志
declaredField.set(cast,heard.getContext() + "日志内容为:" + workOrderInfo.getChangeContext());
} else if (model.equals(ConstantValue.WORK_CHANGE_LOG)) {
declaredField.set(cast,heard.getContext() + " " + workOrderInfo.getCreatorName() + "将:" + workOrderInfo.getFieldTitle() + "修改为" + workOrderInfo.getFieldValue());
} else {
declaredField.set(cast,heard.getContext());
}
}
}
}
java 不同实体类 为参数的公共方法
最新推荐文章于 2024-11-03 11:01:20 发布
这段代码展示了如何使用Java反射机制动态设置对象字段,根据不同的工作日志类型填充上下文信息,如工作日志内容或工作变更记录。方法接收模型名称、目标类、实例、听力信息和工单信息作为参数,通过反射遍历对象字段,针对'context'字段进行内容赋值。
摘要由CSDN通过智能技术生成