-1

我需要有关 Java 反射的帮助。根据附加的代码,我不确定如何在 buildPersonDynamically 方法中动态返回类对象的 ArrayList。逻辑类似于 buildPerson 方法。请参考 MyMain.java。

package reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import reflection.builder.PersonBuilder;
import reflection.one.two.three.Person;

public class MyMain {
public static void main(String[] args) {                
    buildPerson(3);
    buildPersonDynamically(3);
}   
static void buildPerson(int total) {
    PersonBuilder pb = new PersonBuilder();

    pb.build(3);
    ArrayList<Person> per = pb.getDataSet();
    System.out.println(per.get(1).FirstName);
}   
static void buildPersonDynamically(int total) {
    try {
        Class<?> c = java.lang.Class.forName("reflection.builder.PersonBuilder");
        Object obj = c.newInstance();            

        Class<Integer> partypes = Integer.TYPE;
        Method m1 = c.getMethod("build", partypes);
        m1.invoke(obj, total);

        Method m2 = c.getMethod("getFirstName", partypes);
        Object objStr = m2.invoke(obj, 2);
        System.out.println(objStr.toString()); // success

        Method m3 = c.getMethod("getDataSet");

        // Not sure how to get an ArrayList of Person dataSet dynamically.
        // Example, want to get the FirstName from the m3.

    }
    catch (Throwable e) {
        System.err.println(e);
    }
}
}



package reflection.builder;
import java.util.ArrayList;
import reflection.one.two.three.Person;

public class PersonBuilder {
private ArrayList<Person> dataSet;

public ArrayList<Person> getDataSet() {
    return dataSet;
}

public ArrayList<Person> build(int numberOfRecords) {
    ArrayList<String> firstName = new ArrayList<String>();
    ArrayList<String> lastName = new ArrayList<String>();
    ArrayList<Integer> age = new ArrayList<Integer>();
    ArrayList<String> emailAddress = new ArrayList<String>();

    Person per;

    this.dataSet = new ArrayList<Person>();

    firstName.add("Barrack");
    firstName.add("John");
    firstName.add("Whitney");
    firstName.add("Paris");
    firstName.add("Britney");

    lastName.add("Obama");
    lastName.add("Lennon");
    lastName.add("Houston");
    lastName.add("Hilton");
    lastName.add("Spears");

    age.add(47);
    age.add(60);
    age.add(48);
    age.add(32);
    age.add(25);

    emailAddress.add("barrack.obama@gmail.com");
    emailAddress.add("john.lennon@gmail.com");
    emailAddress.add("whitney.houston@gmail.com");
    emailAddress.add("paris.hilton@gmail.com");
    emailAddress.add("britney.spears@gmail.com");

    if (numberOfRecords >= 5) numberOfRecords = 5;
    for (int i=0; i < numberOfRecords; i++) {
        per = new Person();
        per.FirstName = firstName.get(i);
        per.LastName = lastName.get(i);
        per.Age = age.get(i);
        per.EmailAddress = emailAddress.get(i);

        this.dataSet.add(per);
    }   
    return this.dataSet;
}

public String getFirstName(int a) {
    return this.dataSet.get(a).FirstName;
}
}


package reflection.one.two.three;

public class Person {
public String FirstName;
public String LastName;
public int Age;
public String EmailAddress;
}
4

2 回答 2

1

这应该有效:

Method m3 = c.getMethod("getDataSet");
ArrayList<Person> per = (ArrayList<Person>) m3.invoke(obj, null);

该方法的null参数invoke表明没有参数要传递给该方法。

于 2012-07-23T11:05:23.680 回答
1

如果您确定 aArrayList已生成,您可以直接转换它。

Method m3 = c.getMethod("getDataSet");
Object resultM3 = m3.invoke(obj);
ArrayList<Person> resultListM3 = (ArrayList<Person>) resultM3;
于 2012-07-23T11:02:49.517 回答