7
7
8
8
import org .jruby .Ruby ;
9
9
import org .jruby .RubyArray ;
10
+ import org .jruby .RubyBasicObject ;
10
11
import org .jruby .RubyBignum ;
11
12
import org .jruby .RubyBoolean ;
12
13
import org .jruby .RubyClass ;
15
16
import org .jruby .RubyHash ;
16
17
import org .jruby .RubyNumeric ;
17
18
import org .jruby .RubyString ;
19
+ import org .jruby .runtime .ClassIndex ;
18
20
import org .jruby .runtime .ThreadContext ;
19
21
import org .jruby .runtime .builtin .IRubyObject ;
20
22
import org .jruby .util .ByteList ;
@@ -57,6 +59,19 @@ private Generator() {
57
59
return handler .generateNew (session , object );
58
60
}
59
61
62
+ // NOTE: drop this once Ruby 1.9.3 support is gone!
63
+ private static final int FIXNUM = 1 ;
64
+ private static final int BIGNUM = 2 ;
65
+ private static final int ARRAY = 3 ;
66
+ private static final int STRING = 4 ;
67
+ private static final int NIL = 5 ;
68
+ private static final int TRUE = 6 ;
69
+ private static final int FALSE = 7 ;
70
+ private static final int HASH = 10 ;
71
+ private static final int FLOAT = 11 ;
72
+ // hard-coded due JRuby 1.7 compatibility
73
+ // https://github.com/jruby/jruby/blob/1.7.27/core/src/main/java/org/jruby/runtime/ClassIndex.java
74
+
60
75
/**
61
76
* Returns the best serialization handler for the given object.
62
77
*/
@@ -65,16 +80,24 @@ private Generator() {
65
80
@ SuppressWarnings ("unchecked" )
66
81
private static <T extends IRubyObject >
67
82
Handler <? super T > getHandlerFor (Ruby runtime , T object ) {
68
- RubyClass metaClass = object .getMetaClass ();
69
- if (metaClass == runtime .getString ()) return (Handler )STRING_HANDLER ;
70
- if (metaClass == runtime .getFixnum ()) return (Handler )FIXNUM_HANDLER ;
71
- if (metaClass == runtime .getHash ()) return (Handler )HASH_HANDLER ;
72
- if (metaClass == runtime .getArray ()) return (Handler )ARRAY_HANDLER ;
73
- if (object .isNil ()) return (Handler )NIL_HANDLER ;
74
- if (object == runtime .getTrue ()) return (Handler )TRUE_HANDLER ;
75
- if (object == runtime .getFalse ()) return (Handler )FALSE_HANDLER ;
76
- if (metaClass == runtime .getFloat ()) return (Handler )FLOAT_HANDLER ;
77
- if (metaClass == runtime .getBignum ()) return (Handler )BIGNUM_HANDLER ;
83
+ switch (((RubyBasicObject ) object ).getNativeTypeIndex ()) {
84
+ // can not use getNativeClassIndex due 1.7 compatibility
85
+ case NIL : return (Handler ) NIL_HANDLER ;
86
+ case TRUE : return (Handler ) TRUE_HANDLER ;
87
+ case FALSE : return (Handler ) FALSE_HANDLER ;
88
+ case FLOAT : return (Handler ) FLOAT_HANDLER ;
89
+ case FIXNUM : return (Handler ) FIXNUM_HANDLER ;
90
+ case BIGNUM : return (Handler ) BIGNUM_HANDLER ;
91
+ case STRING :
92
+ if (((RubyBasicObject ) object ).getMetaClass () != runtime .getString ()) break ;
93
+ return (Handler ) STRING_HANDLER ;
94
+ case ARRAY :
95
+ if (((RubyBasicObject ) object ).getMetaClass () != runtime .getArray ()) break ;
96
+ return (Handler ) ARRAY_HANDLER ;
97
+ case HASH :
98
+ if (((RubyBasicObject ) object ).getMetaClass () != runtime .getHash ()) break ;
99
+ return (Handler ) HASH_HANDLER ;
100
+ }
78
101
return GENERIC_HANDLER ;
79
102
}
80
103
0 commit comments