[jruby-user] Weirdness in Finding the Right Constructor

3 views
Skip to first unread message

Keith Bennett

unread,
May 8, 2013, 8:17:33 PM5/8/13
to us...@jruby.codehaus.org
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

Michael Hall

unread,
May 8, 2013, 8:24:08 PM5/8/13
to us...@jruby.codehaus.org
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



AppConverter convert Apple jvm to openjdk apps http://www195.pair.com/mik3hall/index.html#appconverter





Keith Bennett

unread,
May 10, 2013, 12:29:17 PM5/10/13
to us...@jruby.codehaus.org
Michael -

The signature for the 'constructor' method is:

public Method getMethod(String name,
                        Class<?>... parameterTypes)
                 throws NoSuchMethodException,
                        SecurityException

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


Keith Bennett

unread,
May 10, 2013, 3:31:07 PM5/10/13
to us...@jruby.codehaus.org
Sorry, I meant to include the *right* Javadoc:

public Constructor<T> getConstructor(Class<?>... parameterTypes)
                              throws NoSuchMethodException,
                                     SecurityException

- Keith

Reply all
Reply to author
Forward
0 new messages