|
|
||
Evan Summers's BlogExplicit ReflectionPosted by evanx on May 04, 2006 at 04:33 AM | Comments (2)
public class MyPanel extends JPanel {
JButton myButton = new JButton();
public void myButtonActionPerformed(ActionEvent event) {
}
protected void configure() {
configure(myButton, MyPanelInfo.myButton.getName());
addActionHandler(myButton, MyPanelInfo.myButtonActionPerformed);
}
}
Where MyPanelInfo magically reflects MyPanel explicitly, as if manually coded as follows.
public final class MyPanelInfo {
public static final Field myButton = getField(...);
public static final Method myButtonActionPerformed = getMethod(...);
}
The "explicit reflection" would promote tooling/refactability by avoiding strings as in getMethod("myButtonActionPerformed") and configure(myButton, "myButton"). Using strings to refer to instances and methods is "fragile" eg. not "refactoring-safe" (or "exception-safe") and is not readily "toolable" in the sense that it does not enjoy auto-completion and error-highlighting in IDE's. An alternative to the above that would still work, is to introduce a language notation such as MyPanel:myButton, which to the compiler is just the string literal "myButton", but is understood by the IDE to be the name of the myButton instance, for the purposes of refactoring eg. renaming myButton to something else.
But having refactoring capabilities doesn't help if our code is not safely refactorable. And if we use strings to refer fields and methods, then those fields and methods are not safely refactorable. And if our code is not refactorable, then we can't be agile. In a future blog, i will extend this argument to Java persistence (as in Hibernate, JDO, EJB3), and make an argument for "native queries," which might enable refactoring. CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|