1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package groovy.lang;
36
37 import java.lang.reflect.Modifier;
38
39 import org.codehaus.groovy.runtime.MetaClassHelper;
40 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
41
42 /***
43 * Represents a property on a bean which may have a getter and/or a setter
44 *
45 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
46 * @author Pilho Kim
47 * @version $Revision: 4445 $
48 */
49 public class MetaBeanProperty extends MetaProperty {
50
51 private MetaMethod getter;
52 private MetaMethod setter;
53 private MetaFieldProperty field;
54
55 public MetaBeanProperty(String name, Class type, MetaMethod getter, MetaMethod setter) {
56 super(name, type);
57 this.getter = getter;
58 this.setter = setter;
59 }
60
61 /***
62 * Get the property of the given object.
63 *
64 * @param object which to be got
65 * @return the property of the given object
66 * @throws Exception if the property could not be evaluated
67 */
68 public Object getProperty(Object object) {
69 if (getter == null) {
70
71 throw new GroovyRuntimeException("Cannot read write-only property: " + name);
72 }
73 return getter.invoke(object, MetaClassHelper.EMPTY_ARRAY);
74 }
75
76 /***
77 * Set the property on the given object to the new value.
78 *
79 * @param object on which to set the property
80 * @param newValue the new value of the property
81 * @throws RuntimeException if the property could not be set
82 */
83 public void setProperty(Object object, Object newValue) {
84 if (setter == null) {
85 throw new GroovyRuntimeException("Cannot set read-only property: " + name);
86 }
87 newValue = DefaultTypeTransformation.castToType(newValue, getType());
88 setter.invoke(object, new Object[] { newValue });
89 }
90
91 /***
92 * Get the getter method.
93 */
94 public MetaMethod getGetter() {
95 return getter;
96 }
97
98 /***
99 * Get the setter method.
100 */
101 public MetaMethod getSetter() {
102 return setter;
103 }
104
105 /***
106 * This is for MetaClass to patch up the object later when looking for get*() methods.
107 */
108 void setGetter(MetaMethod getter) {
109 this.getter = getter;
110 }
111
112 /***
113 * This is for MetaClass to patch up the object later when looking for set*() methods.
114 */
115 void setSetter(MetaMethod setter) {
116 this.setter = setter;
117 }
118
119 public int getModifiers() {
120 if (setter!=null && getter==null) return setter.getModifiers();
121 if (getter!=null && setter==null) return getter.getModifiers();
122 int modifiers = getter.getModifiers() | setter.getModifiers();
123 int visibility = 0;
124 if (Modifier.isPublic(modifiers)) visibility = Modifier.PUBLIC;
125 if (Modifier.isProtected(modifiers)) visibility = Modifier.PROTECTED;
126 if (Modifier.isPrivate(modifiers)) visibility = Modifier.PRIVATE;
127 int states = getter.getModifiers() & setter.getModifiers();
128 states &= ~(Modifier.PUBLIC|Modifier.PROTECTED|Modifier.PRIVATE);
129 states |= visibility;
130 return states;
131 }
132
133 public void setField(MetaFieldProperty f) {
134 this.field = f;
135 }
136
137 public MetaFieldProperty getField() {
138 return field;
139 }
140 }