Forum: JRuby Example of become java! class later instantiated on Java side

Posted by Patrick Mahoney (Guest)
on 2013-02-05 16:47
(Received via mailing list)
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 Mahoney
Posted by kristian (Guest)
on 2013-02-05 17:55
(Received via mailing list)
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<String>

- Christian
Posted by Patrick Mahoney (Guest)
on 2013-02-05 19:20
(Received via mailing list)
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 Mahoney
Posted by Keith B. (keith_b)
on 2013-02-05 21:58
(Received via mailing list)
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/generi... .)

- Keith
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.