Permalink
Browse files

Provide alternate implementation of Object#subclasses_of for JRuby

Signed-off-by: Michael Koziarski <michael@koziarski.com>

[#1144 state:committed]
  • Loading branch information...
1 parent 8d337e9 commit 395369bc2b85346e8f196f202941e5015dc1481e @nicksieger nicksieger committed with NZKoz Sep 30, 2008
Showing with 33 additions and 12 deletions.
  1. +33 −12 activesupport/lib/active_support/core_ext/object/extending.rb
@@ -3,22 +3,43 @@ def remove_subclasses_of(*superclasses) #:nodoc:
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:

0 comments on commit 395369b

Please sign in to comment.