Skip to content
Advertisement

Is there a general “back-end” library for Java reflection

I’m currently working with a specialized, interpreted, programming language implemented in Java. As a very small part of the language, I’d like to add the ability to make calls into Java. Before I dive into all of the nitty-gritty of reflection, I was wondering if anyone knew of a general library for doing the “back-end” part of invoking Java code reflectively.

That is, I parse a string (I define the grammar) into some data structure that represents a Java method call (or constructor, or field access) and then pass that data structure to this library that invokes the call and returns the result. In particular, I’d like it to already handle all the edge cases that I don’t want to figure out:

  • Automagically pick the right method based on the types of arguments (like an intelligent Class.getDeclaredMethod())
  • Handle distinction between arrays and normal object references
  • etc

I’ve spent a little time looking at the implementations of dynamic languages on the JVM, but these are generally much more complicated than I’m looking for, or highly optimized for the particular language.

Another option is to convert my grammar into a string in some dynamic language and invoke it with Rhino or something, but that’s a little more overhead than I’m looking for.

Advertisement

Answer

Just a comment to your own answer; actually beanutils has support for getting “a close match” given a set of parameters. See getMatchingAccessibleMethod()

BeanUtils is really powerful and has lots of utility methods for inspecting classes. The same support is naturally available for constructors.

Advertisement