View Javadoc

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