01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.jaxen;
04:
05: import net.sourceforge.pmd.ast.Node;
06:
07: import java.lang.reflect.InvocationTargetException;
08: import java.lang.reflect.Method;
09:
10: /**
11: * @author daniels
12: */
13: public class Attribute {
14:
15: private static final Object[] EMPTY_OBJ_ARRAY = new Object[0];
16: private Node parent;
17: private String name;
18: private Method method;
19:
20: public Attribute(Node parent, String name, Method m) {
21: this .parent = parent;
22: this .name = name;
23: this .method = m;
24: }
25:
26: public String getValue() {
27: // this lazy loading reduces calls to Method.invoke() by about 90%
28: try {
29: Object res = method.invoke(parent, EMPTY_OBJ_ARRAY);
30: if (res != null) {
31: if (res instanceof String) {
32: return (String) res;
33: }
34: return String.valueOf(res);
35: }
36: } catch (IllegalAccessException iae) {
37: iae.printStackTrace();
38: } catch (InvocationTargetException ite) {
39: ite.printStackTrace();
40: }
41: return "";
42: }
43:
44: public String getName() {
45: return name;
46: }
47:
48: public Node getParent() {
49: return parent;
50: }
51:
52: public String toString() {
53: return name + ":" + getValue() + ":" + parent;
54: }
55: }
|