Example of become java! class later instantiated on Java side

I’m trying to use become_java! to generate a class that some Java code
will
later load with Class.forName or equivalent. Is this possible? Is there
an
example somewhere? The Ruby class must implement a Java interface.

This is my attempt: https://gist.github.com/4686262

The Java side is able to create an instance of my become_java! ruby
class.
However, when it calls the interface method on that object, I get the
error:

Observable.java:142:in `notifyObservers’:
java.lang.AbstractMethodError:
rubyobj.MyObserver.update(Ljava/util/Observable;Ljava/lang/Object;)V

Should this be possible? And if so, what am I missing?


Patrick M.

without

include Java::JavaUtil::Observer

your example works. I am not too deep into this, but it feels like
type checks do not take place i.e. in JRuby you can add any object to
an instance of java.util.List

  • Christian

On 05.02.2013 10:52, kristian wrote:

without

include Java::JavaUtil::Observer

your example works. I am not too deep into this, but it feels like
type checks do not take place i.e. in JRuby you can add any object to
an instance of java.util.List

Fantastic! Thank you. I did not even think to try it that way.


Patrick M.

Kristian -

You’re right that in code you can do this in JRuby but not in Java. To
refine the point, Java’s restriction is at compile time; there are no
checks at runtime. The “generic” typing system used by the Java
collections is something that is used by the Java compiler to check
accesses, but the type information does not make it into the runtime.
This removal is called type erasure, and is a surprise to most people
when they first encounter it. (See
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html .)

  • Keith

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs