KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > jaxen > Attribute


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3  */

4 package net.sourceforge.pmd.jaxen;
5
6 import net.sourceforge.pmd.ast.Node;
7
8 import java.lang.reflect.InvocationTargetException JavaDoc;
9 import java.lang.reflect.Method JavaDoc;
10
11 /**
12  * @author daniels
13  */

14 public class Attribute {
15
16     private static final Object JavaDoc[] EMPTY_OBJ_ARRAY = new Object JavaDoc[0];
17     private Node parent;
18     private String JavaDoc name;
19     private Method JavaDoc method;
20
21     public Attribute(Node parent, String JavaDoc name, Method JavaDoc m) {
22         this.parent = parent;
23         this.name = name;
24         this.method = m;
25     }
26
27     public String JavaDoc getValue() {
28         // this lazy loading reduces calls to Method.invoke() by about 90%
29
try {
30             Object JavaDoc res = method.invoke(parent, EMPTY_OBJ_ARRAY);
31             if (res != null) {
32                 if (res instanceof String JavaDoc) {
33                     return (String JavaDoc) res;
34                 }
35                 return String.valueOf(res);
36             }
37         } catch (IllegalAccessException JavaDoc iae) {
38             iae.printStackTrace();
39         } catch (InvocationTargetException JavaDoc ite) {
40             ite.printStackTrace();
41         }
42         return "";
43     }
44
45     public String JavaDoc getName() {
46         return name;
47     }
48
49     public Node getParent() {
50         return parent;
51     }
52
53     public String JavaDoc toString() {
54         return name + ":" + getValue() + ":" + parent;
55     }
56 }
57
Popular Tags