posts - 241,  comments - 116,  trackbacks - 0
jOOR (Java Object Oriented Reflection).

对于这样的Java代码:
// Classic example of reflection usage
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);
}
posted on 2012-01-16 09:28 墙头草 阅读(241) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
人人游戏网 软件开发网 货运专家