简体   繁体   English

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

[英]Are there any open source Java reflection utilities or jars?

Is there any open source utility or jar for handling reflection in java? 是否有任何开源实用程序或jar用于处理java中的反射?

I am passing methods Dynamically to a class and I would like to fetch the return value. 我正在动态地将方法传递给类,我想获取返回值。

For Example: 例如:

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

    public String getName() {
        return name;
    }

    public  Employee[] getEmployes() {
        return employees;

    }
}

I would like to print all the employees to the console output but instead getting it at run-time like this: 我想将所有员工打印到控制台输出,而是在运行时获取它,如下所示:

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.

Is there any opensource on reflection to accommodate something like this? 有没有反思的开源来容纳这样的东西?

Apart from using Apache BeanUtils or using the java.lang.reflect API directly, as others suggested, you could also use jOOR , which I created to decrease the verbosity of using reflection in Java. 除了使用Apache BeanUtils或直接使用java.lang.reflect API,正如其他人建议的那样,你也可以使用我创建的jOOR来减少在Java中使用反射的冗长程度。 Your example could then be implemented like this: 您的示例可以像这样实现:

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

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

The same example with normal reflection in Java: 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);
}

Also, jOOR wraps the java.lang.reflect.Proxy functionality in a more convenient way: 此外,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);

This kind of thing always rings design alarm bells when I see it. 当我看到它时,这种东西总会响起设计警钟。

That being said, I usually think that JXPath ( http://commons.apache.org/jxpath/users-guide.html ) is the most reasonable solution for that type of problem if it can't be solved in a more engineered way: 话虽这么说,我通常认为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;
        }
    }

}

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

You can use some third-party library as others suggest or can do the trick manually yourself, it is not so hard. 您可以像其他人建议的那样使用某些第三方库,也可以自己手动完成这个技巧,但这并不难。 The following example should illustrate the way one could take: 以下示例应说明可以采取的方式:


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());;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM