我需要用 Java 反射执行 java bean 的 setXXX() 方法,我现有的变量是 String, 但是setXXX() 的参数类型可能是 Date, int 或者其他。请问如何用 Java 反射实现,或者用 jakarta commons 的包实现也可以,请指点谢谢

解决方案 »

  1.   

    循环取该类所有的方法,将名称为“setXXX”的得到以后就知道是什么类型了,然后再将你的 String 转化成为该类型,然后再调用该方法。
      

  2.   

    将名称为“setXXX”的得到以后就知道是什么类型了 ?????????得到 set 方法,怎么知道类型???谢谢!!!
      

  3.   

    你看一下用反射实现的Visitor设计模式,网上搜一下,有这篇文章的.
      

  4.   

    org.apache.commons.beanutils.BeanUtilsstruts从request里面读form就是利用这个实现的,String转换过程利用到了Converter接口
      

  5.   

    package test;import java.lang.reflect.InvocationTargetException;
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.sql.Date;
    import java.sql.Time;
    import java.sql.Timestamp;
    import java.util.HashMap;
    import java.util.Map;import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.ConversionException;
    import org.apache.commons.beanutils.ConvertUtils;
    import org.apache.commons.beanutils.Converter;
    import org.apache.commons.beanutils.converters.BigDecimalConverter;
    import org.apache.commons.beanutils.converters.BigIntegerConverter;
    import org.apache.commons.beanutils.converters.BooleanConverter;
    import org.apache.commons.beanutils.converters.ByteConverter;
    import org.apache.commons.beanutils.converters.CharacterConverter;
    import org.apache.commons.beanutils.converters.DoubleConverter;
    import org.apache.commons.beanutils.converters.FloatConverter;
    import org.apache.commons.beanutils.converters.IntegerConverter;
    import org.apache.commons.beanutils.converters.LongConverter;
    import org.apache.commons.beanutils.converters.ShortConverter;
    import org.apache.commons.beanutils.converters.SqlDateConverter;
    import org.apache.commons.beanutils.converters.SqlTimeConverter;
    import org.apache.commons.beanutils.converters.SqlTimestampConverter;//默认支持8个原生类型(int...),String, BigDecimal, BigInteger, java.sql.Date, java.sql.Time, java.sql.Timestamp等
    public class Test {
      int x;  Date date;  B b;  public Date getDate() {
        return date;
      }  public int getX() {
        return x;
      }  public B getB() {
        return b;
      }  public void setDate(Date date) {
        this.date = date;
      }  public void setX(int x) {
        this.x = x;
      }  public void setB(B b) {
        this.b = b;
      }  public static void main(String[] args) {
        ConvertUtils.deregister();
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class); //如果转换出错置null,默认报错
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
        ConvertUtils.register(new BooleanConverter(null), Boolean.class);
        ConvertUtils.register(new ByteConverter(null), Byte.class);
        ConvertUtils.register(new CharacterConverter(null), Character.class);
        ConvertUtils.register(new DoubleConverter(null), Double.class);
        ConvertUtils.register(new FloatConverter(null), Float.class);
        ConvertUtils.register(new IntegerConverter(null), Integer.class);
        ConvertUtils.register(new LongConverter(null), Long.class);
        ConvertUtils.register(new ShortConverter(null), Short.class);
        ConvertUtils.register(new SqlDateConverter(null), Date.class);
        ConvertUtils.register(new SqlTimeConverter(null), Time.class);
        ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class);
        ConvertUtils.register(new BConverter(), B.class); //注册
        try {
          Test t = new Test();
          BeanUtils.setProperty(t, "x", "12345"); //单个属性设置
          Map properties = new HashMap();
          properties.put("x", "123");
          properties.put("date", "2005-10-11");
          properties.put("b", "value in b");
          BeanUtils.populate(t, properties); //整个JavaBean设置
          System.out.println(BeanUtils.describe(t)); //与populate相反
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }class B {
      String value;  public String getValue() {
        return value;
      }  public void setValue(String value) {
        this.value = value;
      }
    }class BConverter implements Converter {
      public Object convert(Class type, Object value) {
        try {
          B b = (B) type.newInstance();
          if (value != null) {
            b.setValue(String.valueOf(value));
          }
          return b;
        } catch (Exception ex) {
          throw new ConversionException("Unable to convert the FieldAttribute object!", ex);
        }
      }
    }
      

  6.   

    更能比较强大,支持setProperty(obj, "a.x.y", "") // obj has an a, a has an x, and x has a y
    以及“a[i]”//a是一个数组
      

  7.   

    >得到 set 方法,怎么知道类型???Method.java    /**
         * Returns a <code>Class</code> object that represents the formal return type
         * of the method represented by this <code>Method</code> object.
         * 
         * @return the return type for the method this object represents
         */
        public Class getReturnType() {
    return returnType;
        }