From 991ce7dbc68db3039cf2905a295d7aa823e4467c Mon Sep 17 00:00:00 2001 From: Nick Sieger Date: Tue, 30 Sep 2008 12:08:33 -0500 Subject: [PATCH] [#1144]: Provide alternate implementation of Object#subclasses_of for JRuby --- .../active_support/core_ext/object/extending.rb | 43 +++++++++++++++----- 1 files changed, 32 insertions(+), 11 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb index 082e98a..bbf6f85 100644 --- a/activesupport/lib/active_support/core_ext/object/extending.rb +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -3,22 +3,43 @@ class Object Class.remove_class(*subclasses_of(*superclasses)) end - # Exclude this class unless it's a subclass of our supers and is defined. - # We check defined? in case we find a removed class that has yet to be - # garbage collected. This also fails for anonymous classes -- please - # submit a patch if you have a workaround. - def subclasses_of(*superclasses) #:nodoc: - subclasses = [] - - superclasses.each do |sup| - ObjectSpace.each_object(class << sup; self; end) do |k| - if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) - subclasses << k + begin + ObjectSpace.each_object(Class.new) {} + + # Exclude this class unless it's a subclass of our supers and is defined. + # We check defined? in case we find a removed class that has yet to be + # garbage collected. This also fails for anonymous classes -- please + # submit a patch if you have a workaround. + def subclasses_of(*superclasses) #:nodoc: + subclasses = [] + + superclasses.each do |sup| + ObjectSpace.each_object(class << sup; self; end) do |k| + if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) + subclasses << k + end end end + + subclasses end + rescue RuntimeError + # JRuby and any implementations which cannot handle the objectspace traversal + # above fall back to this implementation + def subclasses_of(*superclasses) #:nodoc: + subclasses = [] - subclasses + superclasses.each do |sup| + ObjectSpace.each_object(Class) do |k| + if superclasses.any? { |superclass| k < superclass } && + (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) + subclasses << k + end + end + subclasses.uniq! + end + subclasses + end end def extended_by #:nodoc: -- 1.5.6