如何获取方法返回对象的类型信息(属性和方法)呢?有一个接口:
public interface IView {
public Object execute(HttpServletRequest request);
}
有很多方法实现该接口,但返回值不同,如:
public class UserView implements IView{
  public Object execute(HttpServletRequest request){
    User user=new User();
    ......
    return user;
  }
}
这个类返回的是user对象,另一个类返回的可能是product对象,返回值将送给velocity或freeer模板来显示,我需要开发一个辅助创建velocity模板的工具,即列出每个实现类返回对象的属性和方法,例如UserView类则显示user的属性和方法,请问如何做到这一点呢?还有,假如返回的是一个list或map,如上例改为:
.....
List<User> userList=new ArrayList<User>();
....
return userList;那么我如何获得和描述这个类的返回对象的属性和方法,以给模板开发者帮助呢?(告之为该类开发的模板数据对象可能的属性和方法)非常感谢您的帮助!

解决方案 »

  1.   

    还有,假如返回的是一个list或map,如上例改为: 
    request.setAttributer("user",user);获取的时候List <User> userList=new ArrayList <User>(); 遍历 userlist 就可以了。
      

  2.   

    Map map = new HashMap(); 
    Iterator iter = map.keySet().iterator(); 
    while (iter.hasNext()) { 
    Object key = iter.next(); 
    Object val = map.get(key); 
    } 这里是对map的遍历
      

  3.   

    我不是要遍历!!!
    我是问,如何能通过分析类,获取其返回对象的实际类型信息?(具体类型,而不是接口中的Object)
      

  4.   

    得到它的class名称不就知道它的类型了
      

  5.   

    方法返回值的class名称是Object,这并不是实际的类型,我想得到实际类型
      

  6.   


    正解,每个对象都有一个方法。getClass()。getClassName();  返回类型的方法忘记了,估计也是。getClass()里面
      

  7.   

    问题是我并不执行这个方法,并不获得返回值,而只是分析这个类文件啊,这么说吧:入参只是类名和方法名,我要获得的出参是该类该方法的实际返回类型,即User而不是Object
      

  8.   

    http://jena.sourceforge.net/examples/describe-class/index.html
      

  9.   

    写了个例子
    Test:
    package test;import java.lang.reflect.Field;
    import java.lang.reflect.Method;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    Class c = Class.forName("test.Teacher");
    Method[] m = c.getDeclaredMethods();
    Field[] f = c.getDeclaredFields();
    System.out.println("Field :");
    for (int j = 0; j < f.length; j++) {
    System.out.println(f[j].getName());
    }
    System.out.println("Method :");
    for (int i = 0; i < m.length; i++) {
    Class[] c1 = m[i].getParameterTypes();
    System.out.print(m[i].getName() + "(");
    for (int k = 0; k < c1.length; k++) {
    System.out.print(c1[k].getName());
    if (k < c1.length - 1) {
    System.out.print(",");
    }
    }
    System.out.println(")");
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }Teacher:
    package test;public class Teacher {
    private String name; public String addTeacher(String name) {
    System.out.println("addTeacher : " + name);
    this.setName(name);
    return name;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public Teacher() {
    }
    }
      

  10.   

    dickli1986:
    谢谢您的例子,但是我的问题是想获得方法的实际返回值:改一下您的例子:
    public ObjectString addTeacher(String name) {
            System.out.println("addTeacher : " + name);
            this.setName(name);
            return name;
        }
    方法addTeacher返回值标为Object,实际上是返回了String,我如何能获得String而不是Object呢?
      

  11.   

    把方法定义成以下形式:public <T> T boo(...);
      

  12.   

    土人只会土办法:
    Object intanceof ***
      

  13.   

    谢谢大家的热心帮助!
    我应该改变接口,用范型,如同zhigangxie说的。只有用范型才能带有类型信息并能通过分析得到。
    crazylaa说的Object intanceof ***恐怕也不行吧,这种方式也必须运行时才能得到,而我的需要是不运行方法,而仅是通过分析类文件得到。