Package com.gargoylesoftware.base.gui
Class ReflectedAction
java.lang.Object
javax.swing.AbstractAction
com.gargoylesoftware.base.gui.ReflectedAction
- All Implemented Interfaces:
ActionListener
,Serializable
,Cloneable
,EventListener
,Action
A swing "action" that uses reflection to find and invoke a given target
method.
ReflectedAction is a Swing "action" that uses reflection to call a specific target method when the action is fired. Actions are often implemented using inner classes which can be very inefficient in terms of performance and deployment size. This action is a tiny bit slower when it is actually fired but is smaller and generally faster otherwise due to the fact that those inner classes are no longer needed.
Action searchAction = new ReflectedAction(this, "performSearch");
- Version:
- $Revision: 1.4 $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String
private final Object
private static final long
Fields inherited from class javax.swing.AbstractAction
changeSupport, enabled
Fields inherited from interface javax.swing.Action
ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, DISPLAYED_MNEMONIC_INDEX_KEY, LARGE_ICON_KEY, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SELECTED_KEY, SHORT_DESCRIPTION, SMALL_ICON
-
Constructor Summary
ConstructorsConstructorDescriptionReflectedAction
(Object object, String methodName) Create a reflected action with an object and the name of the method that will be invoked on that object when the action is fired. -
Method Summary
Modifier and TypeMethodDescriptionvoid
actionPerformed
(ActionEvent event) The action has been fired so now we use reflection to invoke the method that was specified previously.protected final void
assertNotNull
(String fieldName, Object object) Throw an exception if the specified object is nullprotected void
exceptionThrown
(Exception exception) Callback that will be invoked if an exception is thrown during the reflected method call.private Method
findMethodOnObject
(Object object, String methodName) Return the Method object that corresponds to the specified method name.Methods inherited from class javax.swing.AbstractAction
addPropertyChangeListener, clone, firePropertyChange, getKeys, getPropertyChangeListeners, getValue, isEnabled, putValue, removePropertyChangeListener, setEnabled
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
object_
-
methodName_
-
-
Constructor Details
-
ReflectedAction
Create a reflected action with an object and the name of the method that will be invoked on that object when the action is fired.- Parameters:
object
- The object upon which we will invoke a method when the action is firedmethodName
- The name of the method that we will invoke. This method must take either no parameters or one parameter which is an ActionEvent.- Throws:
IllegalArgumentException
- if there are no matching methods.
-
-
Method Details
-
actionPerformed
The action has been fired so now we use reflection to invoke the method that was specified previously.- Parameters:
event
- The ActionEvent
-
findMethodOnObject
Return the Method object that corresponds to the specified method name. The method may take either no arguments or may take an ActionEvent.- Parameters:
object
- The object that contains the methodmethodName
- The name of the method that we're looking for- Returns:
- The method that matches the specified name.
- Throws:
IllegalArgumentException
- If there aren't any methods that match.SecurityException
- If we do not have access to this method.
-
exceptionThrown
Callback that will be invoked if an exception is thrown during the reflected method call.Override this to provide custom error handling. Default behaviour is to print a stack trace.
- Parameters:
exception
- The exception
-
assertNotNull
Throw an exception if the specified object is null- Parameters:
fieldName
- The name of the paremeter we are checkingobject
- The value of the parameter we are checking
-