01: package jaxx.introspection;
02:
03: import jaxx.*;
04: import jaxx.reflect.*;
05:
06: /** Mirrors the class <code>java.beans.PropertyDescriptor</code>. JAXX uses its own introspector rather than the built-in
07: * <code>java.beans.Introspector</code> so that it can introspect {@link jaxx.reflect.ClassDescriptor},
08: * not just <code>java.lang.Class</code>.
09: */
10: public class JAXXPropertyDescriptor extends JAXXFeatureDescriptor {
11: private ClassDescriptor propertyType;
12: private MethodDescriptor readMethod;
13: private MethodDescriptor writeMethod;
14: private boolean bound;
15:
16: public JAXXPropertyDescriptor(ClassDescriptor classDescriptor,
17: String propertyName) {
18: this (classDescriptor, propertyName, null, null);
19: }
20:
21: public JAXXPropertyDescriptor(ClassDescriptor classDescriptor,
22: String propertyName, MethodDescriptor readMethod,
23: MethodDescriptor writeMethod) {
24: this (classDescriptor, propertyName, readMethod, writeMethod,
25: false);
26: }
27:
28: public JAXXPropertyDescriptor(ClassDescriptor classDescriptor,
29: String propertyName, MethodDescriptor readMethod,
30: MethodDescriptor writeMethod, boolean bound) {
31: super (classDescriptor, propertyName);
32: this .readMethod = readMethod;
33: this .writeMethod = writeMethod;
34: this .bound = bound;
35: }
36:
37: public MethodDescriptor getReadMethodDescriptor() {
38: if (readMethod == null) {
39: try {
40: readMethod = getClassDescriptor().getMethodDescriptor(
41: "get" + capitalize(getName()),
42: new ClassDescriptor[0]);
43: } catch (NoSuchMethodException e) {
44: try {
45: readMethod = getClassDescriptor()
46: .getMethodDescriptor(
47: "is" + capitalize(getName()),
48: new ClassDescriptor[0]);
49: } catch (NoSuchMethodException e2) {
50: }
51: }
52: }
53: return readMethod;
54: }
55:
56: public MethodDescriptor getWriteMethodDescriptor() {
57: if (writeMethod == null) {
58: try {
59: String methodName = "set" + capitalize(getName());
60: MethodDescriptor read = getReadMethodDescriptor();
61: if (read != null) {
62: writeMethod = getClassDescriptor()
63: .getMethodDescriptor(
64: methodName,
65: new ClassDescriptor[] { read
66: .getReturnType() });
67: } else {
68: throw new CompilerException(
69: "Internal error: requesting 'set' method for property of unknown type: '"
70: + getName() + "' (in "
71: + getClassDescriptor() + ")");
72: }
73: } catch (NoSuchMethodException e) {
74: }
75: }
76: return writeMethod;
77: }
78:
79: public ClassDescriptor getPropertyType() {
80: if (propertyType == null) {
81: MethodDescriptor read = getReadMethodDescriptor();
82: if (read != null)
83: propertyType = read.getReturnType();
84: else {
85: MethodDescriptor write = getWriteMethodDescriptor();
86: propertyType = write.getParameterTypes()[0];
87: }
88: }
89: return propertyType;
90: }
91:
92: public boolean isBound() {
93: return bound;
94: }
95:
96: public void setBound(boolean bound) {
97: this.bound = bound;
98: }
99: }
|