/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2012 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.module;

import imagej.util.ClassUtils;
import imagej.util.Log;

import java.lang.reflect.Method;

/**
 * A reference to a {@link Method}, which can be invoked at will.
 * 
 * @author Curtis Rueden
 */
public class MethodRef {

	private final Method method;
	private final String label;

	public MethodRef(final String className, final String methodName,
		final Class<?>... params)
	{
		method = findMethod(className, methodName, params);
		label = method == null ? null : makeLabel(className, methodName);
	}

	public void execute(final Object obj, final Object... args) {
		if (method == null) return;
		Log.debug("Executing method: " + label);
		try {
			method.invoke(obj, args);
		}
		catch (final Exception e) {
			// NB: Several types of exceptions; simpler to handle them all the same.
			Log.warn("Error executing method: " + label, e);
		}
	}

	private Method findMethod(final String className, final String methodName,
		final Class<?>... params)
	{
		if (methodName == null || methodName.isEmpty()) return null;
		final Class<?> c = ClassUtils.loadClass(className);
		if (c == null) return null;
		try {
			// TODO - support inherited methods
			final Method m = c.getDeclaredMethod(methodName, params);
			m.setAccessible(true);
			return m;
		}
		catch (final Exception e) {
			// NB: Multiple types of exceptions; simpler to handle them all the same.
			Log.warn("Cannot find method: " + makeLabel(c.getName(), methodName), e);
		}
		return null;
	}

	private String makeLabel(final String className, final String methodName) {
		return className + "#" + methodName;
	}
}

@@ -2861,22 +2861,16 @@
 null;%0A%09%09
-final 
 Class%3C?%3E
@@ -2870,17 +2870,25 @@
 lass%3C?%3E 
-c
+baseClass
  = Class
@@ -2921,36 +2921,76 @@
 ;%0A%09%09
-if (c == null) return null;%0A
+for (Class%3C?%3E c = baseClass; c != null; c = c.getSuperclass())%0A%09%09%7B%0A%09
 %09%09tr
@@ -2996,16 +2996,17 @@
 ry %7B%0A%09%09%09
+%09
 // TODO 
@@ -3033,16 +3033,17 @@
 methods%0A
+%09
 %09%09%09final
@@ -3098,16 +3098,17 @@
 ms);%0A%09%09%09
+%09
 m.setAcc
@@ -3122,16 +3122,17 @@
 (true);%0A
+%09
 %09%09%09retur
@@ -3134,26 +3134,28 @@
 return m;%0A%09%09
+%09
 %7D%0A
+%09
 %09%09catch (fin
@@ -3149,32 +3149,44 @@
 %09%09%09catch (final 
+NoSuchMethod
 Exception e) %7B%0A%09
@@ -3177,32 +3177,140 @@
 Exception e) %7B%0A%09
+%09%09%09// ... continue to loop into super class methods%0A%09%09%09%7D%0A%09%09%09catch (final Exception e) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09// NB: Multipl
@@ -3363,33 +3363,32 @@
 all the same.%0A%09%09
-%09
 Log.warn(%22Cannot
@@ -3393,16 +3393,25 @@
 ot find 
+suitable 
 method: 
@@ -3413,17 +3413,21 @@
 hod: %22 +
- 
+%0A%09%09%09%09
 makeLabe
@@ -3428,17 +3428,25 @@
 keLabel(
-c
+baseClass
 .getName
@@ -3460,25 +3460,18 @@
 hodName)
-, e);%0A%09%09%7D
+);
 %0A%09%09retur

Change MethodRef::findMethod()