Forum: JRuby Weirdness in Finding the Right Constructor

486ca04f06d968004643ce5b47376ded?d=identicon&s=25 Keith B. (keith_b)
on 2013-05-09 02:18
(Received via mailing list)
All -

I'm trying to get a particular constructor overload in order to call it.
 The one I want takes a byte [] as its sole parameter.

For some reason, the 'constructor' method doesn't find it, but iterating
over the list returned by 'constructors' and checking the params myself
works:


    byte_array_class = java.lang.Class.forName('[B')

    # This doesn't find the constructor:
    constructor = Message.java_class.constructor(byte_array_class)

    # But this does!:
    constructor = Message.java_class.constructors.detect do |c|
      c.parameter_types == [byte_array_class]
    end


Anyone have any idea why the first approach doesn't work?

- Keith
6621e2fb37ab6f7c3a2b53103f83b41a?d=identicon&s=25 Michael Hall (Guest)
on 2013-05-09 02:25
(Received via mailing list)
On May 8, 2013, at 7:17 PM, Keith Bennett wrote:

>    c.parameter_types == [byte_array_class]
>     end
>
>
> Anyone have any idea why the first approach doesn't work?


It's not a Class[] like the second?
If I remember the reflection api's right a array of parameter types is
necessary.
I'm guessing that's what whatever this is ends up using.

Michael Hall

trz nio.2 for OS X http://www195.pair.com/mik3hall/index.html#trz

HalfPipe Java 6/7 shell app
http://www195.pair.com/mik3hall/index.html#halfpipe

AppConverter convert Apple jvm to openjdk apps
http://www195.pair.com/mik3hall/index.html#appconverter
486ca04f06d968004643ce5b47376ded?d=identicon&s=25 Keith B. (keith_b)
on 2013-05-10 18:30
(Received via mailing list)
Michael -

The signature for the 'constructor' method is:

public Method
<http://docs.oracle.com/javase/6/docs/api/java/lang...
*getMethod*(String
<http://docs.oracle.com/javase/6/docs/api/java/lang... name,
                        Class
<http://docs.oracle.com/javase/6/docs/api/java/lang......
parameterTypes)
                 throws NoSuchMethodException
<http://docs.oracle.com/javase/6/docs/api/java/lang...,
                        SecurityException
<http://docs.oracle.com/javase/6/docs/api/java/lang...


The 3 dots means that you can list the parameter types as separate
arguments, and inside getMethod they can be accessed as a single
array. That's why I'm thinking it should work -- and Java didn't
complain that the argument list was incorrect.

- Keith
486ca04f06d968004643ce5b47376ded?d=identicon&s=25 Keith B. (keith_b)
on 2013-05-10 21:32
(Received via mailing list)
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.