62,614
社区成员
发帖
与我相关
我的任务
分享
Exception in thread "main" java.lang.IllegalAccessException: Class reflection.test.CreateExcel can not access a member of class reflection.test.ExcelModel with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
at java.lang.reflect.Field.get(Field.java:376)
我改变了策略:
public class CreateExcel {
public static File ExcelMaking(File file , List<?> model) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method[] method = null;
Field[] field = null;
for ( Object c : model ) {
method = c.getClass().getDeclaredMethods();
field = c.getClass().getDeclaredFields();
// for ( Field m : field ) {
// System.out.println(m.getName() +" : "+m.get( c ));
// }
for ( Method m : method ) {
if ( m.getName().startsWith("get") ) {
System.out.println( m.invoke(c) );
}
}
}
return file;
}
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
List<ExcelModel> list = new LinkedList<>();
ExcelModel em = new ExcelModel("小明","男",true);
list.add(em);
File file = new File("/Users/xia/Desktop/test.xls");
ExcelMaking(file, list);
}
}
class ExcelModel {
public String name;
public String pass;
public boolean love;
public ExcelModel(){}
public ExcelModel(String name, String pass, boolean love) {
super();
this.name = name;
this.pass = pass;
this.love = love;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public boolean getLove() {
return love;
}
public void setLove(boolean love) {
this.love = love;
}
}