1 min read

JRuby Complex Classes in Java Method Signatures

As documented in the JRuby wiki, java_signature changes a method’s signature to match the signature string passed to it:
Observe that the classes in the method signature are primitive. What if we use a complex class as a parameter type?

Running the above code will give you the following NoMethodError message:

The way I went about using complex classes in signatures is to utilise add_method_signature instead of java_signature:

add_method_signature expects the first argument to be the name of the method that will have its signature changed. For the second argument, it expects it to be a list of classes. The list’s first item is the return class (e.g., void) while the subsequent items are the signature’s parameter classes (e.g., int and MyClass). Note that I invoke become_java! on the complex class. This tells MyClass to materialize itself into a Java class. The false flag is needed so that JRuby’s main class loader is used to load the class. Without it, you’ll be greeted by a java.lang.ClassNotFoundException.

comments powered by Disqus