Forum: JRuby Differences Between MyClass.class and getClass() in JRuby

48e8a52db22ee2eeabb994dced226773?d=identicon&s=25 s shaw (Guest)
on 2014-07-08 02:20
(Received via mailing list)
I
n Java


My
Class.class and (new
My
Class()).
getC
lass
()
return the same thin
g. In JRuby the equivalent calls would be
Java::
My
Class
.java_class and Java::MyClass.new.get_class right? But these calls
return
Java::JavaClass
and
 Java::JavaLang::Class
 respectively.

Java::JavaClass
 seems like a java.lang.Class: it has
annotation_present
() and encosing_class() methods. But if you call declared_fields() it
returns an array of
Java::JavaFiel
d, which does not have java.lang.Field methods like
generic_type
().

How is one supposed to get a true instance of
java.lang.Class

, one that returns
java.lang.Field
 when declared_fields() is called?

It appears that
one must first instantiate
the given class
 and then call get_class
; o

f course this does not work for abstract classes...
48e8a52db22ee2eeabb994dced226773?d=identicon&s=25 s shaw (Guest)
on 2014-07-08 02:26
(Received via mailing list)
(Original message was badly mangled, so resending)

In Java MyClass.class and (new MyClass()).getClass() return the same
thing.
In JRuby the equivalent calls would be Java:: MyClass.java_class and
Java::MyClass.new.get_class right? But these calls return
Java::JavaClass
and Java::JavaLang::Class respectively.

Java::JavaClass seems like a java.lang.Class: it has
annotation_present()
and encosing_class() methods. But if you call declared_fields() it
returns
an array of Java::JavaField, which does not have java.lang.Field methods
like generic_type().

How is one supposed to get a true instance of java.lang.Class , one that
returns java.lang.Field when declared_fields() is called? It appears
that
one must first instantiate the given class and then call get_class; of
course this does not work for abstract classes...
C983ad33b47479f17a28e083e1bb3d8b?d=identicon&s=25 Eric West (edubkendo)
on 2014-07-08 04:52
(Received via mailing list)
I’m getting the expected result. Using Rsense <http://rsense.github.io/>
as
an example:

require "rsense-core"
Java::org.cx4a.rsense.typing::Graph.java_class#=> class
org.cx4a.rsense.typing.Graph

graph = Java::org.cx4a.rsense.typing::Graph.new#=>
#<Java::OrgCx4aRsenseTyping::Graph:0x6d79d6d1>

graph.java_class#=> class org.cx4a.rsense.typing.Graph

graph.get_class#=> class org.cx4a.rsense.typing.Graph

Can you share some example code that produces the result your getting
above? Also, I’m running this on JRuby 1.7.12 on Java 7, is it possible
you
are running an older version of JRuby, or perhaps the development
version,
JRuby?
​
C983ad33b47479f17a28e083e1bb3d8b?d=identicon&s=25 Eric West (edubkendo)
on 2014-07-08 04:53
(Received via mailing list)
...or perhaps the development version JRuby9000 ? I mean
48e8a52db22ee2eeabb994dced226773?d=identicon&s=25 s shaw (Guest)
on 2014-07-08 06:04
(Received via mailing list)
> I'm getting the expected result.

That's because inspect is calling toString() which outputs "class ...".
If you look at the ruby class you can see the difference:

~ >cat /tmp/reflect.rb
klass = java.lang.String
p klass.new.get_class.class
p klass.java_class.class
p klass.new.get_class.declared_fields[0].generic_type
p klass.java_class.declared_fields[0].generic_type
~ >ruby -v !!:$
ruby -v /tmp/reflect.rb
jruby 1.7.12 (1.9.3p392) 2014-06-20 643e292 on Java HotSpot(TM) 64-Bit
Server VM 1.6.0_65-b14-462-11M4609 [darwin-x86_64]
Java::JavaLang::Class
Java::JavaClass
class [C
NoMethodError: undefined method `generic_type' for private final char[]
java.lang.String.value:Java::JavaField
  (root) at /tmp/reflect.rb:5
Caa2df9372ffa0a9e95b2bab1e8fea34?d=identicon&s=25 Karol Bucek (Guest)
on 2014-07-09 16:10
(Received via mailing list)
reflection is a bit "non-trivial" but there's some logic involved (it is
mostly due supporting the Ruby side of Java).
what you're trying to achieve is "best" done using :

*    klass.java_class.to_java.getDeclaredFields[0].generic_type*

you got the *klass.java_class *right but *declared_fiels* is still a
helper
from JRuby and returns a

*Java::JavaField*
p.s. questions such as these might be beneficial for others, maybe next
time try asking at http://stackoverflow.com/questions/tagged/jruby
48e8a52db22ee2eeabb994dced226773?d=identicon&s=25 s shaw (Guest)
on 2014-07-10 01:31
(Received via mailing list)
> what you're trying to achieve is "best" done using:
*>     klass.java_class.to_java.getDeclaredFields[0].generic_type*

Ah yes, great. I'm familiar with to_java(), but thought that in this
case
it was redundant. So java_class() returns the JRuby "java class" while
to_java() gives you the actual java class?

> p.s. questions such as these might be beneficial for others, maybe next
time try asking
> at http://stackoverflow.com/questions/tagged/jruby

I don't use StackOverflow much and http://jruby.org/community didn't
mention it.

Thanks for your help.


2014-07-09 10:09 GMT-04:00 Karol Bucek <self@kares.org>:
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.