jOOR - Java 反射封装库


Apache
跨平台
Java

软件简介

jOOR 在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);  
}

jOOR写法:

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

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