I want to have one feature of Java 8 for Java 7: automatic interface implementation generation for method (to avoid performace deficiency due to reflection calls). I know that Java 8 provide the generation at compile time but I think it's not possible for Java 7 (without maintainance of metadata files). So I agree with implementation generation at runtime.
Example:
I have following interface:
public interface Extractor<E> {
public Object getProperty(E aSourceObject);
}
And a bean class (or interface)
public class Foo {
public int getProperty1();
public String getProperty2();
public boolean getProperty3();
}
I need for each property of Foo an implementation of Extractor interface. Something like Foo::getProperty1
for Java 8.
public class Foo1Extractor implements Extractor<Foo> {
public Object getProperty(Foo anObject) {
return anObject.getProperty1();
}
}
Should I use JavaCompiler
(currently I have few interfaces to implement and can work with template classes) or you have better solutions?
The main requirement is the short bytecode generation time and LGPL compatibility (can be used in commercial product).
If possible, provide a little example for my case.