I don't really like building up a string and evaluating it, but by using the bindings suggested by Alex, I avoid most of the pitfalls around escaping. There's obviously a bit more to it, but this is the basic idea. Use bindings to avoid having to escape arguments Private void buildArgs(Object args, StringBuilder exp) Throw new RhsFunctionException(e.getMessage(), e) StringBuilder exp = new StringBuilder("o." + methodName) Private Object invoke(Object object, String methodName, Object args) Here's the basic idea: private ScriptEngine engine =. So, since this is a minor and experimental feature, I've chosen an expeditious solution using the scripting engine support (JavaScript, in particular) in Java 1.6. Obviously, scripting languages built on the JVM solve this problem, but in a much more complicated way than I need due to language-specific optimizations. If a method takes a float and I have a double, it should be smart enough to not reject that method because the signature doesn't match exactly. Namely, given a method name and list of arguments, pick the method that best "fits" the arguments. FEST looks really cool and I've bookmarked it for further study, but like BeanUtils, it doesn't appear to solve what I consider to be the difficult problem here. BeanUtils helps a lot for beans, but I don't want to work solely with Beans. So if it doesn't have the exact method lookup strategy you want, hopefully it is easy to add it. It is designed to be quite extensible, so you can plugin in strategies to find the methods you want in a loosely coupled (compositional) style.
#Java download reflections code
I am using it in some production code at them moment. But it is developed to quite a high quality standard (I hope) and it is opensource so you can basically use it as it is now if it has all the features you need (you just might have to adjust your code a bit if you want to use newer versions that are released). It will probably take a while to get to a really feature stable point (like a year or two), as I am only working on it occasionally. It has some basic features missing at the moment, like access to fields, but it works for methods. Object(subject).methods(annotatedWith(PostConstruct.class)), You can write code like this which calls all the post construct annotated methods in a class: forEach(
Which integrates with hamcrest and lambdaj I have started to create a library -reflection:fluent-reflection 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.Īnother 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.
In particular, I'd like it to already handle all the edge cases that I don't want to figure out: 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. 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.
As a very small part of the language, I'd like to add the ability to make calls into Java. I'm currently working with a specialized, interpreted, programming language implemented in Java.