|
Since I've worked on the problem of capturing method/field references for a while, I thought I'd share some recent things with this thread.
Capturing method references has always been straightforward by creating a proxy and intercepting the invocations. Capturing field references though requires examining some bytecode, and this is something that can be done well enough so long as the field reference lives in a controlled area, such as a class that exists solely for the purpose of configuring properties (which is a safe assumption for Hibernate). So we should be able to do something like:
new ConfigurationMapping<Person>() { public void configure() { addConstraintTo(property.firstName, new NotNullDef()); }
}
Or something like:
ConfigurationMapping<Person> mapping = (Mapper mapper, Person person) -> mapper.addConstraintTo(person.firstName, new NotNullDef());
I'm using this approach successfully in ModelMapper to capture object to object field/method mappings. An ASM ClassVisitor inspects the bytecode and captures the field/method references:
https://github.com/jhalterman/modelmapper/blob/master/core/src/main/java/org/modelmapper/internal/ExplicitMappingBuilder.java#L214
This works well so long as the bytecode is consistent, but special support is needed for unusual bytecode such as Groovy properties and of course Android's Dalvik.
Hope this helps.
|