Re: [jruby-user] Re: Differences Between MyClass.class and getClass() in JRuby

s shaw <[email protected]>
Newsgroups gmane.comp.lang.jruby.user
Message-ID <CACnU8SE3evLAqj-V7XodBcaExSyr7piJ-LcVWthM6XDPpGg6qA@mail.gmail.com>
> 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 <[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 http://stackoverflow.com/questions/tagged/jruby
>
>
>
> On Tue, Jul 8, 2014 at 6:03 AM, s shaw <[email protected]> wrote:
>
>> > 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
>>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.