是否有任何开源 Java 反射实用程序或 jars? [英] Are there any open source Java reflection utilities or jars?

查看:39
本文介绍了是否有任何开源 Java 反射实用程序或 jars?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何开源实用程序或 jar 来处理 Java 中的反射?

Is there any open source utility or jar for handling reflection in java?

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

I am passing methods Dynamically to a class and I would like to fetch the return value.

例如:

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?

推荐答案

这种事情我看到的时候总是会敲响设计警钟.

This kind of thing always rings design alarm bells when I see it.

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

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:

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;
        }
    }

}

这篇关于是否有任何开源 Java 反射实用程序或 jars?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