There’s the following class:
public abstract class AbstractWriter<T extends BE> {
protected final T be;
// Constructor, some methods
public static interface Setter {
void setNewValue();
}
protected <S> void setValue(final Class<S> clazz, final S oldValue,
final S newValue, final Setter setter) {
// Do something
setter.setNewValue();
// Do something
}
}
Then there’s PersonWriter, which extends AbstractWriter and looks currently like this:
public class PersonWriter extends AbstractWriter<BEPerson> {
public PersonWriter(BEPerson be) {
super(be);
}
public void setName(String oldValue, final String newValue) {
setValue(String.class, oldValue, newValue, new Setter() {
@Override
public void setNewValue() {
be.setName(newValue);
}
});
};
}
But I want setName to look like this:
public void setName(String oldValue, String newValue) {
setValue(String.class, oldValue, newValue, new Setter() {
@Override
public void setNewValue(String newValue) {
be.setName(newValue);
}
});
};
How do I have to modify AbstractWriter, to make it work (if it’s even possible)?
You seem to be going a rather long way to call a setter function on a field ;-). How is the
new Setter()in thesetValuecall you want to have supposed to know whennewValueis aStringand when it is something else?With a type parameter for setter, I think it should not be difficult: