com.pietschy.command.reflection
Class ReflectionCommand

java.lang.Object
  extended by com.pietschy.command.Command
      extended by com.pietschy.command.ActionCommand
          extended by com.pietschy.command.reflection.ReflectionCommand
All Implemented Interfaces:
ActionCommandExecutor, Bindable, Identifiable

public class ReflectionCommand
extends ActionCommand

An ActionCommand that invokes a method on a target object using reflection.

As an alternative you can also consider using the @ActionCommand and @SwingWorkerCommand annotations in conjuction with GuiCommands.bindAnnotatedCommands(CommandContainer, Object) to achive a similar outcome.

Version:
$Revision: 1.3 $
Author:
andrewp

Field Summary
 
Fields inherited from class com.pietschy.command.ActionCommand
PARAMETER_ACTION_EVENT, PARAMETER_INVOKER, PARAMETER_INVOKER_WINDOW, PARAMETER_MODIFIERS
 
Fields inherited from class com.pietschy.command.Command
internalLog, PROPERTY_ENABLED, PROPERTY_VISIBLE
 
Constructor Summary
ReflectionCommand(java.lang.String id, java.lang.Class clazz, java.lang.String methodName, java.lang.Class... methodArgs)
          Creates a new instance that invokes a static class method.
ReflectionCommand(java.lang.String id, java.lang.Object target, java.lang.reflect.Method method)
          Creates a new instance that invokes the specified method on the specified target object.
ReflectionCommand(java.lang.String id, java.lang.Object target, java.lang.reflect.Method method, ArgumentProvider argumentProvider)
          Creates a new instance that invokes the specified method on the specified target object.
ReflectionCommand(java.lang.String id, java.lang.Object target, java.lang.String methodName, java.lang.Class... methodArgs)
          Creates a new reflection command that invokes a specified method on the specified target.
ReflectionCommand(java.lang.String id, java.lang.String className, java.lang.String methodName, java.lang.Class... methodArgs)
          Creates a new reflection command that invokes a static method.
 
Method Summary
 ArgumentProvider getArgumentProvider()
          Sets the current ArgumentProvider for this command.
 ExceptionHandler getExceptionHandler()
          Gets the current exception handler for this command.
protected  java.lang.Object[] getInvocationArgs()
          Gets any arguments required by the delegate.
 java.lang.reflect.Method getMethod()
          Gets the method this command will invoke.
 java.lang.Object getTarget()
          Gets the object instance that will recieve the method call.
protected  void handleExecute()
          Invokes the commands method using reflection.
protected  void handleInvocationException(java.lang.Exception e)
          Called to handle any exceptions that occur during the invocation of the delegate method.
 void setArgumentProvider(ArgumentProvider argumentProvider)
          Sets the current ArgumentProvider for this command.
 void setExceptionHandler(ExceptionHandler exceptionHandler)
          Sets the current exception handler for this command.
 
Methods inherited from class com.pietschy.command.ActionCommand
addActionCommandListener, addInterceptor, bindToContainer, createButton, createButton, createButton, createButton, createMenuItem, createMenuItem, createMenuItem, createMenuItem, doButtonAttachments, doButtonDetachments, execute, execute, getActionAdapter, getActionAdapter, getActionEvent, getInvoker, getInvokerWindow, getModifiers, getParameter, getParameter, getParameters, installShortCut, installShortCut, postExecute, preExecute, putParameter, putParameters, removeActionCommandListener, removeInterceptor, requestDefaultIn, unbindFromContainer, uninstallShortCut, uninstallShortCut
 
Methods inherited from class com.pietschy.command.Command
addContainerPropertyListener, addFace, addHoverListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, attach, bind, bind, bind, buttonIterator, configureAppearance, configureAppearances, configureButtonAppearances, configureButtonStates, configureButtonStates, createButton, createButton, createButton, createButton, createComponent, createMenuItem, createMenuItem, createMenuItem, createMenuItem, detach, faceExists, findBestFace, fireHoverEnded, fireHoverStarted, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlternativeFaceContexts, getButtonFactory, getButtonIn, getCommandContainer, getContainerProperty, getDefaultFace, getDefaultFace, getFace, getFace, getId, getMenuFactory, getProperty, getProperty, getPropertyNames, getToolbarFactory, isAnonymous, isAttachedTo, isBound, isEnabled, isVisible, loadConfiguration, notifyCommandContainerHierarchyChanged, putProperties, putProperty, rebind, removeContainerPropertyListener, removeHoverListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, requestFocusIn, setButtonFactory, setEnabled, setMenuFactory, setToolbarFactory, setVisible, toString, unbind, validateId
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.pietschy.command.ActionCommandExecutor
addPropertyChangeListener, addPropertyChangeListener, isEnabled, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from interface com.pietschy.command.Identifiable
getId
 

Constructor Detail

ReflectionCommand

