Posts tagged reflection
Using Java Reflection API to clean up code and speed up development
11 year ago
The challenge was a simple one: my method is passed in an object. This object may or may not have a certain method – called getClientListeners(). When it does, the method should be invoked and the result from the call must be returned by my method. If the object does not expose such a getClientListeners() method, my method should return null.
Sounds simple enough. However because the objects do not implement a common interface that specifies the method getClientListeners(), I found myself writing repeated if-clauses, for all the known Class that could be passed in to my method:
if (uiComponent instanceof RichInputText) { RichInputText uixComponent = (RichInputText)uiComponent; cls = uixComponent.getClientListeners(); if (cls == null) { cls = new ClientListenerSet(); uixComponent.setClientListeners(cls); } }And that for a seriously large number of classes.
Then it hit me:
Recent Comments