是否有任何开源Java反射实用程序或jar?

时间:2010-12-08 07:09:40

标签: java reflection

是否有用于处理java中反射的开源实用程序或jar?

我正在动态地将方法传递给类,我想获取返回值。

例如:

class Department {
    String name ;
    Employee[] employees;
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public  Employee[] getEmployes() {
        return employees;

    }
}

我想将所有员工打印到控制台输出,而是在运行时将其打印出来,如下所示:

Department dept = new Department();
// add employees..

getEmployeeNames(dept,"getEmployees.getAddress.getStreet");
// So the user says they want employee street, but we don't know that
// until run-tme.

是否有反思的开源来容纳这样的东西?

4 个答案:

答案 0 :(得分:20)

除了使用Apache BeanUtils或直接使用java.lang.reflect API,正如其他人所建议的那样,你也可以使用我创建的jOOR来减少在Java中使用反射的冗长程度。您的示例可以像这样实现:

Employee[] employees = on(department).call("getEmployees").get();

for (Employee employee : employees) {
  Street street = on(employee).call("getAddress").call("getStreet").get();
  System.out.println(street);
}

与Java中的正常反射相同的示例:

try {
  Method m1 = department.getClass().getMethod("getEmployees");
  Employee employees = (Employee[]) m1.invoke(department);

  for (Employee employee : employees) {
    Method m2 = employee.getClass().getMethod("getAddress");
    Address address = (Address) m2.invoke(employee);

    Method m3 = address.getClass().getMethod("getStreet");
    Street street = (Street) m3.invoke(address);

    System.out.println(street);
  }
}

// There are many checked exceptions that you are likely to ignore anyway 
catch (Exception ignore) {

  // ... or maybe just wrap in your preferred runtime exception:
  throw new RuntimeException(e);
}

此外,jOOR以更方便的方式包装java.lang.reflect.Proxy功能:

interface StringProxy {
  String mySubString(int beginIndex);
}

// You can easily create a proxy of a custom type to a jOOR-wrapped object
String substring = on("java.lang.String")
                    .create("Hello World")
                    .as(StringProxy.class)
                    .mySubString(6);

答案 1 :(得分:5)

当我看到它时,这种东西总会敲响警钟。

话虽这么说,我通常认为JXPath(http://commons.apache.org/jxpath/users-guide.html)对于这类问题是最合理的解决方案,如果它不能以更加工程化的方式解决:

import org.apache.commons.jxpath.JXPathContext;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 */
public class JXPather {
    public static void main(String[] args) {
        Department d = new Department();
        d.employees.add(new Employee(new Address("wherever a")));
        d.employees.add(new Employee(new Address("wherever b")));
        d.employees.add(new Employee(new Address("wherever c")));
        d.employees.add(new Employee(new Address("wherever def")));

        JXPathContext context = JXPathContext.newContext(d);
        // access matched xpath objects by iterating over them
        for (Iterator iterator = context.iterate("/employees/address/street"); iterator.hasNext();) {
            System.out.println(iterator.next());
        }

        // or directly via standard xpath expressions

        System.out.println("street of third employee is: "+context.getValue("/employees[3]/address/street"));

        // supports most (or all ?) xpath expressions

        for (Iterator iterator = context.iterate("/employees/address/street[string-length(.) > 11]"); iterator.hasNext();) {
            System.out.println("street length longer than 11 c'ars:"+iterator.next());
        }
    }

    static public class Department {
        List<Employee> employees = new ArrayList<Employee>();
        public List<Employee> getEmployees() {
            return employees;
        }
    }

    static public class Employee {
        Address address;
        Employee(Address address) {
            this.address = address;
        }
        public Address getAddress() {
            return address;
        }

    }

    static public class Address {
        String street;
        Address(String street) {
            this.street = street;
        }
        public String getStreet() {
            return street;
        }
    }

}

答案 2 :(得分:1)

您可以使用apache beanutils:http://commons.apache.org/beanutils/

答案 3 :(得分:1)

您可以像其他人建议的那样使用某些第三方库,或者可以自己手动完成这个技巧,但这并不难。以下示例应说明可以采取的方式:


class Department {
  Integer[] employees;
  public void setEmployes(Integer[] employees) { this.employees = employees; }
  public Integer[] getEmployees() { return employees; }
}

Department dept = new Department();
dept.setEmployes(new Integer[] {1, 2, 3});
Method mEmploees = Department.class.getMethod("getEmployees", new Class[] {});
Object o = mEmploees.invoke(dept, new Object[] {});
Integer[] employees = (Integer[])o;
System.out.println(employees[0].doubleValue());;