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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.foundation;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
import net.wotonomy.foundation.internal.PropertyComparator;
/**
* A pure java implementation of NSSelector.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 892 $
*/
public class NSSelector implements Comparator, Serializable
{
protected NSMutableDictionary methodMap; // map of classes to methods
protected String methodName;
protected Class[] parameterTypes;
/**
* A marker to indicate object not found.
*/
protected static final String NOT_FOUND = "NOT_FOUND";
/**
* Saves creating a new class array for parameterless method invocation.
*/
protected static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
/**
* Saves creating a new object array for parameterless method invocation.
*/
protected static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
/**
* Constructor specifying a method name and an array of parameter types.
*/
public NSSelector (String aMethodName, Class[] aParameterTypeArray)
{
methodName = aMethodName;
parameterTypes = aParameterTypeArray;
methodMap = new NSMutableDictionary();
}
/**
* Constructor specifying a method name with no parameters.
*/
public NSSelector (String aMethodName)
{
this( aMethodName, EMPTY_CLASS_ARRAY );
}
/**
* Constructor for custom subclasses that implement specific operators
* and that do not use dynamic method invocation.
*/
protected NSSelector()
{
}
/**
* Returns the name of the method.
*/
public String name ()
{
return methodName;
}
/**
* Returns the array of parameter types.
*/
public Class[] parameterTypes ()
{
return parameterTypes;
}
/**
* A String description of this selector.
*/
public String toString ()
{
StringBuffer result = new StringBuffer();
result.append( "[" + getClass().getName() + ": name = " + name() + ", parameter types = [" );
if ( parameterTypes != null )
{
if ( parameterTypes.length > 0 )
{
result.append( parameterTypes[0].toString() );
}
for ( int i = 1; i < parameterTypes.length; i++ )
{
result.append( ", " );
result.append( parameterTypes[i].toString() );
}
}
result.append( "] ]" );
return result.toString();
}
/**
* Returns the appropriate method for the specified class.
*/
public Method methodOnClass (Class aClass)
throws NoSuchMethodException
{
Object result = methodMap.objectForKey( aClass );
if ( result == null )
{
result = getMethodForClass( aClass );
if ( result == null )
{
result = NOT_FOUND;
}
methodMap.setObjectForKey( result, aClass );
}
if ( result == NOT_FOUND )
{
throw new NoSuchMethodException();
}
return (Method) result;
}
/**
* Returns the appropriate method, or null if not found.
*/
private Method getMethodForClass( Class aClass )
{
Method[] methods = aClass.getMethods();
for ( int i = 0; i < methods.length; i++ )
{
if ( methods[i].getName().equals( name() ) )
{
Class[] params = methods[i].getParameterTypes();
if ( params.length == parameterTypes.length )
{
boolean pass = true;
for ( int j = 0; j < params.length; j++ )
{
if ( ! params[j].isAssignableFrom( parameterTypes[j] ) )
{
pass = false;
}
}
if ( pass ) return methods[i];
}
}
}
return null;
}
/**
* Convenience to get a method for an object.
*/
public Method methodOnObject (Object anObject)
throws NoSuchMethodException
{
Method m = methodOnClass( anObject.getClass() );
if ( m == null ) throw new NoSuchMethodException( name() );
return m;
}
/**
* Returns whether the class implements the method for this selector.
*/
public boolean implementedByClass (Class aClass)
{
try
{
methodOnClass( aClass );
return true;
}
catch ( NoSuchMethodException exc )
{
}
return false;
}
/**
* Returns whether the object's class implements the method
* for this selector.
*/
public boolean implementedByObject (Object anObject)
{
try
{
methodOnObject( anObject );
return true;
}
catch ( NoSuchMethodException exc )
{
}
return false;
}
/**
* Invokes this selector's method on the specified object
* using the specified parameters.
*/
public Object invoke (Object anObject, Object[] parameters)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return methodOnObject( anObject ).invoke( anObject, parameters );
}
/**
* Invokes this selector's method on the specified object
* with no parameters.
*/
public Object invoke (Object anObject)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return invoke( anObject, EMPTY_OBJECT_ARRAY );
}
/**
* Invokes this selector's method on the specified object
* with the specified parameter.
*/
public Object invoke (Object anObject, Object aParameter)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return invoke( anObject, new Object[] { aParameter } );
}
/**
* Invokes this selector's method on the specified object
* using the specified two parameters.
*/
public Object invoke (Object anObject, Object p1, Object p2)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return invoke( anObject, new Object[] { p1, p2 } );
}
/**
* Invokes the method with the specified signature on the specified
* object using the specified parameters.
*/
public static Object invoke
(String methodName, Class[] parameterTypes, Object anObject, Object[] parameters)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return new NSSelector( methodName, parameterTypes ).invoke( anObject, parameters );
}
/**
* Invokes the method with the specified signature on the specified object
* with no parameters.
*/
public static Object invoke
(String methodName, Object anObject)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return NSSelector.invoke(
methodName, EMPTY_CLASS_ARRAY, anObject, EMPTY_OBJECT_ARRAY );
}
/**
* Invokes the method with the specified signature on the specified
* object using the specified parameter.
*/
public static Object invoke
(String methodName, Class[] parameterTypes,
Object anObject, Object aParameter)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return NSSelector.invoke(
methodName, parameterTypes, anObject, new Object[] { aParameter } );
}
/**
* Invokes the method with the specified signature on the specified
* object using the specified two parameters.
*/
public static Object invoke
(String methodName, Class[] parameterTypes,
Object anObject, Object p1, Object p2)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException
{
return NSSelector.invoke(
methodName, parameterTypes, anObject, new Object[] { p1, p2 } );
}
// interface Comparator
private Comparator comparator;
/**
* Constructor specifying a method name and a comparator.
* This is not in the spec.
*/
public NSSelector (String aMethodName, Comparator aComparator)
{
this( aMethodName, EMPTY_CLASS_ARRAY );
comparator = aComparator;
}
/**
* Returns the Comparator used for this selector.
* This is not in the spec.
*/
public Comparator comparator()
{
if ( comparator == null )
{
comparator = new PropertyComparator( methodName );
}
return comparator;
}
public int compare(Object o1, Object o2)
{
if ( comparator == null )
{
comparator = new PropertyComparator( methodName );
}
return comparator.compare( o1, o2 );
}
public boolean equals(Object obj)
{
return ( obj == this );
}
}
/*
* $Log$
* Revision 1.1 2006/02/16 12:47:16 cgruber
* Check in all sources in eclipse-friendly maven-enabled packages.
*
* Revision 1.9 2003/02/12 19:34:35 mpowers
* Added accessor for comparator.
*
* Revision 1.8 2003/02/07 20:23:41 mpowers
* Provided backwards compatibility for comparators.
*
* Revision 1.7 2003/01/22 23:02:25 mpowers
* Fixed a null pointer error in NSSelector.toString.
*
* Revision 1.6 2003/01/18 23:46:58 mpowers
* EOSortOrdering is now correctly using NSSelectors.
*
* Revision 1.4 2001/10/31 15:24:45 mpowers
* Implicit constructor is now protected.
*
* Revision 1.3 2001/02/07 19:25:51 mpowers
* Fixed: method matching uses isAssignableFrom rather than ==.
*
* Revision 1.2 2001/01/08 23:30:16 mpowers
* Fixed major bug - selectors were not supposed to share a method map.
*
* Revision 1.1.1.1 2000/12/21 15:47:45 mpowers
* Contributing wotonomy.
*
* Revision 1.3 2000/12/20 16:25:39 michael
* Added log to all files.
*
*
*/
|