如何用反射来改变下列代码中的"bbbList" 成员的值?class aaa {   private List <bbbb> bbbList;}class bbb {
   private String aaa;
}

解决方案 »

  1.   

    import java.beans.IntrospectionException;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.util.ArrayList;
    import java.util.List;public class TestBean
    {
    private String name;
    private int age;
    private boolean merrage;
    public List<Sub> list;
    public List<Sub> getList()
    {
    return list;
    }
    public void setList(List<Sub> list)
    {
    this.list = list;
    }
    public int getAge()
    {
    return age;
    }
    public void setAge(int age)
    {
    this.age = age;
    }
    public boolean isMerrage()
    {
    return merrage;
    }
    public void setMerrage(boolean merrage)
    {
    this.merrage = merrage;
    }
    public String getName()
    {
    return name;
    }
    public void setName(String name)
    {
    this.name = name;
    }

    public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException
    {
    TestBean tb=new TestBean();
    tb.setName("huangfeihong");
    tb.setAge(70);
    tb.setMerrage(false);
    List<Sub> list=new ArrayList<Sub>();
    Sub s1=new Sub();
    Sub s2=new Sub();
    s1.setName("sub1");
    s2.setName("sub2");
    list.add(s1);
    list.add(s2);
    tb.setList(list);
    System.out.println("Before, the list's size is: "+tb.getList().size());
    Field myList=tb.getClass().getField("list");
    List<Sub> myListValue=(List<Sub>)myList.get(tb);
    Sub s3=new Sub();
    s3.setName("sub3");
    myListValue.add(s3);
    myList.set(tb, myListValue);
    System.out.println("After, the list's size is: "+tb.getList().size());
    }
    }class Sub
    {
    private String name; public String getName()
    {
    return name;
    } public void setName(String name)
    {
    this.name = name;
    }

    }这样就可以了,但是这个是你的成员变量list必须是public的。
      

  2.   

    另外,如果你想保持成员变量的私有限制,可以使用java.beans中的API,这个时候你的类必须符合javaBean规范,也就是对于想操作的成员变量,比如有相应的get/set方法,如下:
    import java.beans.IntrospectionException;
    import java.lang.reflect.InvocationTargetException;
    import java.util.ArrayList;
    import java.util.List;public class TestBean
    {
    private String name;
    private int age;
    private boolean merrage;
    private List<Sub> list;
    public List<Sub> getList()
    {
    return list;
    }
    public void setList(List<Sub> list)
    {
    this.list = list;
    }
    public int getAge()
    {
    return age;
    }
    public void setAge(int age)
    {
    this.age = age;
    }
    public boolean isMerrage()
    {
    return merrage;
    }
    public void setMerrage(boolean merrage)
    {
    this.merrage = merrage;
    }
    public String getName()
    {
    return name;
    }
    public void setName(String name)
    {
    this.name = name;
    }

    public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException
    {
    TestBean tb=new TestBean();
    tb.setName("huangfeihong");
    tb.setAge(70);
    tb.setMerrage(false);
    List<Sub> list=new ArrayList<Sub>();
    Sub s1=new Sub();
    Sub s2=new Sub();
    s1.setName("sub1");
    s2.setName("sub2");
    list.add(s1);
    list.add(s2);
    tb.setList(list);
    System.out.println("Before, the list's size is: "+tb.getList().size());
    java.beans.BeanInfo bi=java.beans.Introspector.getBeanInfo(tb.getClass());
    java.beans.PropertyDescriptor[] pds=bi.getPropertyDescriptors();
    for(int i=0;i<pds.length;i++)
    {
    if(pds[i].getName().equals("list"))
    {
    List<Sub> theList=(List<Sub>)pds[i].getReadMethod().invoke(tb, new Object[]{});
    Sub s3=new Sub();
    s3.setName("sub3");
    theList.add(s3);
    pds[i].getWriteMethod().invoke(tb, new Object[]{theList});
    }
    System.out.println(pds[i].getName()+" : "+pds[i].getReadMethod().invoke(tb, new Object[]{}));
    }
    }
    }class Sub
    {
    private String name; public String getName()
    {
    return name;
    } public void setName(String name)
    {
    this.name = name;
    }

    }