# Getting the Right Java Constructor

**URL:** <https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823>\
**Category:** JRuby\
**Created:** [May 9, 2013, 2:14am UTC](https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823 "2013-05-09T02:14:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [May 9, 2013, 2:14am UTC](https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823/1 "2013-05-09T02:14:00Z")

</div>

All -

I need to get the right constructor overload. For some reason,  
iterating  
over all the constructors and matching the parameter types works, but  
using  
the ‘constructor’ method does not:

```
# java_message_class = 

```

java.lang.Class.forName(‘org.xbill.DNS.Message’)  
byte\_array\_class = java.lang.Class.forName(’[B’)

# constructor = java\_message\_class.constructor(byte\_array\_class)

```
# p
# p byte_array_class
# p
# p "Constructors:"
constructor = Message.java_class.constructors.detect do |c|
  c.parameter_types == [byte_array_class]
end
```

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [May 9, 2013, 3:31am UTC](https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823/2 "2013-05-09T03:31:07Z")

</div>

I wonder a bit why you’re going down this road. I think  
reflection-heavy  
code is probably better left to Java code that you load within JRuby,  
rather than doing Java reflection directly in Ruby code.

But, if you’re set on your path, I believe you simply mistyped the  
method  
name. You’re looking for getConstructor (or get\_constructor).

Assuming you have the following class on your classpath:

public class Message {  
public Message(byte[] bytes) {  
}  
}

Then this works fine:

irb(main):001:0\> import ‘Message’  
=\> [Java::Default::Message]  
irb(main):002:0\> m\_class = java.lang.Class.for\_name(“Message”)  
=\> class Message  
irb(main):003:0\> b\_class = java.lang.Class.for\_name("[B") =\> class [B irb(main):004:0\> m\_class.get\_constructor(b\_class) =\> public Message(byte[])

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [May 10, 2013, 8:34am UTC](https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823/3 "2013-05-10T08:34:09Z")

</div>

Interesting.

And yet this works using a more

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [May 10, 2013, 6:28pm UTC](https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823/4 "2013-05-10T18:28:20Z")

</div>

Anthony and Sebastien -

Thanks for your help.

Anthony, using ‘constructor’ as the method name worked (try it if you  
don’t  
believe me). JRuby aliases Java accessors and mutators not only in the  
usual way (that is, in snake case), but also in conventional Ruby  
notation  
(like attr\_accessor would).

- Keith

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [May 10, 2013, 8:01pm UTC](https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823/5 "2013-05-10T20:01:48Z")

</div>

The constructor method is there, I agree, but I don’t believe for the  
reason you think.

I thought you were looking at the raw Java Class. The constructor method  
you’re referring to is provided by JavaClass, the JRuby proxy for a raw  
Java Class object. And it expects another (proxy) JavaClass as an  
argument. Using java.lang.Class.for\_name returns an unproxied Java  
Class.

I believe dropping the get only works for JavaBean property (no arg)  
getter  
methods.

---

<div class="post-metadata">

**Author:** ![islepilot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac8455/32.png) [@islepilot](https://www.ruby-forum.com/u/islepilot)\
**Post date:** [May 10, 2013, 9:40pm UTC](https://www.ruby-forum.com/t/getting-the-right-java-constructor/228823/6 "2013-05-10T21:40:33Z")

</div>

Guys -

You were right and I was wrong. First of all, this:

```
  Message.java_class.declared_constructor([].to_java(:byte).java_class)

```

…worked perfectly. Secondly, I forgot that the attr\_accessor type  
aliases  
only apply to beanlike signatures.

Thanks so much for the corrections. Everything’s working great now.

- Keith
