我在java后台调用一个方法返回给我一个对象,我想遍历得到的这个对象。我使用了反射的方式如下:
ConfigGroupgroup=newConfigGroup();//java.lang.reflect.Field[]fields=group.getClass()....
ConfigGroup group = new ConfigGroup();
// java.lang.reflect.Field[] fields = group.getClass().getFields();
java.lang.reflect.Field[] fields = group.getClass().getDeclaredFields();
if ( fields != null ){
for ( int i = 0; i < fields.length; i++ ){
System.out.println(fields[i].getName() + " - " + fields[i].get("url"));
}
}
当调用getFields方法时fields.length返回为0;当调用getDeclaredFields时。直接报错了内容:
java.lang.IllegalAccessException: Class org.apache.jsp.jsp.map_jsp can not access a member of class cn.gov.customs.casp.config.entity.ConfigGroup with modifiers "private"
sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
java.lang.reflect.Field.get(Field.java:376)
org.apache.jsp.jsp.map_jsp._jspService(map_jsp.java:95)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
com.primeton.access.http.impl.WebInterceptorChainImpl.doIntercept(WebInterceptorChainImpl.java:51)
cn.gov.customs.cadp.components.cupaa.fm.policy.RulesIpFilter.doIntercept(RulesIpFilter.java:50)
com.primeton.access.http.impl.WebInterceptorChainImpl.doIntercept(WebInterceptorChainImpl.java:56)
com.eos.access.http.InterceptorFilter.doFilter(InterceptorFilter.java:120) 展开
// java.lang.reflect.Field[] fields = group.getClass().getFields();
java.lang.reflect.Field[] fields = group.getClass().getDeclaredFields();
if ( fields != null ){
for ( int i = 0; i < fields.length; i++ ){
System.out.println(fields[i].getName() + " - " + fields[i].get("url"));
}
}
当调用getFields方法时fields.length返回为0;当调用getDeclaredFields时。直接报错了内容:
java.lang.IllegalAccessException: Class org.apache.jsp.jsp.map_jsp can not access a member of class cn.gov.customs.casp.config.entity.ConfigGroup with modifiers "private"
sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
java.lang.reflect.Field.get(Field.java:376)
org.apache.jsp.jsp.map_jsp._jspService(map_jsp.java:95)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
com.primeton.access.http.impl.WebInterceptorChainImpl.doIntercept(WebInterceptorChainImpl.java:51)
cn.gov.customs.cadp.components.cupaa.fm.policy.RulesIpFilter.doIntercept(RulesIpFilter.java:50)
com.primeton.access.http.impl.WebInterceptorChainImpl.doIntercept(WebInterceptorChainImpl.java:56)
com.eos.access.http.InterceptorFilter.doFilter(InterceptorFilter.java:120) 展开
2015-04-08 · 知道合伙人互联网行家
关注
展开全部
import java.util.*;
// 测试方法
public class Main {
public static void main(String[] args){
Student student1 = new Student();
student1.setName("张三");
Student student2 = new Student();
student2.setName("李四");
Student student3 = new Student();
student3.setName("王五");
List<Student> list = new ArrayList<Student>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
for (Student student : list) {
System.out.println(student.getName());
}
// 或
// for (int i = 0; i < list.size; i++) {
// System.out.println(list.get(i).getName());
// }
}
}
// Student实体类
public class Student{
private String name;
public Student(){
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
追问
是因为我少引用了util包吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询