1 package net.sourceforge.pmd.jaxen;
2 import net.sourceforge.pmd.ast.Node;
3
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.util.Iterator;
7
8
9 public class AttributeAxisIterator implements Iterator {
10
11 private static final Object[] EMPTY_OBJ_ARRAY = new Object[0];
12 private Object currObj;
13 private Method[] methods;
14 private int position;
15 private Node node;
16
17 public AttributeAxisIterator(Node contextNode) {
18 this.node = contextNode;
19 this.methods = contextNode.getClass().getMethods();
20 this.position = 0;
21 this.currObj = getNextAttribute();
22 }
23
24 public Object next() {
25 if(currObj == null) {
26 throw new IndexOutOfBoundsException();
27 }
28 Object ret = currObj;
29 currObj = getNextAttribute();
30 return ret;
31 }
32
33 public boolean hasNext() {
34 return currObj != null;
35 }
36
37 public void remove() {
38 throw new UnsupportedOperationException();
39 }
40
41 private Attribute getNextAttribute() {
42 while (position < methods.length) {
43 Method method = methods[position];
44 try {
45 if (isAttribute(method)) {
46 Class returnType = method.getReturnType();
47 if (Boolean.TYPE == returnType
48 || String.class == returnType
49 || Integer.TYPE == returnType) {
50 Attribute attribute = getAttribute(node, method);
51 if (attribute != null) {
52 return attribute;
53 }
54 }
55 }
56 } catch (IllegalAccessException e) {
57 e.printStackTrace();
58 } catch (InvocationTargetException e) {
59 e.printStackTrace();
60 } finally {
61 position++;
62 }
63 }
64 return null;
65 }
66
67 protected Attribute getAttribute(Node node, Method method)
68 throws IllegalAccessException, InvocationTargetException {
69 String name = method.getName();
70 name = truncateMethodName(name);
71 Object value = method.invoke(node, EMPTY_OBJ_ARRAY);
72 if (value != null) {
73 if (value instanceof String) {
74 return new Attribute(node, name, (String) value);
75 } else {
76 return new Attribute(node, name, String.valueOf(value));
77 }
78 } else {
79 return null;
80 }
81 }
82
83 protected String truncateMethodName(String name) {
84 if (name.startsWith("is")) {
85 name = name.substring("is".length());
86 } else if (name.startsWith("uses")) {
87 name = name.substring("uses".length());
88 } else if (name.startsWith("has")) {
89 name = name.substring("has".length());
90 } else if (name.startsWith("get")) {
91 name = name.substring("get".length());
92 }
93 return name;
94 }
95
96 protected boolean isAttribute(Method method) {
97 String name = method.getName();
98 Class returnType = method.getReturnType();
99 return (method.getParameterTypes().length == 0)
100 && (Void.TYPE != returnType)
101 && !name.startsWith("jjt")
102 && !name.equals("toString")
103 && !name.equals("getScope")
104 && !name.equals("getClass")
105 && !name.equals("getFinallyBlock")
106 && !name.equals("getCatchBlocks")
107 && !name.equals("getTypeNameNode")
108 && !name.equals("getImportedNameNode")
109 && !name.equals("hashCode");
110 }
111 }
This page was automatically generated by Maven