如何在加载插件时调用run()方法?

时间:2017-02-12 13:03:20

标签: java imagej

在ImageJ中,Interface Plugin有一个方法run(),如下所示:

package ij.plugin;

/** Plugins that acquire images or display windows should
    implement this interface. Plugins that process images 
    should implement the PlugInFilter interface. */
public interface PlugIn {

    /** This method is called when the plugin is loaded.
        'arg', which may be blank, is the argument specified
        for this plugin in IJ_Props.txt. */ 
    public void run(String arg);

}

为什么在加载插件时可以自动调用run()方法?

1 个答案:

答案 0 :(得分:3)

  

加载插件时可以自动调用run()方法吗?

没有什么是自动的。 imagej库中有一行代码表示:

thePlugIn.run(arg);

完整代码段(来自here):

/** Runs the specified plugin and returns a reference to it. */
public static Object runPlugIn(String commandName, String className, String arg) {
    if (arg==null) arg = "";
    if (IJ.debugMode)
        IJ.log("runPlugIn: "+className+argument(arg));
    // Load using custom classloader if this is a user 
    // plugin and we are not running as an applet
    if (!className.startsWith("ij.") && applet==null)
        return runUserPlugIn(commandName, className, arg, false);
    Object thePlugIn=null;
    try {
        Class c = Class.forName(className);
        thePlugIn = c.newInstance();
        if (thePlugIn instanceof PlugIn)
            ((PlugIn)thePlugIn).run(arg);
        else
            new PlugInFilterRunner(thePlugIn, commandName, arg);
    }
    catch (ClassNotFoundException e) {
        if (IJ.getApplet()==null)
            log("Plugin or class not found: \"" + className + "\"\n(" + e+")");
    }
    catch (InstantiationException e) {log("Unable to load plugin (ins)");}
    catch (IllegalAccessException e) {log("Unable to load plugin, possibly \nbecause it is not public.");}
    redirectErrorMessages = false;
    return thePlugIn;
}