Hi there,
When I run the following:
module Ex7
class Task
include java.lang.Runnable
def run
puts "A Java interface method"
end
end
def self.test
puts Task.ancestors.join(", ")
task2 = java.lang.Object.new
class << task2
include java.lang.Runnable
def run
puts "A Java interface method - with anonymous class definition"
end
end
puts task2.class.ancestors.join(", ")
end
end
Ex7::test
I get the following =>
Ex7::Task, Java::JavaLang::Runnable, JavaProxyMethods, Object, Kernel
Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods,
Object, Kernel
Shouldn’t the second line include the “Java::JavaLang::Runnable”?
Thanks,
-Z
Hmm…does this actually work? I don’t believe it’s possible to << a
Java object and then add interfaces after the fact. What you’re seeing
is perhaps it failing to add Runnable because it can’t implement the
interface?
On Wed, Dec 23, 2009 at 3:35 PM, Zemian D. [email protected]
wrote:
  end
  end
  http://xircles.codehaus.org/manage_email
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Hi Charles,
The sample I gave compiles and runs if that what your definition of
“actually work” is.
What I observed is little inconsistency. The output of first line is
“Ex7::Task, Java::JavaLang::Runnable, JavaProxyMethods, Object, Kernel”,
which is from a full Ruby class that included a java interface in it.
But when creating a anonymous class that modify an existing object, the
included java interface does not show, as it display here:
“Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods,
Object, Kernel”
What’s more strange is “task2.kind_of? java.lang.Runnable” will return
true,
but yet I can’t pass this task2 object into a java method that accepts a
Runnable parameter (like Thread.new for example).
-Z
–
View this message in context:
http://old.nabble.com/Anonymous-object's-ancestors-doesn't-list-java-interfaces--tp26907736p26910636.html
Sent from the JRuby - User mailing list archive at Nabble.com.
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
BTW, you can do this even more simply using JRuby’s auto-magic
block–>interface coercion:
t = java.lang.Thread.new { puts “foo” }
t.start
-Bill
You’re adding the interface to the object’s singleton class. This isn’t
currently supported, but there’s no inconsistency. When you write:
task2.class.ancestors
you are requesting the ancestors of the main class, in this case
java.lang.Object. To obtain the ancestors of the singleton class, you
would
write:
(class<<task2;self;end).ancestors
which in this case will include java.lang.Runnable.
To create an anonymous Java class, create an anonymous class using
Class.new(java_superclass). Assuming you only need one instance, the
code
might look something like this:
t = java.lang.Thread.new(
Class.new(java.lang.Object) {
include java.lang.Runnable
def run
puts “hi, mom”
end
}.new #=> instance created right here
)
t.start
-Bill
Hi Bill, Thanks for the tips, that worked.
-Z