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
(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…
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?
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™ 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?
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 :