Discussion:
[jruby-user] Differences Between MyClass.class and getClass() in JRuby
s shaw
2014-07-08 00:19:42 UTC
Permalink
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...
s shaw
2014-07-08 00:26:05 UTC
Permalink
(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...
Eric West
2014-07-08 02:51:05 UTC
Permalink
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?
​
Post by s shaw
(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...
Eric West
2014-07-08 02:51:50 UTC
Permalink
...or perhaps the development version JRuby9000 ? I mean
Post by Eric West
I’m getting the expected result. Using Rsense <http://rsense.github.io/>
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?
​
Post by s shaw
(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...
s shaw
2014-07-08 04:03:47 UTC
Permalink
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
Karol Bucek
2014-07-09 14:09:18 UTC
Permalink
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
Post by s shaw
Post by Eric West
I’m getting the expected result.
That's because inspect is calling toString() which outputs "class ...".
~ >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
s shaw
2014-07-09 23:30:24 UTC
Permalink
*> 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?
Post by Karol Bucek
p.s. questions such as these might be beneficial for others, maybe next
time try asking
Post by Karol Bucek
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.
Post by Karol Bucek
reflection is a bit "non-trivial" but there's some logic involved (it is
mostly due supporting the Ruby side of Java).
* 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
Post by s shaw
I'm getting the expected result.
That's because inspect is calling toString() which outputs "class ...".
~ >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
Loading...