[Japi-cvs] SF.net SVN: japi: [256] libs/swing-action/trunk/src/net/sf/japi/swing/ ReflectionAction.
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-12-06 22:00:06
|
Revision: 256 http://svn.sourceforge.net/japi/?rev=256&view=rev Author: christianhujer Date: 2006-12-06 14:00:00 -0800 (Wed, 06 Dec 2006) Log Message: ----------- Improved argument passing mechanism. Modified Paths: -------------- libs/swing-action/trunk/src/net/sf/japi/swing/ReflectionAction.java Modified: libs/swing-action/trunk/src/net/sf/japi/swing/ReflectionAction.java =================================================================== --- libs/swing-action/trunk/src/net/sf/japi/swing/ReflectionAction.java 2006-12-06 21:48:26 UTC (rev 255) +++ libs/swing-action/trunk/src/net/sf/japi/swing/ReflectionAction.java 2006-12-06 22:00:00 UTC (rev 256) @@ -48,6 +48,9 @@ * </pre> * Note that because of Reflection this Action is slightly slower than implementing your own Action instance, but in most cases this really does not matter at all. * Usually you won't use ReflectionAction yourself. Instead you'll let {@link ActionFactory} create an instance for you. + * <p /> + * You can use {@link #REFLECTION_ARGUMENTS} for providing arguments for the action method that's called via Reflection. + * Because that Object[] is not cloned, you can keep the reference and dynamically change the arguments of the invoked method. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> * @todo ReflectionAction should be able to invoke methods with parameters, three variants: Object..., ActionEvent or void */ @@ -141,7 +144,14 @@ putValue(REFLECTION_METHOD, null); } } - super.putValue(key, newValue); + if (REFLECTION_ARGUMENTS.equals(key)) { + if (newValue != null && !(newValue instanceof Object[])) { + throw new IllegalArgumentException("Value for key REFLECTION_ARGUMENTS must be of type " + Object[].class.getName() + " but was " + newValue.getClass().getName()); + } + super.putValue(key, newValue == null ? NO_ARGUMENTS : newValue); + } else { + super.putValue(key, newValue); + } } /** {@inheritDoc} @@ -158,6 +168,10 @@ if (!isEnabled()) { return; } final Object instance = getValue(REFLECTION_TARGET); try { + // TODO: Special value for REFLECTION_ARGUMENTS which determines whether to use ActionEvent. + final Method method = getMethod(instance); + final Object[] arguments = getArguments(method, e); + method.invoke(instance, arguments); getMethod(instance).invoke(instance, (Object[]) getValue(REFLECTION_ARGUMENTS)); } catch (final IllegalAccessException ex) { throw new IllegalAccessError(ACTION_FACTORY.format("ReflectionAction.nonPublicMethod", ex)); @@ -184,6 +198,20 @@ } } + /** + * Returns the arguments suited for invoking the specified method. + * @param method Method to invoke + * @param e ActionEvent which eventually might be used as method argument. + * @return Argument array. + */ + @NotNull private Object[] getArguments(@NotNull final Method method, @NotNull final ActionEvent e) { + final Class<?>[] parameterTypes = method.getParameterTypes(); + if (parameterTypes.length == 1 && parameterTypes[0].equals(ActionEvent.class)) { + return new Object[] { e }; + } + return (Object[]) getValue(REFLECTION_ARGUMENTS); + } + /** Get the method associated with this action. * @param instance Instance to get method for * @return associated method or <code>null</code> if no method could be found This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |