1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath;
18
19 import java.beans.BeanInfo;
20 import java.beans.IntrospectionException;
21 import java.beans.Introspector;
22 import java.beans.PropertyDescriptor;
23 import java.util.Arrays;
24 import java.util.Comparator;
25 import java.util.HashMap;
26
27
28
29
30
31
32
33
34
35
36
37
38 public class JXPathBasicBeanInfo implements JXPathBeanInfo {
39 private static final long serialVersionUID = -3863803443111484155L;
40
41 private static final Comparator PROPERTY_DESCRIPTOR_COMPARATOR = new Comparator() {
42 public int compare(Object left, Object right) {
43 return ((PropertyDescriptor) left).getName().compareTo(
44 ((PropertyDescriptor) right).getName());
45 }
46 };
47
48 private boolean atomic = false;
49 private Class clazz;
50 private Class dynamicPropertyHandlerClass;
51 private transient PropertyDescriptor[] propertyDescriptors;
52 private transient HashMap propertyDescriptorMap;
53
54
55
56
57
58 public JXPathBasicBeanInfo(Class clazz) {
59 this.clazz = clazz;
60 }
61
62
63
64
65
66
67
68 public JXPathBasicBeanInfo(Class clazz, boolean atomic) {
69 this.clazz = clazz;
70 this.atomic = atomic;
71 }
72
73
74
75
76
77
78 public JXPathBasicBeanInfo(Class clazz, Class dynamicPropertyHandlerClass) {
79 this.clazz = clazz;
80 this.atomic = false;
81 this.dynamicPropertyHandlerClass = dynamicPropertyHandlerClass;
82 }
83
84
85
86
87
88
89 public boolean isAtomic() {
90 return atomic;
91 }
92
93
94
95
96
97 public boolean isDynamic() {
98 return dynamicPropertyHandlerClass != null;
99 }
100
101 public synchronized PropertyDescriptor[] getPropertyDescriptors() {
102 if (propertyDescriptors == null) {
103 if (clazz == Object.class) {
104 propertyDescriptors = new PropertyDescriptor[0];
105 }
106 else {
107 try {
108 BeanInfo bi = null;
109 if (clazz.isInterface()) {
110 bi = Introspector.getBeanInfo(clazz);
111 }
112 else {
113 bi = Introspector.getBeanInfo(clazz, Object.class);
114 }
115 PropertyDescriptor[] pds = bi.getPropertyDescriptors();
116 PropertyDescriptor[] descriptors = new PropertyDescriptor[pds.length];
117 System.arraycopy(pds, 0, descriptors, 0, pds.length);
118 Arrays.sort(descriptors, PROPERTY_DESCRIPTOR_COMPARATOR);
119 propertyDescriptors = descriptors;
120 }
121 catch (IntrospectionException ex) {
122 ex.printStackTrace();
123 return new PropertyDescriptor[0];
124 }
125 }
126 }
127 if (propertyDescriptors.length == 0) {
128 return propertyDescriptors;
129 }
130 PropertyDescriptor[] result = new PropertyDescriptor[propertyDescriptors.length];
131 System.arraycopy(propertyDescriptors, 0, result, 0, propertyDescriptors.length);
132 return result;
133 }
134
135 public synchronized PropertyDescriptor getPropertyDescriptor(String propertyName) {
136 if (propertyDescriptorMap == null) {
137 propertyDescriptorMap = new HashMap();
138 PropertyDescriptor[] pds = getPropertyDescriptors();
139 for (int i = 0; i < pds.length; i++) {
140 propertyDescriptorMap.put(pds[i].getName(), pds[i]);
141 }
142 }
143 return (PropertyDescriptor) propertyDescriptorMap.get(propertyName);
144 }
145
146
147
148
149
150
151 public Class getDynamicPropertyHandlerClass() {
152 return dynamicPropertyHandlerClass;
153 }
154
155 public String toString() {
156 StringBuffer buffer = new StringBuffer();
157 buffer.append("BeanInfo [class = ");
158 buffer.append(clazz.getName());
159 if (isDynamic()) {
160 buffer.append(", dynamic");
161 }
162 if (isAtomic()) {
163 buffer.append(", atomic");
164 }
165 buffer.append(", properties = ");
166 PropertyDescriptor[] jpds = getPropertyDescriptors();
167 for (int i = 0; i < jpds.length; i++) {
168 buffer.append("\n ");
169 buffer.append(jpds[i].getPropertyType());
170 buffer.append(": ");
171 buffer.append(jpds[i].getName());
172 }
173 buffer.append("]");
174 return buffer.toString();
175 }
176 }