1 /* -------------------------------------------------------------------
2 * Java source file for the class PretenderImpl
3 *
4 * Copyright (c), 2003, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:46 $
11 *
12 * $Id: PretenderImpl.java,v 1.1.1.1 2003/07/25 04:51:46 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.lang.reflect;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.beans.*;
27 import java.lang.reflect.*;
28 import java.util.*;
29
30 import javax.swing.event.*;
31
32 import net.jbeans.bean.*;
33 import net.jbeans.lang.*;
34
35 /*====================================================================
36 Implementation of class PretenderImpl
37 ====================================================================*/
38 /***
39 * generally describe PretenderImpl in here
40 *
41 * @version $Revision: 1.1.1.1 $
42 * @author Masahiro Takatsuka (masa@jbeans.net)
43 * @see Pretender, IndexedValueSetter, IndexedValueGetter
44 */
45
46 public class PretenderImpl implements Pretender, IndexedValueSetter, IndexedValueGetter {
47 /* ------------------------ not serialized ------------------------ */
48 transient private Vector getters;
49 transient private Vector setters;
50 transient private String[] setterNames;
51 transient private String[] getterNames;
52 transient private PropertyChangeSupport changes;
53
54 /* -------------------------- serialized -------------------------- */
55 private Object source;
56
57 public PretenderImpl() {
58 super();
59 this.changes= new PropertyChangeSupport(this);
60 reset();
61 }
62
63 public PretenderImpl(Object source) {
64 this();
65 setSource(source);
66 }
67
68 private void reset() {
69 this.source = null;
70 this.getters = new Vector();
71 this.setters = new Vector();
72 this.setterNames = new String[] {};
73 this.getterNames = new String[] {};
74 }
75
76 public void addPropertyChangeListener(PropertyChangeListener l) {
77 this.changes.addPropertyChangeListener(l);
78 }
79
80 public void removePropertyChangeListener(PropertyChangeListener l) {
81 this.changes.removePropertyChangeListener(l);
82 }
83
84 /* --------------------- begin : Pretender ---------------------- */
85 public void setSource(Object source) {
86 Object oldSource = this.source;
87 reset(); // clear the Vector;
88 this.source = source;
89 Class clazz = source.getClass();
90 try {
91 // get methods from BeanInfo using getMethodDescriptors.
92 BeanInfo binfo = BeanInfoFinder.getBeanInfo(clazz);
93 MethodDescriptor[] mds = binfo.getMethodDescriptors();
94 for (int i = 0; i < mds.length; i++) {
95 Method method = mds[i].getMethod();
96 String methodName = method.getName();
97 if (methodName.startsWith(SETTER_STR)) {
98 /*
99 * In this version, only the method
100 * with one arg is supportetd.
101 */
102 Class[] paramTypes = method.getParameterTypes();
103 if (paramTypes.length == 1) {
104 addSetter(method);
105 }
106 } else if (methodName.startsWith(GETTER_STR)) {
107 /*
108 * null-arg and return something.
109 */
110 Class[] paramTypes = method.getParameterTypes();
111 Class returnType = method.getReturnType();
112 if ((returnType != Void.TYPE) &&
113 (paramTypes.length == 0)) {
114 addGetter(method);
115 }
116 }
117 }
118 } catch (IntrospectionException ex) {
119 ex.printStackTrace();
120 }
121 setupIndexedValueAccessors();
122
123 this.changes.firePropertyChange("source", oldSource, this.source);
124 }
125
126 public Object getSource() {
127 return this.source;
128 }
129
130 public Method[] getSetters() {
131 Method[] array = new Method[] {};
132 return (Method[]) this.setters.toArray(array);
133 }
134
135 public void setSetters(Method[] methods) {
136 if (this.setters.size() > 0) {
137 this.setters.clear();
138 }
139 int size = methods.length;
140 for (int i = 0; i < size; i++) {
141 this.setters.add(methods[i]);
142 }
143 }
144
145 public Method getSetterAt(int index) {
146 return (Method) this.setters.get(index);
147 }
148
149 public void setSetterAt(int index, Method method) {
150 this.setters.add(index, method);
151 }
152
153 private void addSetter(Method method) {
154 this.setters.add(method);
155 }
156
157 private void addGetter(Method method) {
158 this.getters.add(method);
159 }
160
161 public Method[] getGetters() {
162 Method[] array = new Method[] {};
163 return (Method[]) this.getters.toArray(array);
164 }
165
166 public void setGetters(Method[] methods) {
167 if (this.getters.size() > 0) {
168 this.getters.clear();
169 }
170 int size = methods.length;
171 for (int i = 0; i < size; i++) {
172 this.getters.add(methods[i]);
173 }
174 }
175
176 public Method getGetterAt(int index) {
177 return (Method) this.getters.get(index);
178 }
179
180 public void setGetterAt(int index, Method method) {
181 this.getters.add(index, method);
182 }
183
184 /* ---------------------- end : Pretender ----------------------- */
185
186 private void setupIndexedValueAccessors() {
187 int size = this.setters.size();
188 Method method = null;
189 this.setterNames = new String[size];
190 for (int i = 0; i < size; i++) {
191 method = (Method) this.setters.get(i);
192 String methodName = ClassUtil.formatMethodSignature(method);
193 int idx = methodName.indexOf(SETTER_STR);
194 setSetterNameAt(i, SETTER_PREFIX + methodName.substring(idx + SETTER_STR.length()));
195 }
196 size = this.getters.size();
197 this.getterNames = new String[size];
198 for (int i = 0; i < size; i++) {
199 method = (Method) this.getters.get(i);
200 String methodName = ClassUtil.formatMethodSignature(method);
201 int idx = methodName.indexOf(GETTER_STR);
202 setGetterNameAt(i, GETTER_PREFIX + methodName.substring(idx + GETTER_STR.length()));
203 }
204 }
205
206 /* ----------------- begin : IndexedValueSetter ----------------- */
207 /***
208 * returns the number of available index.
209 */
210 public int getNumberOfSetter() {
211 return this.setterNames.length;
212 }
213
214 /***
215 * sets the number of index.
216 */
217 public void setNumberOfSetter(int num) {
218 // NOP
219 }
220
221 /***
222 * Method to set value at the specified index.
223 */
224 public void setValueAt(int index, Object value) {
225 Object[] args = {value};
226 Method method = (Method) this.setters.get(index);
227 try {
228 ClassUtil.makeObjectPerform(this.source,
229 method.getName(),
230 args
231 );
232 } catch (Exception e) {
233 e.printStackTrace();
234 }
235 }
236
237 /***
238 * set the setter method name for the specified index.
239 */
240 public void setSetterNameAt(int index, String name) {
241 this.setterNames[index] = name;
242 }
243
244 /***
245 * get the setter method name for the specified index.
246 */
247 public final String getSetterNameAt(int index) {
248 return (this.setterNames != null) ? this.setterNames[index]
249 : null;
250 }
251
252 public Method getIndexedValueSetter() {
253 Class[] argTypes = new Class[]{Integer.TYPE, Object.class};
254 return ClassUtil.getMethod(this, "setValueAt", argTypes);
255 }
256
257 /* ------------------ end : IndexedValueSetter ------------------ */
258
259 /* ----------------- begin : IndexedValueGetter ----------------- */
260 /***
261 * returns the number of available index.
262 */
263 public int getNumberOfGetter() {
264 return this.setterNames.length;
265 }
266
267 /***
268 * sets the number of index.
269 */
270 public void setNumberOfGetter(int num) {
271 // NOP
272 }
273
274 /***
275 * Method to set value at the specified index.
276 */
277 public Object getValueAt(int index) {
278 Object[] args = {};
279 Method method = (Method) this.getters.get(index);
280 try {
281 return ClassUtil.makeObjectPerform(this.source,
282 method.getName(),
283 args
284 );
285 } catch (Exception e) {
286 e.printStackTrace();
287 }
288 return null;
289 }
290
291 /***
292 * set the getter method name for the specified index.
293 */
294 public void setGetterNameAt(int index, String name) {
295 this.getterNames[index] = name;
296 }
297
298 /***
299 * get the getter method name for the specified index.
300 */
301 public String getGetterNameAt(int index) {
302 return (this.getterNames != null) ? this.getterNames[index]
303 : null;
304 }
305 /* ------------------ end : IndexedValueGetter ------------------ */
306 }
This page was automatically generated by Maven