我要你扮演一个软件工程师。现在我需要你仔细思考,以下代码存在一些问题,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 17:15:28 浏览: 56
在代码中,headClazz获取到的是传入的参数,因此需要确保传入的参数是正确的。如果headClazz获取到的是空值,很可能是传入的参数为空或者不正确。
为了让headClazz能够动态获取到entity包中的user、admin表的字段名,可以使用Java反射机制来实现。代码如下:
```
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());
}
}
}
// 获取entity包中的user、admin表的字段名
try {
Class<?> entityClass = Class.forName("entity." + headClazz.getSimpleName());
Field[] entityFields = entityClass.getDeclaredFields();
for (Field entityField : entityFields) {
String fieldName = entityField.getName();
// 添加到nullAbleFieldMap中
nullAbleFieldMap.put(fieldName, true);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
在代码中,我们通过反射获取到了entity包中的user、admin表的字段名,并将其添加到nullAbleFieldMap中。这样就可以实现动态获取表的字段名了。
阅读全文