Weirdness in Finding the Right Constructor

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

On May 8, 2013, at 7:17 PM, Keith B. 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 H.

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

Michael -

The signature for the ‘constructor’ method is:

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

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

Sorry, I meant to include the right Javadoc:

public Constructor
http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Constructor.html<T
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html>
getConstructor(Class
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html<?>…
parameterTypes)
throws NoSuchMethodException
http://docs.oracle.com/javase/6/docs/api/java/lang/NoSuchMethodException.html,
SecurityException
http://docs.oracle.com/javase/6/docs/api/java/lang/SecurityException.html

  • Keith