My Own Java 7 Suggestion
I’d like to see an interface something like this:
public interface PropertyChangeSource {
void addPropertyChangeListener(PropertyChangeListener l);
void removePropertyChangeListener(PropertyChangeListener l);
}
I’m not sure if this would also have these additional methods, or if that requires another interface:
public interface NamedPropertyChangeSource extends PropertyChangeSource {
void addPropertyChangeListener(String propertyName, PropertyChangeListener l);
void removePropertyChangeListener(String propertyName, PropertyChangeListener l);
}
Of course PropertyChangeSupport would implement this interface, as would many other beans with these methods.
Why?
For example, here is some code from Glazed Lists‘ JavaBeanEventListConnector class:
public JavaBeanEventListConnector(Class<E> beanClass) {
final Method[] methods = beanClass.getMethods();
for (int m = 0; m < methods.length; m++) {
if(methods[m].getParameterTypes().length != 1) continue;
if(methods[m].getParameterTypes()[0] != PropertyChangeListener.class) continue;
if(methods[m].getName().startsWith("add")) this.addListenerMethod = methods[m];
if(methods[m].getName().startsWith("remove"))
this.removeListenerMethod = methods[m];
}
if (this.addListenerMethod == null || this.removeListenerMethod == null)
throw new IllegalArgumentException("Couldn't find listener methods for " +
beanClass.getName());
}
Admittedly, it would be difficult to change this particular class due to backwards compatibility concerns. But I know I’ve written very similar reflection code in some of my humble little projects where I work, and I’d rather just utilize an interface.
This is much like the Closeable interface added in Java 5.
Hey Eric, I added this to my Java 7 page.