困扰多年的反射问题,要什么姿势才能正常反射呢?
相关帖子:
http://www.jfinal.com/feedback/4622
http://www.jfinal.com/feedback/409
难道我要重新写对应的get、set方法?应该不是吧!~
很难取舍啊,是这里问题吗?测试过:和这个无关系,没get、set的方法,怎么反射呢?
// 设置是否生成链式 setter 方法,强烈建议配置成 false,否则 fastjson 反序列化会跳过有返回值的 setter 方法 generator.setGenerateChainSetter(false);
打印field出来的是:
public static final com.analysis.model.Company com.analysis.model.Company.dao
调用的方法:
public static <T> boolean outPutExcel(List<T> list, String[] hdNames, String[] hds, String xlsName,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Workbook wb = new HSSFWorkbook(); // 创建工作薄
Sheet sheet = wb.createSheet(); // 创建工作表
sheet.autoSizeColumn((short) 0); // 自适应宽度
// 写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); // 创建行
for (int i = 0; i < hdNames.length; i++) {
CellStyle style = wb.createCellStyle();
// 设置字体和文字大小
Font font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
//font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
font2.setBold(true);
font2.setFontHeightInPoints((short) 12);
// font2.setColor(HSSFColor.RED.index);// 字体颜色:红色
//font2.setColor(HSSFColor.BLACK.index);// 字体颜色:黑色
style.setFont(font2);// 选择需要用到的字体格式
Cell nCell = nRow.createCell(i); // 创建单元格
nCell.setCellValue(hdNames[i]);
nCell.setCellStyle(style);
}
// 写入每一行数据---一条记录就是一行数据
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < hds.length; j++) {
//logger.info("list.get(i):" + list.get(i) + " hds[j]: " + hds[j]);
Object o = getFieldValue(list.get(i), hds[j]); // 得到列的值
data2Excel(sheet, o, i + 1, j); // 将值写入Excel
}
}
return downloadExcel(wb, xlsName, request, response);
}
private static <T> Object getFieldValue(T t, String fieldName) throws Exception {
if (t == null) {
return null;
}
Field[] s = t.getClass().getDeclaredFields();
logger.info("s: " + s);
for (Field field : s) {
logger.info("field: " + field);
}
//logger.info("反射t.getClass(): " + clazz.getClass().getDeclaredField(fieldName));
Field field = t.getClass().getDeclaredField(fieldName);
field.setAccessible(true);//暴力反射
Object o = field.get(t);//得到字段数据的值
return o;
}Generator生成的model:
package com.analysis.model.base;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.IBean;
/**
* Generated by JFinal, do not modify this file.
*/
@SuppressWarnings("serial")
public abstract class BaseCompany<M extends BaseCompany<M>> extends Model<M> implements IBean {
public void setId(java.lang.Integer id) {
set("id", id);
}
public java.lang.Integer getId() {
return getInt("id");
}
public void setCompanyname(java.lang.String companyname) {
set("companyname", companyname);
}
public java.lang.String getCompanyname() {
return getStr("companyname");
}
public void setTshortname(java.lang.String tshortname) {
set("tshortname", tshortname);
}
public java.lang.String getTshortname() {
return getStr("tshortname");
}
public void setManager(java.lang.String manager) {
set("manager", manager);
}
public java.lang.String getManager() {
return getStr("manager");
}
public void setProvince(java.lang.String province) {
set("province", province);
}
public java.lang.String getProvince() {
return getStr("province");
}
public void setCity(java.lang.String city) {
set("city", city);
}
public java.lang.String getCity() {
return getStr("city");
}
public void setCountry(java.lang.String country) {
set("country", country);
}
public java.lang.String getCountry() {
return getStr("country");
}
public void setLongitude(java.lang.Double longitude) {
set("longitude", longitude);
}
public java.lang.Double getLongitude() {
return getDouble("longitude");
}
public void setLatitude(java.lang.Double latitude) {
set("latitude", latitude);
}
public java.lang.Double getLatitude() {
return getDouble("latitude");
}
public void setArea(java.lang.Integer area) {
set("area", area);
}
public java.lang.Integer getArea() {
return getInt("area");
}
public void setIsJoint(java.lang.Integer isJoint) {
set("isJoint", isJoint);
}
public java.lang.Integer getIsJoint() {
return getInt("isJoint");
}
public void setCitylevel(java.lang.Double citylevel) {
set("citylevel", citylevel);
}
public java.lang.Double getCitylevel() {
return getDouble("citylevel");
}
public void setCompanyType(java.lang.Integer companyType) {
set("companyType", companyType);
}
public java.lang.Integer getCompanyType() {
return getInt("companyType");
}
public void setCreated(java.util.Date created) {
set("created", created);
}
public java.util.Date getCreated() {
return get("created");
}
public void setUpdated(java.util.Date updated) {
set("updated", updated);
}
public java.util.Date getUpdated() {
return get("updated");
}
}package com.analysis.model;
import com.analysis.model.base.BaseCompany;
/**
* Generated by JFinal.
*/
@SuppressWarnings("serial")
public class Company extends BaseCompany<Company> {
public static final Company dao = new Company().dao();
}400595 [XNIO-11 task-1] INFO com.tool.ExcelKit [ ] – s: [Ljava.lang.reflect.Field;@42b588 400596 [XNIO-11 task-1] INFO com.tool.ExcelKit [ ] – field: public static final com.analysis.model.Company com.analysis.model.Company.dao java.lang.NoSuchFieldException: id at java.base/java.lang.Class.getDeclaredField(Class.java:2489) at com.tool.ExcelKit.getFieldValue(ExcelKit.java:359) at com.tool.ExcelKit.outPutExcel(ExcelKit.java:299) at com.analysis.company.CompanyController.dowmInExcel(CompanyController.java:340) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at com.jfinal.aop.Invocation.invoke(Invocation.java:97) at com.analysis.admin.auth.AdminAuthInterceptor.intercept(AdminAuthInterceptor.java:50) at com.jfinal.aop.Invocation.invoke(Invocation.java:91) at com.analysis.login.LoginSessionInterceptor.intercept(LoginSessionInterceptor.java:53) at com.jfinal.aop.Invocation.invoke(Invocation.java:91) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:86) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:90) at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77) at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269) at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78) at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133) at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130) at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48) at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:376) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) at java.base/java.lang.Thread.run(Thread.java:832)
项目:JFinal
getFieldValue方法改成getMethodValue (...){
return t.getClass().getMethod("get", String.class).invoke(t, fieldName);}