[Japi-cvs] SF.net SVN: japi: [255] libs/swing-action/trunk/src/net/sf/japi/swing/ ReflectionAction.
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-12-06 21:48:35
|
Revision: 255 http://svn.sourceforge.net/japi/?rev=255&view=rev Author: christianhujer Date: 2006-12-06 13:48:26 -0800 (Wed, 06 Dec 2006) Log Message: ----------- Implemented support for ActionMethods which use different keys than their name and take arguments. 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:40:33 UTC (rev 254) +++ libs/swing-action/trunk/src/net/sf/japi/swing/ReflectionAction.java 2006-12-06 21:48:26 UTC (rev 255) @@ -53,6 +53,9 @@ */ public final class ReflectionAction extends AbstractAction { + /** The empty object array used to denote zero arguments. */ + private static final Object[] NO_ARGUMENTS = new Object[0]; + /** Action Factory for reading strings. */ private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("net.sf.japi.swing"); @@ -61,7 +64,7 @@ */ public static final String REFLECTION_TARGET = "ReflectionTarget"; - /** The key used for storing the method name to use when searching for a method using reflection. + /** The key used for storing the method key to use when searching for a method using reflection. * Value Type: {@link String} (checked). */ public static final String REFLECTION_METHOD_NAME = "ReflectionMethodName"; @@ -71,6 +74,12 @@ */ public static final String REFLECTION_METHOD = "ReflectionMethod"; + /** + * The key used for storing the method arguments to use when invoking the method. + * Value Type: {@link Object[]} (checked). + */ + public static final String REFLECTION_ARGUMENTS = "ReflectionArguments"; + /** The key used for storing the action factory that provides access to error handlers strings. */ public static final String REFLECTION_MESSAGE_PROVIDER = "ActionFactory"; @@ -80,6 +89,7 @@ /** Create an uninitialized ReflectionAction. */ public ReflectionAction() { + putValue(REFLECTION_ARGUMENTS, NO_ARGUMENTS); } /** Create a ReflectionAction with method and target. @@ -87,6 +97,7 @@ * @param target Target object to invoke method at */ public ReflectionAction(final String methodName, final Object target) { + putValue(REFLECTION_ARGUMENTS, NO_ARGUMENTS); putValue(REFLECTION_METHOD_NAME, methodName); putValue(REFLECTION_TARGET, target); } @@ -99,6 +110,7 @@ */ public ReflectionAction(final String name, final Icon icon, final String methodName, final Object target) { super(name, icon); + putValue(REFLECTION_ARGUMENTS, NO_ARGUMENTS); putValue(REFLECTION_METHOD_NAME, methodName); putValue(REFLECTION_TARGET, target); } @@ -146,7 +158,7 @@ if (!isEnabled()) { return; } final Object instance = getValue(REFLECTION_TARGET); try { - getMethod(instance).invoke(instance); + getMethod(instance).invoke(instance, (Object[]) getValue(REFLECTION_ARGUMENTS)); } catch (final IllegalAccessException ex) { throw new IllegalAccessError(ACTION_FACTORY.format("ReflectionAction.nonPublicMethod", ex)); } catch (final InvocationTargetException ex) { @@ -187,8 +199,7 @@ return null; } try { - final Class<? extends Object> clazz = instance.getClass(); - method = clazz.getMethod(methodName); + method = getActionMethod(instance, methodName); putValue(REFLECTION_METHOD, method); } catch (final NoSuchMethodException ex) { assert false : "Action Method not found: " + ex; @@ -198,6 +209,31 @@ return method; } + /** Get the named action method from the target class. + * @param clazz Class to search for method + * @param methodName Method name to get + * @return Method found + * @throws NoSuchMethodException In case the method was not found. + */ + @Nullable public static Method getActionMethod(@NotNull final Class<?> clazz, @NotNull final String methodName) throws NoSuchMethodException { + for (final Method method : clazz.getMethods()) { + if (method.getName().equals(methodName) || method.isAnnotationPresent(ActionMethod.class) && method.getAnnotation(ActionMethod.class).value().equals(methodName)) { + return method; + } + } + throw new NoSuchMethodException(methodName + " (error: no public method named " + methodName + " and annotated as @ActionMethod found)"); + } + + /** Get the named action method from the target object. + * @param target Object to search for method + * @param methodName Method name to get + * @return Method found + * @throws NoSuchMethodException In case the method was not found or is not annotated as @{@link ActionMethod}. + */ + @Nullable public static Method getActionMethod(@NotNull final Object target, @NotNull final String methodName) throws NoSuchMethodException { + return getActionMethod(target.getClass(), methodName); + } + /** {@inheritDoc} */ @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |