我要你扮演一个软件工程师。现在我需要你仔细思考,以下代码存在一些问题,headClazz里面获取到的是空值,我想要headClazz能动态获取到entity包中的user、admin表的字段名,我需要你指出代码错误,以及改正方法。代码如下:public class CommonReadListener<T> extends AnalysisEventListener<T> implements ReadListener<T> {private Class<T> headClazz;public CommonReadListener(Class<T> headClazz) { this.headClazz = headClazz; }@Override public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) { ExcelPropertyCheck clazzHeadAnno = this.headClazz.getAnnotation(ExcelPropertyCheck.class); Field[] declaredFields = headClazz.getDeclaredFields(); if (clazzHeadAnno != null && clazzHeadAnno.required()) { for (Field declaredField : declaredFields) { nullAbleFieldMap.put(declaredField.getName(), true); } } for (Field declaredField : declaredFields) { ExcelPropertyCheck annotation = declaredField.getAnnotation(ExcelPropertyCheck.class); if (annotation != null) { if (annotation.checkFormat()) { checkFormatFieldMap.put(declaredField.getName(), annotation.type() + ""); } if (annotation.required()) { nullAbleFieldMap.put(declaredField.getName(), true); } else { nullAbleFieldMap.remove(declaredField.getName()); } if (annotation.required() && annotation.length() != -1) { checkLengthFieldMap.put(declaredField.getName(), annotation.length()); } } } }
时间: 2023-07-15 11:15:34 浏览: 111
Diboot轻代码开发平台是一个面向开发人员的低代码开发平台,.rar
在这段代码中,headClazz获取到的是空值,可能是因为你没有将entity包中的user、admin表作为参数传入到CommonReadListener构造函数中。
修改代码,需要将entity包中的user、admin表作为参数传入到CommonReadListener构造函数中,然后在invokeHead方法中获取它们的字段名。代码修改如下:
```
public class CommonReadListener<T> extends AnalysisEventListener<T> implements ReadListener<T> {
private Class<T> headClazz;
private Class<?>[] entityClasses;
public CommonReadListener(Class<T> headClazz, Class<?>... entityClasses) {
this.headClazz = headClazz;
this.entityClasses = entityClasses;
}
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
ExcelPropertyCheck clazzHeadAnno = this.headClazz.getAnnotation(ExcelPropertyCheck.class);
Map<String, String[]> fieldNamesMap = new HashMap<>();
for (Class<?> entityClass : entityClasses) {
String className = entityClass.getSimpleName();
Field[] declaredFields = entityClass.getDeclaredFields();
String[] fieldNames = new String[declaredFields.length];
for (int i = 0; i < declaredFields.length; i++) {
fieldNames[i] = declaredFields[i].getName();
}
fieldNamesMap.put(className.toLowerCase(), fieldNames);
}
if (clazzHeadAnno != null && clazzHeadAnno.required()) {
for (Field declaredField : headClazz.getDeclaredFields()) {
nullAbleFieldMap.put(declaredField.getName(), true);
}
}
for (Field declaredField : headClazz.getDeclaredFields()) {
ExcelPropertyCheck annotation = declaredField.getAnnotation(ExcelPropertyCheck.class);
if (annotation != null) {
if (annotation.checkFormat()) {
checkFormatFieldMap.put(declaredField.getName(), annotation.type() + "");
}
if (annotation.required()) {
nullAbleFieldMap.put(declaredField.getName(), true);
} else {
nullAbleFieldMap.remove(declaredField.getName());
}
if (annotation.required() && annotation.length() != -1) {
checkLengthFieldMap.put(declaredField.getName(), annotation.length());
}
String entityClassName = annotation.entityClass().getSimpleName().toLowerCase();
String[] fieldNames = fieldNamesMap.get(entityClassName);
if (fieldNames != null) {
boolean existField = false;
for (String fieldName : fieldNames) {
if (fieldName.equals(annotation.fieldName())) {
existField = true;
break;
}
}
if (!existField) {
throw new IllegalArgumentException("The field " + annotation.fieldName() + " not exists in entity class " + annotation.entityClass().getSimpleName());
}
} else {
throw new IllegalArgumentException("Entity class " + annotation.entityClass().getSimpleName() + " not found");
}
}
}
}
}
```
在修改后的代码中,我们将entityClasses作为参数传入到CommonReadListener构造函数中,并在invokeHead方法中获取它们的字段名,然后根据ExcelPropertyCheck注解中的entityClass和fieldName来判断字段是否存在。这样就可以动态获取到entity包中user、admin表的字段名了。
阅读全文