1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.digester;
18
19 import java.beans.BeanInfo;
20 import java.beans.Introspector;
21 import java.beans.PropertyDescriptor;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26
27
28
29
30
31
32 public abstract class MappedPropertyRule extends RuleSupport {
33
34
35 private static final Log log = LogFactory.getLog( MappedPropertyRule.class );
36
37 public MappedPropertyRule() {
38 }
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 protected PropertyDescriptor getPropertyDescriptor( Class beanClass,
55 String propertyName ) {
56 if ( beanClass != null && propertyName != null ) {
57 if (log.isTraceEnabled()) {
58 log.trace("Searching for property " + propertyName + " on " + beanClass);
59 }
60 try {
61
62
63
64 BeanInfo beanInfo;
65 if( getXMLIntrospector().getConfiguration().ignoreAllBeanInfo() ) {
66 beanInfo = Introspector.getBeanInfo( beanClass, Introspector.IGNORE_ALL_BEANINFO );
67 }
68 else {
69 beanInfo = Introspector.getBeanInfo( beanClass );
70 }
71 PropertyDescriptor[] descriptors =
72 beanInfo.getPropertyDescriptors();
73 if ( descriptors != null ) {
74 for ( int i = 0, size = descriptors.length; i < size; i++ ) {
75 PropertyDescriptor descriptor = descriptors[i];
76 if ( propertyName.equals( descriptor.getName() ) ) {
77 log.trace("Found matching method.");
78 return descriptor;
79 }
80 }
81 }
82
83 if (beanClass.isInterface()) {
84 Class[] superinterfaces = beanClass.getInterfaces();
85 for (int i=0, size=superinterfaces.length; i<size; i++) {
86 PropertyDescriptor descriptor = getPropertyDescriptor(superinterfaces[i], propertyName);
87 if (descriptor != null)
88 {
89 return descriptor;
90 }
91 }
92 }
93
94 log.trace("No match found.");
95 return null;
96 } catch (Exception e) {
97 log.warn( "Caught introspection exception", e );
98 }
99 }
100 return null;
101 }
102
103
104
105
106
107
108
109
110
111
112 protected Class getPropertyType( String propertyClassName,
113 Class beanClass, String propertyName ) {
114
115
116 if ( propertyClassName != null ) {
117 try {
118 Class answer = loadClass(propertyClassName);
119 if (answer != null) {
120 if (log.isTraceEnabled()) {
121 log.trace("Used specified type " + answer);
122 }
123 return answer;
124 }
125 } catch (Exception e) {
126 log.warn("Cannot load specified type", e);
127 }
128 }
129
130 PropertyDescriptor descriptor =
131 getPropertyDescriptor( beanClass, propertyName );
132 if ( descriptor != null ) {
133 return descriptor.getPropertyType();
134 }
135
136 if (log.isTraceEnabled()) {
137 log.trace("Cannot find property type.");
138 log.trace(" className=" + propertyClassName
139 + " base=" + beanClass + " name=" + propertyName);
140 }
141 return null;
142 }
143 }