Differences Between MyClass.class and getClass() in JRuby

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…

(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…

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?

…or perhaps the development version JRuby9000 ? I mean

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 !!:blush:
ruby -v /tmp/reflect.rb
jruby 1.7.12 (1.9.3p392) 2014-06-20 643e292 on Java HotSpot™ 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

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 Newest 'jruby' Questions - Stack Overflow

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 B. [email protected]:

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 Newest 'jruby' Questions - Stack Overflow