/*
 * #%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<?> baseClass = ClassUtils.loadClass(className);
		for (Class<?> c = baseClass; c != null; c = c.getSuperclass()) {
			try {
				final Method m = c.getDeclaredMethod(methodName, params);
				m.setAccessible(true);
				return m;
			}
			catch (final NoSuchMethodException e) {
				// ... continue to loop into super class methods
			}
			catch (final Exception e) {
				break;
			}
		}
		// NB: Multiple types of exceptions; simpler to handle them all the same.
		Log.warn("Cannot find suitable method: " +
				makeLabel(baseClass.getName(), methodName));
		return null;
	}

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

@@ -2280,16 +2280,12 @@
 ;%0A%09%09
-label = 
+if (
 meth
@@ -2298,49 +2298,90 @@
 null
- ? null : makeLabel(className,
+) label = null;%0A%09%09else label = method.getClass().getName() + %22#%22 +
  method
+.get
 Name
+(
 );%0A%09
@@ -3418,43 +3418,26 @@
  %22 +
-%0A%09%09%09%09makeLabel(baseClass.getName(),
+ className + %22#%22 +
  met
@@ -3444,17 +3444,16 @@
 hodName)
-)
 ;%0A%09%09retu
@@ -3469,125 +3469,6 @@
 %09%7D%0A%0A
-%09private String makeLabel(final String className, final String methodName) %7B%0A%09%09return className + %22#%22 + methodName;%0A%09%7D%0A
 %7D%0A

make label more accurate