根据传入的class和property,取的属性的值

来源:互联网 发布:arcgis10.2 mac版 编辑:程序博客网 时间:2024/06/05 17:58

package com.wen;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * <p>Title: </p>
 * <p>Description:根据传入的class和property,取的属性的值</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Reflect {
  public static String get(Object object, String methodname, int i){
    Class cls = object.getClass();
    Object ret;
    try {
      Method method = cls.getMethod(methodname, null);
      ret = method.invoke(cls.newInstance(), null);
      ArrayList array = new ArrayList();
      if (ret.getClass().isArray()) {
        ret = Array.get(ret, i);
      }
    } catch (Exception e) {
      ret = null;
    }
    if(ret == null){
      return null;
    }
    return ret.toString();
  }
}