jrubyfx-fxmlloader 0.2-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,133 @@
1
+ =begin
2
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
+ *
5
+ * This code is free software; you can redistribute it and/or modify it
6
+ * under the terms of the GNU General Public License version 2 only, as
7
+ * published by the Free Software Foundation. Oracle designates this
8
+ * particular file as subject to the "Classpath" exception as provided
9
+ * by Oracle in the LICENSE file that accompanied this code.
10
+ *
11
+ * This code is distributed in the hope that it will be useful, but WITHOUT
12
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
+ * version 2 for more details (a copy is included in the LICENSE file that
15
+ * accompanied this code).
16
+ *
17
+ * You should have received a copy of the GNU General Public License version
18
+ * 2 along with this work; if not, write to the Free Software Foundation,
19
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
+ *
21
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
+ * or visit www.oracle.com if you need additional information or have any
23
+ * questions.
24
+ */
25
+ =end
26
+
27
+ #package com.sun.javafx.fxml;
28
+ #
29
+ #import java.lang.reflect.InvocationTargetException;
30
+ #import java.lang.reflect.Method;
31
+ #import java.lang.reflect.Modifier;
32
+ #import java.lang.reflect.ParameterizedType;
33
+ #import java.lang.reflect.Type;
34
+ #import java.lang.reflect.TypeVariable;
35
+ #import java.math.BigDecimal;
36
+ #import java.math.BigInteger;
37
+ #import java.util.AbstractMap;
38
+ #import java.util.Arrays;
39
+ #import java.util.HashMap;
40
+ #import java.util.LinkedList;
41
+ #import java.util.List;
42
+ #import java.util.Map;
43
+ #import java.util.Set;
44
+ #
45
+ #import java.lang.reflect.*;
46
+ #
47
+ #import javafx.@beans.value.ObservableValue;
48
+ #import sun.reflect.misc.FieldUtil;
49
+ #import sun.reflect.misc.MethodUtil;
50
+ #import sun.reflect.misc.ReflectUtil;
51
+
52
+ #/**
53
+ # * Exposes Java Bean properties of an object via the {@link Mapend interface.
54
+ # * A call to {@link Map#get(Object)end invokes the getter for the corresponding
55
+ # * property, and a call to {@link Map#put(Object, Object)end invokes the
56
+ # * property's setter. Appending a "Property" suffix to the key returns the
57
+ # * corresponding property model.
58
+ # */
59
+ class RubyObjectWrapperBeanAdapter
60
+ # private final Object @bean;
61
+
62
+
63
+ @@globalMethodCache = {}
64
+
65
+ GET_PREFIX = "get"
66
+ IS_PREFIX = "is"
67
+ SET_PREFIX = "set"
68
+ PROPERTY_SUFFIX = "Property"
69
+
70
+ VALUE_OF_METHOD_NAME = "valueOf"
71
+
72
+ # static
73
+ # contextClassLoader = Thread.currentThread().getContextClassLoader();
74
+ #
75
+ # if (contextClassLoader == nil)
76
+ # raise ArgumentError.new();
77
+ # end
78
+ # end
79
+
80
+ #Creates a Bean.new adapter.
81
+ #
82
+ #@param @bean
83
+ #The Bean object to wrap.
84
+ def initialize(bean)
85
+ @bean = bean
86
+ end
87
+
88
+
89
+ def getBean()
90
+ return @bean;
91
+ end
92
+
93
+ def [](key)
94
+ return @bean.send("get#{key[0].upcase}#{key[1..-1]}")
95
+ end
96
+
97
+ def []=(key, value)
98
+ dputs "calling setterrorab"
99
+ if (key == nil)
100
+ raise "NULL PTR"
101
+ end
102
+ ty = getType(key)
103
+ co = coerce(value, ty)
104
+ @bean.send("set#{key[0].upcase}#{key[1..-1]}", co);
105
+ return nil;
106
+ end
107
+
108
+ def read_only?(key)
109
+ if (key == nil)
110
+ raise "NULL PTR"
111
+ end
112
+ @bean.methods.include? "set#{key[0].upcase}#{key[1..-1]}"
113
+ end
114
+
115
+ def getType(key)
116
+ if (key == nil)
117
+ raise ArgumentError.new();
118
+ end
119
+ @bean.send(key + "GetType")
120
+ end
121
+
122
+ def getPropertyModel(key)
123
+ if (key == nil)
124
+ raise ArgumentError.new();
125
+ end
126
+
127
+ return @bean.send("#{key}Property")
128
+ end
129
+
130
+ def coerce(value, type)
131
+ RubyWrapperBeanAdapter.coerce(value, type)
132
+ end
133
+ end
@@ -0,0 +1,1007 @@
1
+ =begin
2
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
+ *
5
+ * This code is free software; you can redistribute it and/or modify it
6
+ * under the terms of the GNU General Public License version 2 only, as
7
+ * published by the Free Software Foundation. Oracle designates this
8
+ * particular file as subject to the "Classpath" exception as provided
9
+ * by Oracle in the LICENSE file that accompanied this code.
10
+ *
11
+ * This code is distributed in the hope that it will be useful, but WITHOUT
12
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
+ * version 2 for more details (a copy is included in the LICENSE file that
15
+ * accompanied this code).
16
+ *
17
+ * You should have received a copy of the GNU General Public License version
18
+ * 2 along with this work; if not, write to the Free Software Foundation,
19
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
+ *
21
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
+ * or visit www.oracle.com if you need additional information or have any
23
+ * questions.
24
+ */
25
+ =end
26
+
27
+ #/**
28
+ # * Exposes Java Bean properties of an object via the {@link Mapend interface.
29
+ # * A call to {@link Map#get(Object)end invokes the getter for the corresponding
30
+ # * property, and a call to {@link Map#put(Object, Object)end invokes the
31
+ # * property's setter. Appending a "Property" suffix to the key returns the
32
+ # * corresponding property model.
33
+ # */
34
+ class RubyWrapperBeanAdapter
35
+ # private final Object @bean;
36
+
37
+
38
+ @@globalMethodCache = {}
39
+
40
+ GET_PREFIX = "get"
41
+ IS_PREFIX = "is"
42
+ SET_PREFIX = "set"
43
+ PROPERTY_SUFFIX = "Property"
44
+
45
+ VALUE_OF_METHOD_NAME = "valueOf"
46
+
47
+ # static
48
+ # contextClassLoader = Thread.currentThread().getContextClassLoader();
49
+ #
50
+ # if (contextClassLoader == nil)
51
+ # raise ArgumentError.new();
52
+ # end
53
+ # end
54
+
55
+ #Creates a Bean.new adapter.
56
+ #
57
+ #@param @bean
58
+ #The Bean object to wrap.
59
+ def initialize(bean)
60
+ @bean = bean;
61
+
62
+ type = @bean.java_class
63
+
64
+ while (type != Java.java.lang.Object.java_class && !@@globalMethodCache.has_key?(type))
65
+ @@globalMethodCache[type] = getClassMethodCache(type)
66
+ type = type.superclass();
67
+ end
68
+ end
69
+
70
+ def getClassMethodCache(type)
71
+ classMethodCache = {}
72
+
73
+ ReflectUtil.checkPackageAccess(type);
74
+ declaredMethods = type.declared_instance_methods();
75
+ declaredMethods.each do |method|
76
+ modifiers = method.modifiers();
77
+
78
+ if (Modifier.public?(modifiers) && !Modifier.static?(modifiers))
79
+ name = method.name();
80
+ namedMethods = classMethodCache[name]
81
+
82
+ if (namedMethods == nil)
83
+ namedMethods = []
84
+ classMethodCache[name] = namedMethods
85
+ end
86
+
87
+ namedMethods << (method)
88
+ end
89
+ end
90
+
91
+ return classMethodCache;
92
+ end
93
+
94
+ # /**
95
+ # * Returns the Bean object this adapter wraps.
96
+ # *
97
+ # * @return
98
+ # * The Bean object, or <tt>nil</tt> if no Bean has been set.
99
+ # */
100
+ def getBean()
101
+ return @bean;
102
+ end
103
+
104
+ def getMethod(name, *parameterTypes)
105
+ type = @bean.java_class();
106
+
107
+ method = nil;
108
+ while (type != Java::java.lang.Object.java_class)
109
+ classMethodCache = @@globalMethodCache[(type)] || {}
110
+
111
+ namedMethods = classMethodCache[name]
112
+ if (namedMethods != nil)
113
+ for namedMethod in namedMethods
114
+ if (namedMethod.name() == (name) && namedMethod.parameter_types() == parameterTypes)
115
+ method = namedMethod;
116
+ break;
117
+ end
118
+ end
119
+ end
120
+
121
+ if (method != nil)
122
+ break;
123
+ end
124
+ type = type.superclass();
125
+ end
126
+
127
+ return method;
128
+ end
129
+
130
+ def getGetterMethod( key)
131
+ getterMethod = getMethod(getMethodName(GET_PREFIX, key));
132
+
133
+ if (getterMethod == nil)
134
+ getterMethod = getMethod(getMethodName(IS_PREFIX, key));
135
+ end
136
+
137
+ return getterMethod;
138
+ end
139
+
140
+ def getSetterMethod( key)
141
+ type = getType(key);
142
+
143
+ if (type == nil)
144
+ raise UnsupportedOperationException.new("Cannot determine type for property.");
145
+ end
146
+
147
+ return getMethod(getMethodName(SET_PREFIX, key), type)
148
+ end
149
+
150
+ def getMethodName(prefix, key)
151
+ return prefix + key[0].upcase + key[1..-1];
152
+ end
153
+
154
+ def [](key)
155
+ key = key.to_s
156
+ getterMethod = key.end_with?(PROPERTY_SUFFIX) ? getMethod(key) : getGetterMethod(key);
157
+
158
+ value = nil
159
+ if (getterMethod != nil)
160
+ begin
161
+ value = getterMethod.invoke @bean
162
+ rescue IllegalAccessException => exception
163
+ raise RuntimeException.new(exception);
164
+ rescue InvocationTargetException => exception
165
+ raise RuntimeException.new(exception);
166
+ end
167
+ else
168
+ value = nil;
169
+ end
170
+
171
+ return value;
172
+ end
173
+
174
+ # /**
175
+ # * Invokes a setter method for the given property. The
176
+ # * {@link #coerce(Object, Class)end method is used as needed to attempt to
177
+ # * convert a given value to the property type, as defined by the return
178
+ # * value of the getter method.
179
+ # *
180
+ # * @param key
181
+ # * The property name.
182
+ # *
183
+ # * @param value
184
+ # * The property.new value.
185
+ # *
186
+ # * @return
187
+ # * Returns <tt>nil</tt>, since returning the previous value would require
188
+ # * an unnecessary call to the getter method.
189
+ # *
190
+ # * @throws PropertyNotFoundException
191
+ # * If the given property does not exist or is read-only.
192
+ # */
193
+ # @Override
194
+ def []=(key, value)
195
+ dputs "calling setter"
196
+ if (key == nil)
197
+ raise "NULL PTR"
198
+ end
199
+
200
+ setterMethod = getSetterMethod(key);
201
+
202
+ if (setterMethod == nil)
203
+ dputs caller
204
+ dputs "error in []=!"
205
+ dp key, value, @bean
206
+ raise PropertyNotFoundException.new("Property \"" + key + "\" does not exist" + " or is read-only.");
207
+ end
208
+ begin
209
+ ty = getType(key)
210
+ co = coerce(value, ty)
211
+ setterMethod.invoke(@bean, co );
212
+ rescue IllegalAccessException => exception
213
+ dp "issues1"
214
+ dp exception
215
+ raise "RuntimeException.new(exception);"
216
+ rescue InvocationTargetException => exception
217
+ dp "issues2"
218
+ dp exception
219
+ raise R"untimeException.new(exception);"
220
+ end
221
+ dputs "eone"
222
+ return nil;
223
+ end
224
+
225
+ # /**
226
+ # * Verifies the existence of a property.
227
+ # *
228
+ # * @param key
229
+ # * The property name.
230
+ # *
231
+ # * @return
232
+ # * <tt>true</tt> if the property exists; <tt>false</tt>, otherwise.
233
+ # */
234
+ # @Override
235
+ def has_key?( key)
236
+ if (key == nil)
237
+ raise "NULL PTR"
238
+ end
239
+
240
+ return getType(key.to_s) != nil;
241
+ end
242
+
243
+ def entrySet()
244
+ raise UnsupportedOperationException.new();
245
+ end
246
+
247
+ # /**
248
+ # * Tests the mutability of a property.
249
+ # *
250
+ # * @param key
251
+ # * The property name.
252
+ # *
253
+ # * @return
254
+ # * <tt>true</tt> if the property is read-only; <tt>false</tt>, otherwise.
255
+ # */
256
+ def read_only?(key)
257
+ if (key == nil)
258
+ raise "NULL PTR"
259
+ end
260
+ dputs "checking for readonly-ness of #{key}:"
261
+ dp getSetterMethod(key)
262
+ return getSetterMethod(key) == nil;
263
+ end
264
+
265
+ # /**
266
+ # * Returns the property model for the given property.
267
+ # *
268
+ # * @param key
269
+ # * The property name.
270
+ # *
271
+ # * @return
272
+ # * The named property model, or <tt>nil</tt> if no such property exists.
273
+ # */
274
+ # @SuppressWarnings("unchecked")
275
+ def getPropertyModel( key)
276
+ if (key == nil)
277
+ raise ArgumentError.new();
278
+ end
279
+
280
+ return self[key + PROPERTY_SUFFIX]
281
+ end
282
+
283
+ # /**
284
+ # * Returns the type of a property.
285
+ # *
286
+ # * @param key
287
+ # * The property name.
288
+ # */
289
+ def getType(key)
290
+ if (key == nil)
291
+ raise ArgumentError.new();
292
+ end
293
+
294
+ getterMethod = getGetterMethod(key);
295
+
296
+ return (getterMethod == nil) ? nil : getterMethod.return_type();
297
+ end
298
+
299
+ # /**
300
+ # * Returns the generic type of a property.
301
+ # *
302
+ # * @param key
303
+ # * The property name.
304
+ # */
305
+ def getGenericType(key)
306
+ if (key == nil)
307
+ raise ArgumentError.new();
308
+ end
309
+
310
+ getterMethod = getGetterMethod(key);
311
+ dputs "GOt getter method for #{key}"
312
+ dp getterMethod
313
+ dputs getterMethod
314
+
315
+ return (getterMethod == nil) ? nil : getterMethod.return_type
316
+ end
317
+
318
+ def coerce(value, type)
319
+ RubyWrapperBeanAdapter.coerce(value, type)
320
+ end
321
+ # /**
322
+ # * Coerces a value to a given type.
323
+ # *
324
+ # * @param value
325
+ # * @param type
326
+ # *
327
+ # * @return
328
+ # * The coerced value.
329
+ # */
330
+ # @SuppressWarnings("unchecked")
331
+ def self.coerce( value, type)
332
+ dputs "coercing..."
333
+ if (type == nil)
334
+ dputs "WHAT!"
335
+ raise "ArgumentError.new();"
336
+ end
337
+ if (value.class == Java::JavaObject)
338
+ dputs "de-objectifying it!!!!"
339
+ dp value.class
340
+ dp value.java_class
341
+ dp value.to_java
342
+ value = value.to_java
343
+ end
344
+
345
+ coercedValue = nil;
346
+ if (value == nil)
347
+ # Null values can only be coerced to nil
348
+ coercedValue = nil;
349
+ elsif (value.is_a?(EventHandlerWrapper) && type == Java.javafx.event.EventHandler.java_class) || (value.respond_to?(:java_class) && !value.is_a?(EventHandlerWrapper) && type.assignable_from?(value.java_class))
350
+ # Value doesn't require coercion
351
+ coercedValue = value;
352
+ elsif !value.respond_to?(:java_class) && !type.enum?
353
+ # its a ruby value
354
+ dir = ->(x){x}
355
+ to_x = ->(x){->(o){o.send(x)}}
356
+ to_dbl = ->(x){java.lang.Double.valueOf(x.to_s)}
357
+ to_bool = ->(x){java.lang.Boolean.valueOf(x.to_s)}
358
+ value_of = ->(x){type.ruby_class.valueOf(x.to_s)}
359
+ # TODO: Java::double[].java_class.component_type
360
+ mapper = {
361
+ [String, java.lang.String.java_class] => dir,
362
+ [Fixnum, java.lang.Integer.java_class] => dir,
363
+ [Float, java.lang.Double.java_class] => dir,
364
+ [Float, Java::double.java_class] => dir,
365
+ [String, Java::double.java_class] => to_dbl,
366
+ [String, java.lang.Double.java_class] => to_dbl,
367
+ [String, Java::int.java_class] => to_x.call(:to_i),
368
+ [String,java.lang.Integer.java_class] => to_x.call(:to_i),
369
+ [String, Java::boolean.java_class] => to_bool,
370
+ [String, Java::javafx.scene.paint.Paint.java_class] => value_of,
371
+ [String, Java::java.lang.Object.java_class] => ->(x){x},
372
+ [String, Java::double[].java_class] => ->(x){x.split(/[, ]+/).map(&:to_f)}
373
+ }
374
+ if mapper[[value.class, type]]
375
+ coercedValue = mapper[[value.class, type]].call(value)
376
+ else
377
+ dputs "!! Non-normal RUBY coerce (#{value}, #{type}) (#{value.inspect}, [#{value.class}, #{type.inspect}])"
378
+ raise "Unknown Coercion map: (#{value}, #{type}) (#{value.inspect}, [#{value.class}, #{type.inspect}]; Please file a bug on this."
379
+ end
380
+ # Ruby String :D
381
+ elsif value.class == String && type.enum?
382
+ if value[0] == value[0].downcase
383
+ puts "WHOA Value is not #{value}"
384
+ #TODO: does this need proper snake casing when upcasting?
385
+ value = value.upcase
386
+ end
387
+ coercedValue = type.ruby_class.valueOf(value)
388
+ elsif value.respond_to?(:java_class) && value.java_class == Java::java.net.URL.java_class && type == Java::java.lang.String.java_class
389
+ # TODO: HACK!
390
+ dputs "COnverting url to string"
391
+ coercedValue = value.to_s
392
+ else
393
+ dputs "!! Non-normal coerce (#{value}, #{type}) (#{value.inspect}, #{type.inspect})"
394
+ if (type == java.lang.Boolean.java_class || type == Boolean.TYPE)
395
+ coercedValue = Boolean.valueOf(value.toString());
396
+ elsif (type == Character.java_class || type == Character.TYPE)
397
+ coercedValue = value.toString().charAt(0);
398
+ elsif (type == Byte.java_class || type == Byte.TYPE)
399
+ if (value.is_a? Number)
400
+ coercedValue = (value).byteValue();
401
+ else
402
+ coercedValue = Byte.valueOf(value.toString());
403
+ end
404
+ elsif (type == Short.java_class || type == Short.TYPE)
405
+ if (value.is_a? Number)
406
+ coercedValue = (value).shortValue();
407
+ else
408
+ coercedValue = Short.valueOf(value.toString());
409
+ end
410
+ elsif (type == Integer.java_class || type == Integer.TYPE)
411
+ if (value.is_a? Number)
412
+ coercedValue = (value).intValue();
413
+ else
414
+ coercedValue = Integer.valueOf(value.toString());
415
+ end
416
+ elsif (type == Long.java_class || type == Long.TYPE)
417
+ if (value.is_a? Number)
418
+ coercedValue = (value).longValue();
419
+ else
420
+ coercedValue = Long.valueOf(value.toString());
421
+ end
422
+ elsif (type == BigInteger.java_class)
423
+ if (value.is_a? Number)
424
+ coercedValue = BigInteger.valueOf((value).longValue());
425
+ else
426
+ coercedValue = BigInteger.new(value.toString());
427
+ end
428
+ elsif (type == Float.java_class || type == Float.TYPE)
429
+ if (value.is_a? Number)
430
+ coercedValue = (value).floatValue();
431
+ else
432
+ coercedValue = Float.valueOf(value.toString());
433
+ end
434
+ elsif (type == Double.java_class || type == Double.TYPE)
435
+ if (value.is_a? Number)
436
+ coercedValue = (value).doubleValue();
437
+ else
438
+ coercedValue = Double.valueOf(value.toString());
439
+ end
440
+ elsif (type == Number.java_class)
441
+ number = value.toString();
442
+ if (number.contains("."))
443
+ coercedValue = Double.valueOf(number);
444
+ else
445
+ coercedValue = Long.valueOf(number);
446
+ end
447
+ elsif (type == BigDecimal.java_class)
448
+ if (value.is_a? Number)
449
+ coercedValue = BigDecimal.valueOf((value).doubleValue());
450
+ else
451
+ coercedValue = BigDecimal.new(value.toString());
452
+ end
453
+ elsif (type == Class.java_class)
454
+ begin
455
+ ReflectUtil.checkPackageAccess(value.toString());
456
+ coercedValue = Class.forName(
457
+ value.to_s,
458
+ false,
459
+ JRuby.runtime.get_class_loader);
460
+ rescue ClassNotFoundException => exception
461
+ raise Exception.new(exception);
462
+ end
463
+ else
464
+ dputs "elsee"
465
+ valueType = value.java_class();
466
+ valueOfMethod = nil;
467
+
468
+ while (valueOfMethod == nil && valueType != nil)
469
+ begin
470
+ dputs "checking access"
471
+ ReflectUtil.checkPackageAccess(type);
472
+ valueOfMethod = type.declared_method(VALUE_OF_METHOD_NAME, valueType);
473
+ rescue NoSuchMethodException => exception
474
+ # No-op
475
+ end
476
+
477
+ if (valueOfMethod == nil)
478
+ valueType = valueType.superclass();
479
+ end
480
+ end
481
+
482
+ if (valueOfMethod == nil)
483
+ raise IllegalArgumentException.new("Unable to coerce " + value + " to " + type + ".");
484
+ end
485
+
486
+ if type.isEnum() && value.is_a?(String) && value[0] == value[0].downcase
487
+ value = RubyWrapperBeansAdapter.toUpcase value;
488
+ end
489
+
490
+ begin
491
+ coercedValue = MethodUtil.invoke(valueOfMethod, nil, [ value ]);
492
+ rescue IllegalAccessException => exception
493
+ dputs "EAI1"
494
+ dp exception
495
+ raise "RuntimeException.new(exception);"
496
+ rescue InvocationTargetException => exception
497
+ dputs "ETI1"
498
+ dp exception
499
+ raise "RuntimeException.new(exception);"
500
+ rescue SecurityException => exception
501
+ dputs "SE1"
502
+ dp exception
503
+ raise "RuntimeException.new(exception);"
504
+ end
505
+ end
506
+ end
507
+ dputs "Coerced #{value.class} into a #{coercedValue.class} for #{type}"
508
+ dp value, coercedValue
509
+ return coercedValue;
510
+ end
511
+
512
+ # /**
513
+ # * Invokes the static getter method for the given property.
514
+ # *
515
+ # * @param target
516
+ # * The object to which the property is attached.
517
+ # *
518
+ # * @param sourceType
519
+ # * The class that defines the property.
520
+ # *
521
+ # * @param key
522
+ # * The property name.
523
+ # *
524
+ # * @return
525
+ # * The value returned by the method, or <tt>nil</tt> if no such method
526
+ # * exists.
527
+ # */
528
+ # @SuppressWarnings("unchecked")
529
+ def self.get3(target, sourceType, key)
530
+ value = nil;
531
+
532
+ targetType = target.java_class();
533
+ getterMethod = getStaticGetterMethod(sourceType, key, targetType);
534
+
535
+ if (getterMethod != nil)
536
+ begin
537
+ value = MethodUtil.invoke(getterMethod, nil, [target ] );
538
+ rescue InvocationTargetException => exception
539
+ raise RuntimeException.new(exception);
540
+ rescue IllegalAccessException => exception
541
+ raise RuntimeException.new(exception);
542
+ end
543
+ end
544
+
545
+ return value;
546
+ end
547
+
548
+ # /**
549
+ # * Invokes a static setter method for the given property. If the value is
550
+ # * <tt>nil</tt> or there is no explicit setter for a given type, the
551
+ # * {@link #coerce(Object, Class)end method is used to attempt to convert the
552
+ # * value to the actual property type (defined by the return value of the
553
+ # * getter method).
554
+ # *
555
+ # * @param target
556
+ # * The object to which the property is or will be attached.
557
+ # *
558
+ # * @param sourceType
559
+ # * The class that defines the property.
560
+ # *
561
+ # * @param key
562
+ # * The property name.
563
+ # *
564
+ # * @param value
565
+ # * The property.new value.
566
+ # *
567
+ # * @throws PropertyNotFoundException
568
+ # * If the given static property does not exist or is read-only.
569
+ # */
570
+ def self.put3(target, sourceType, key, value)
571
+ targetType = nil
572
+ if target.respond_to? :java_class
573
+ targetType = target.java_class();
574
+ elsif target.is_a? String
575
+ targetType = java.lang.String.java_class
576
+ else
577
+ dp target, sourceType, key, value
578
+ raise "Shoots!"
579
+ end
580
+
581
+ setterMethod = nil;
582
+ if (value != nil)
583
+ valueClass = nil
584
+
585
+ if value.respond_to? :java_class
586
+ valueClass = value.java_class();
587
+ elsif value.is_a? String
588
+ valueClass = java.lang.String.java_class
589
+ else
590
+ dp target, sourceType, key, value
591
+ raise "Shoots TWICE!"
592
+ end
593
+ setterMethod = getStaticSetterMethod(sourceType, key, valueClass, targetType);
594
+ end
595
+
596
+ if (setterMethod == nil)
597
+ # Get the property type and attempt to coerce the value to it
598
+ propertyType = getType(sourceType, key, targetType);
599
+
600
+ if (propertyType != nil)
601
+ setterMethod = getStaticSetterMethod(sourceType, key, propertyType, targetType);
602
+ value = coerce(value, propertyType);
603
+ end
604
+ end
605
+
606
+ if (setterMethod == nil)
607
+ raise PropertyNotFoundException.new("Static property \"" + key + "\" does not exist" + " or is read-only.");
608
+ end
609
+
610
+ # Invoke the setter
611
+ begin
612
+ getStaticSetterMethod(sourceType, key, valueClass, targetType, true).call(target.java_object, value);
613
+ rescue InvocationTargetException => exception
614
+ raise "RuntimeException.new(exception);"
615
+ rescue IllegalAccessException => exception
616
+ raise " RuntimeException.new(exception);"
617
+ end
618
+ end
619
+
620
+ # /**
621
+ # * Tests the existence of a static property.
622
+ # *
623
+ # * @param sourceType
624
+ # * The class that defines the property.
625
+ # *
626
+ # * @param key
627
+ # * The property name.
628
+ # *
629
+ # * @param targetType
630
+ # * The type of the object to which the property applies.
631
+ # *
632
+ # * @return
633
+ # * <tt>true</tt> if the property exists; <tt>false</tt>, otherwise.
634
+ # */
635
+ def self.isDefined( sourceType, key, targetType)
636
+ return (getStaticGetterMethod(sourceType, key, targetType) != nil);
637
+ end
638
+
639
+ # /**
640
+ # * Returns the type of a static property.
641
+ # *
642
+ # * @param sourceType
643
+ # * The class that defines the property.
644
+ # *
645
+ # * @param key
646
+ # * The property name.
647
+ # *
648
+ # * @param targetType
649
+ # * The type of the object to which the property applies.
650
+ # */
651
+ def self.getType(sourceType, key, targetType)
652
+ getterMethod = getStaticGetterMethod(sourceType, key, targetType);
653
+ return (getterMethod == nil) ? nil : getterMethod.return_type();
654
+ end
655
+
656
+ # /**
657
+ # * Returns the generic type of a static property.
658
+ # *
659
+ # * @param sourceType
660
+ # * The class that defines the property.
661
+ # *
662
+ # * @param key
663
+ # * The property name.
664
+ # *
665
+ # * @param targetType
666
+ # * The type of the object to which the property applies.
667
+ # */
668
+ def self.getGenericType( sourceType, key, targetType)
669
+ Method getterMethod = getStaticGetterMethod(sourceType, key, targetType);
670
+ return (getterMethod == nil) ? nil : getterMethod.getGenericReturnType();
671
+ end
672
+
673
+ # /**
674
+ # * Determines the type of a list item.
675
+ # *
676
+ # * @param listType
677
+ # */
678
+ def self.getListItemType(listType)
679
+ itemType = getGenericListItemType(listType);
680
+
681
+ if (itemType.is_a? ParameterizedType)
682
+ itemType = (itemType).getRawType();
683
+ end
684
+ dputs "Listem item type is for "
685
+ dp listType, itemType
686
+ return itemType;
687
+ end
688
+
689
+ # /**
690
+ # * Determines the type of a map value.
691
+ # *
692
+ # * @param listType
693
+ # */
694
+ def self.getMapValueType( mapType)
695
+ valueType = getGenericMapValueType(mapType);
696
+
697
+ if (valueType.is_a? ParameterizedType)
698
+ valueType = (valueType).getRawType();
699
+ end
700
+
701
+ return valueType;
702
+ end
703
+
704
+ # /**
705
+ # * Determines the type of a list item.
706
+ # *
707
+ # * @param listType
708
+ # */
709
+ def self.getGenericListItemType(listType)
710
+ itemType = nil;
711
+
712
+ parentType = listType;
713
+ dputs "searching for generic #{listType}"
714
+ while (parentType != nil)
715
+ dputs "Still not nill"
716
+ dp parentType
717
+ if (parentType.is_a? ParameterizedType)
718
+ dputs "Parametratized type!"
719
+ parameterizedType = parentType;
720
+ rawType = parameterizedType.getRawType();
721
+ dp rawType, parameterizedType
722
+ if (List.java_class.assignable_from?(rawType))
723
+ itemType = parameterizedType.getActualTypeArguments()[0];
724
+ dputs "OOOOOHHH item type is #{itemType}"
725
+ dp itemType
726
+ end
727
+
728
+ break;
729
+ end
730
+
731
+ classType = parentType;
732
+ dputs "checinhg generic interfaces"
733
+ genericInterfaces = classType.generic_interfaces();
734
+
735
+ genericInterfaces.each do |genericInterface|
736
+ dputs "serarcing ingeraface"
737
+ dp genericInterface
738
+ if (genericInterface.is_a? ParameterizedType)
739
+ parameterizedType = genericInterface;
740
+ interfaceType = parameterizedType.getRawType();
741
+ dputs "checking"
742
+ dp parameterizedType, interfaceType
743
+ if (List.java_class.assignable_from?(interfaceType.java_class)) || (List.java_class.assignable_from?(interfaceType.java_object))
744
+ itemType = parameterizedType.getActualTypeArguments()[0];
745
+ dputs "found it at "
746
+ dp parameterizedType, interfaceType, itemType
747
+ dp itemType.bounds
748
+ break;
749
+ end
750
+ end
751
+ end
752
+
753
+ if (itemType != nil)
754
+ break;
755
+ end
756
+
757
+ parentType = classType.generic_superclass();
758
+ end
759
+
760
+ if (itemType != nil && itemType.is_a?(java.lang.reflect.TypeVariable))
761
+ dputs 'aww shucks'
762
+ dp itemType
763
+ itemType = Java::java.lang.Object.java_class;
764
+ end
765
+
766
+ return itemType;
767
+ end
768
+
769
+ # /**
770
+ # * Determines the type of a map value.
771
+ # *
772
+ # * @param mapType
773
+ # */
774
+ def self.getGenericMapValueType( mapType)
775
+ valueType = nil;
776
+
777
+ parentType = mapType;
778
+ while (parentType != nil)
779
+ if (parentType.is_a? ParameterizedType)
780
+ parameterizedType = parentType;
781
+ rawType = parameterizedType.getRawType();
782
+
783
+ if (java.util.Map.java_class.assignable_from?(rawType))
784
+ valueType = parameterizedType.getActualTypeArguments()[1];
785
+ end
786
+
787
+ break;
788
+ end
789
+
790
+ classType = parentType;
791
+ genericInterfaces = classType.getGenericInterfaces();
792
+
793
+ genericInterfaces.each do |genericInterface|
794
+
795
+ if (genericInterface.is_a? ParameterizedType)
796
+ parameterizedType = genericInterface;
797
+ interfaceType = parameterizedType.getRawType();
798
+
799
+ if (java.util.Map.java_class.assignable_from?(interfaceType))
800
+ valueType = parameterizedType.getActualTypeArguments()[1];
801
+ break;
802
+ end
803
+ end
804
+ end
805
+
806
+ if (valueType != nil)
807
+ break;
808
+ end
809
+
810
+ parentType = classType.getGenericSuperclass();
811
+ end
812
+
813
+ if valueType != nil && valueType.is_a?(TypeVariable)
814
+ valueType = Java::java.lang.Object.java_class;
815
+ end
816
+
817
+ return valueType;
818
+ end
819
+
820
+ # /**
821
+ # * Returns the value of a named constant.
822
+ # *
823
+ # * @param type
824
+ # * The type that defines the constant.
825
+ # *
826
+ # * @param name
827
+ # * The name of the constant.
828
+ # */
829
+ def self.getConstantValue( type, name)
830
+ if (type == nil)
831
+ raise IllegalArgumentException.new();
832
+ end
833
+
834
+ if (name == nil)
835
+ raise IllegalArgumentException.new();
836
+ end
837
+
838
+ field = nil
839
+ begin
840
+ field = FieldUtil.getField(type, name);
841
+ rescue NoSuchFieldException => exception
842
+ raise IllegalArgumentException.new(exception);
843
+ end
844
+
845
+ int fieldModifiers = field.modifiers();
846
+ if ((fieldModifiers & Modifier.STATIC) == 0 || (fieldModifiers & Modifier.FINAL) == 0)
847
+ raise IllegalArgumentException.new("Field is not a constant.");
848
+ end
849
+
850
+ value = nil
851
+ begin
852
+ value = field.get(nil);
853
+ rescue IllegalAccessException => exception
854
+ raise IllegalArgumentException.new(exception);
855
+ end
856
+
857
+ return value;
858
+ end
859
+
860
+ def self.getStaticGetterMethod( sourceType, key, targetType)
861
+ if (sourceType == nil)
862
+ raise ArgumentError.new();
863
+ end
864
+
865
+ if (key == nil)
866
+ raise ArgumentError.new();
867
+ end
868
+
869
+ method = nil;
870
+
871
+ if (targetType != nil)
872
+ key = key[0].upcase + key[1..-1];
873
+
874
+ getMethodName = GET_PREFIX + key;
875
+ isMethodName = IS_PREFIX + key;
876
+
877
+ begin
878
+ method = MethodUtil.getMethod(sourceType, getMethodName, [ targetType ]);
879
+ rescue NoSuchMethodException => exception
880
+ # No-op
881
+ end
882
+
883
+ if (method == nil)
884
+ begin
885
+ method = MethodUtil.getMethod(sourceType, isMethodName, [targetType ]);
886
+ rescue NoSuchMethodException => exception
887
+ # No-op
888
+ end
889
+ end
890
+
891
+ # Check for interfaces
892
+ if (method == nil)
893
+ interfaces = targetType.interfaces();
894
+ interfaces.length.times do |i|
895
+ begin
896
+ method = MethodUtil.getMethod(sourceType, getMethodName, [ interfaces[i] ]);
897
+ rescue NoSuchMethodException => exception
898
+ # No-op
899
+ end
900
+
901
+ if (method == nil)
902
+ begin
903
+ method = MethodUtil.getMethod(sourceType, isMethodName, [interfaces[i]] );
904
+ rescue NoSuchMethodException => exception
905
+ # No-op
906
+ end
907
+ end
908
+
909
+ if (method != nil)
910
+ break;
911
+ end
912
+ end
913
+ end
914
+
915
+ if (method == nil)
916
+ method = getStaticGetterMethod(sourceType, key, targetType.superclass());
917
+ end
918
+ end
919
+
920
+ return method;
921
+ end
922
+
923
+ def self.getStaticSetterMethod( sourceType, key,
924
+ valueType, targetType, rubify=false)
925
+ if (sourceType == nil)
926
+ raise "NULL PTR"
927
+ end
928
+
929
+ if (key == nil)
930
+ raise "NULL PTR"
931
+ end
932
+
933
+ if (valueType == nil)
934
+ dputs caller
935
+ dp sourceType, key, valueType, targetType, rubify
936
+ raise "NULL PTR"
937
+ end
938
+
939
+ method = nil;
940
+
941
+ if (targetType != nil)
942
+ key = key[0].upcase + key[1..-1];
943
+
944
+ setMethodName = SET_PREFIX + key;
945
+ begin
946
+ unless rubify
947
+ method = MethodUtil.getMethod(sourceType, setMethodName,[ targetType, valueType ]);
948
+ else
949
+ method = sourceType.ruby_class.method(setMethodName)
950
+ end
951
+ rescue NoSuchMethodException => exception
952
+ # No-op
953
+ end
954
+
955
+ # Check for interfaces
956
+ if (method == nil)
957
+ interfaces = targetType.interfaces();
958
+ interfaces.length.times do |i|
959
+ begin
960
+ method = MethodUtil.getMethod(sourceType, setMethodName, [ interfaces[i], valueType ]);
961
+ rescue NoSuchMethodException => exception
962
+ # No-op
963
+ end
964
+
965
+ if (method != nil)
966
+ break;
967
+ end
968
+ end
969
+ end
970
+
971
+ if (method == nil)
972
+ method = getStaticSetterMethod(sourceType, key, valueType, targetType.superclass());
973
+ end
974
+ end
975
+
976
+ return method;
977
+ end
978
+
979
+ def self.toAllCaps(value)
980
+ if (value == nil)
981
+
982
+ raise "NULL PTR"
983
+ end
984
+
985
+ allCapsBuilder = Java.java.lang.StringBuilder.new();
986
+
987
+ value.length.times do |i|
988
+ c = value[(i)];
989
+
990
+ if (c.upcase == c)
991
+ allCapsBuilder.append('_');
992
+ end
993
+
994
+ allCapsBuilder.append(c.upcase);
995
+ end
996
+
997
+ return allCapsBuilder.toString();
998
+ end
999
+
1000
+ def self.for(names)
1001
+ if names.is_a? java.lang.Object
1002
+ RubyWrapperBeanAdapter.new(names)
1003
+ else
1004
+ RubyObjectWrapperBeanAdapter.new(names)
1005
+ end
1006
+ end
1007
+ end