public ReflectionCommand(java.lang.String id,
                         java.lang.Object target,
                         java.lang.String methodName,
                         java.lang.Class... methodArgs)
                  throws java.lang.NoSuchMethodException
Creates a new reflection command that invokes a specified method on the specified target. You can optionally specifiy the argument list.

Parameters:
id - the command id.
target - the target object.
methodName - the method on the target.
methodArgs - the arguments of the method. This is a var-args parameter that my be empty if the method takes no arguments. If arguments are specified an ArgumentProvider must be specified using setArgumentProvider(ArgumentProvider).
Throws:
java.lang.NoSuchMethodException - if the method doesn't exist
java.lang.NullPointerException - if either the instance or methodName is null.
See Also:
setArgumentProvider(ArgumentProvider)

ReflectionCommand

public ReflectionCommand(java.lang.String id,
                         java.lang.String className,
                         java.lang.String methodName,
                         java.lang.Class... methodArgs)
                  throws java.lang.NoSuchMethodException,
                         java.lang.ClassNotFoundException
Creates a new reflection command that invokes a static method. The classloader defined by Defaults.getClassLoader() is used to load the class.

Parameters:
id - the id of the command.
className - the class on which the static method resides
methodName - the name of the no arg static method.
methodArgs - the arguments of the method. This is a var-args parameter that my be empty if the method takes no arguments. If arguments are specified an ArgumentProvider must be specified using setArgumentProvider(ArgumentProvider).
Throws:
java.lang.NoSuchMethodException - if the method doesn't exist
java.lang.NullPointerException - if either the instance or methodName is null.
java.lang.ClassNotFoundException

ReflectionCommand

public ReflectionCommand(java.lang.String id,
                         java.lang.Class clazz,
                         java.lang.String methodName,
                         java.lang.Class... methodArgs)
                  throws java.lang.NoSuchMethodException
Creates a new instance that invokes a static class method.

Parameters:
id - the id of the command.
clazz - the class on which the static method resides.
methodName - the method name.
methodArgs - the arguments of the method. This is a var-args parameter that my be empty if the method takes no arguments. If arguments are specified an ArgumentProvider must be specified using setArgumentProvider(ArgumentProvider).
Throws:
java.lang.NoSuchMethodException - if the method doesn't exist
java.lang.NullPointerException - if either the instance or methodName is null.

ReflectionCommand

public ReflectionCommand(java.lang.String id,
                         java.lang.Object target,
                         java.lang.reflect.Method method)
Creates a new instance that invokes the specified method on the specified target object.

Parameters:
id - the command id.
target - the target object.
method - the target method.

ReflectionCommand

public ReflectionCommand(java.lang.String id,
                         java.lang.Object target,
                         java.lang.reflect.Method method,
                         ArgumentProvider argumentProvider)
Creates a new instance that invokes the specified method on the specified target object. The ArgumentProvider is used to generate the arguments for the method call.

Parameters:
id - the command id.
target - the target object.
method - the target method.
Method Detail

handleExecute

protected final void handleExecute()
Invokes the commands method using reflection.

Specified by:
handleExecute in class ActionCommand

getInvocationArgs

protected java.lang.Object[] getInvocationArgs()
Gets any arguments required by the delegate. This method delegates to the current ArgumentProvider. The method will return null if the provider is null.

Returns:
the arguments required for the delegate method. Returns null by default.
See Also:
setArgumentProvider(ArgumentProvider)

handleInvocationException

protected void handleInvocationException(java.lang.Exception e)
Called to handle any exceptions that occur during the invocation of the delegate method. This method will delegate to the current ExceptionHandler. If the handler is null a RuntimeException is thrown.

Parameters:
e - the exception that occured while calling method.invoke(..)
See Also:
setExceptionHandler(ExceptionHandler)

getMethod

public java.lang.reflect.Method getMethod()
Gets the method this command will invoke.

Returns:
the method this command will invoke.

getTarget

public java.lang.Object getTarget()
Gets the object instance that will recieve the method call. This will be null for static methods.

Returns:
the object instance that will recieve the method call.

getArgumentProvider

public ArgumentProvider getArgumentProvider()
Sets the current ArgumentProvider for this command. May be null if no arguments are used.

Returns:
the current ArgumentProvider for this command, or null if none has been specified.

setArgumentProvider

public void setArgumentProvider(ArgumentProvider argumentProvider)
Sets the current ArgumentProvider for this command.

Parameters:
argumentProvider - the ArgumentProvider to use to obtain the arguments for the method invocation.

getExceptionHandler

public ExceptionHandler getExceptionHandler()
Gets the current exception handler for this command.

Returns:
the current exception handler for this command.

setExceptionHandler

public void setExceptionHandler(ExceptionHandler exceptionHandler)
Sets the current exception handler for this command.

Parameters:
exceptionHandler - the exception handler for this command.


Copyright © 2006 - 2007 Andrew Pietsch