可以套个模板方法
abstract class CheckableIReportHandler implements IReportHandler {
void handleWithCheck(AacmReportInsertBo aacmReportInsertBo) throws IllegalAccessException {
check(aacmReportInsertBo);
handle(aacmReportInsertBo);
}
abstract void check(AacmReportInsertBo aacmReportInsertBo) throws IllegalAccessException;
}
class QueryHandler extends CheckableIReportHandler {
@
Override public void handle(AacmReportInsertBo aacmReportInsertBo) {
System.out.println("handle " + aacmReportInsertBo.toString());
}
@
Override void check(AacmReportInsertBo aacmReportInsertBo) throws IllegalAccessException {
Field[] declaredFields = aacmReportInsertBo.getClass().getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
NotNull annotation = declaredField.getAnnotation(NotNull.class);
if (annotation != null) {
Class<?>[] groups = annotation.groups();
boolean flag = false;
for (Class<?> group : groups) {
if (group == this.getClass()) {
flag = true;
}
}
if(flag) {
if (declaredField.get(aacmReportInsertBo) == null) {
throw new IllegalArgumentException(declaredField.getName() + " is null");
}
}
}
}
}
}
public void save(AacmReportInsertBo aacmReportInsertBo) throws IllegalAccessException {
IReportHandler handler = ReportHandlerFactory.getReportHandlerService(ReportType.getType(aacmReportInsertBo.getType()));
// ReportHandlerErrorEnum.HANDLER_NULL_ERROR.isNull(handler);
if(handler instanceof CheckableIReportHandler) {
((CheckableIReportHandler)handler).handleWithCheck(aacmReportInsertBo);
}
}