gpt4 book ai didi

java - 加载插件时调用 run() 方法是如何工作的?

转载 作者:行者123 更新时间:2023-12-01 20:56:59 26 4
gpt4 key购买 nike

在 ImageJ 中,接口(interface)插件有一个 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);

}

为什么Plugin加载时会自动调用run()方法?

最佳答案

the run() method can be automatically called when the Plugin is loaded?

这并不是自动的。 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;
}

关于java - 加载插件时调用 run() 方法是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42187880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com