I am working with BeanBinding a lot in my current project and so I have code that looks like...
TypeA objA;
TypeB objB;
Bindings.createAutoBinding(UpdateStrategy.READ,
objA, BeanProperty.create("X"),
objB, BeanProperty.create("X"))
.bind();
Where objA
and objB
are instances of classes that have a setX()
method. The problem comes in that if I refactor setX
to setY
then I need to hunt down these string property names. I realize I can create static final strings for property name but if I can get the compiler to do the work for me, all the better.
Ideally, what I would like to be able to do is...
TypeA obja;
TypeB objB;
Bindings.createAutoBinding(UpdateStrategy.READ,
objA, BeanProperty.create( Magic.returnBeanName(TypeA.class).getX() ),
objB, BeanProperty.create( Magic.returnBeanName(TypeB.class).setX() )
.bind();
It would seem this could be possible via some code synthesis and/or aspects.
A complete shot in the dark, but maybe returnBeanName
can use javassist to create a different class similar to the bean, except that it modifies the return types of the getters to String and returns the property name?
For example, if your bean looks like this:
public class Foo{
private int x;
public int getX(){
return x;
}
public void setX(int x){
this.x= x;
}
}
Then dynamically create a different class that looks like this:
public class FooMeta{
public String getX(){
return "x";
}
}
Seem kind of crazy, but sounds fun to write.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With