在没有父类体的情况下用Java编译插件代码
我正在尝试向我的应用程序添加插件系统。我有一个插件必须扩展的抽象类。这个类提供了有用的方法,所以我真的需要它。问题是这些插件可以由任何人编写,所以我认为他们需要抽象类代码才能编译他们的代码。
我不想让创建插件的过程变得复杂。有没有一种方法可以在不知道抽象类主体(仅其方法)的情况下编译代码?
提前致谢。
I´m trying to add a plugin system to my app. I have an abstract class that plugins must extend. This class provide usuful methods so I really need it. The problem is that these plugins could be written by anyone so I suppouse that they'll need the abstract class code to be able to compile their code.
I don´t want to complicate the process of creating a plugin. Is there a way to compile the code without know the abstract class body (only its methods)?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有。为了编译声明的类,
您必须在源路径中具有源格式的
B
或在类路径中具有 .class 格式。(如果只知道方法就足以编写插件,那么听起来更像是在追求接口而不是抽象类。)
提供
B
的已编译 .class 文件完全不复杂,而且可能是最好的在这个场景中进行练习。实际上,通过包含相关类和接口的 .jar 文件获得 API 可能是标准。
需要明确的是:
pluginapi.jar
pluginapi.jar
plugin.jar
(不一定包括pluginapi 类)No. In order to to compile a class declared as
you'll have to have
B
in source format in the source path or in .class format on the class path.(If knowing only the methods is sufficient for writing the plugin, it sounds more like you're after an interface than an abstract class.)
Providing the compiled .class file of
B
is completely uncomplicated and probably the best practice in this scenario.Actually, having an API at hand through a .jar-file containing the relevant classes and interfaces is probably the standard.
To be clear:
pluginapi.jar
pluginapi.jar
plugin.jar
(not necessarily including pluginapi classes)您可以要求您的插件作者为您提供一些具有某些指定方法的类,并使用反射调用这些类。这意味着他们可以编写一个无需访问您的任何代码即可编译的插件。
但是这也会严重限制它们的可能性:如果插件无法调用它,那么插件应该如何与您的系统交互?由于提供这一点的唯一明智的方法是使某些类(或接口)可访问,因此您也可以提供它们需要实现/扩展的接口(或抽象类)。
您可以将该接口(以及插件可见的所有接口/类)放在单独的 .jar 文件中。这样他们只需要该 jar 文件来编译插件。
You could ask your plugin authors to provide you some classes with some specified methods and invoke those using reflection. This would mean that they could write a plugin that can be compiled without access to any of your code.
But it would also severely limit their possibilities: How should the plugin interact with your system if they have no way of calling into it? Since the only sane way to provide that is to make some classes (or interfaces) accessible, you can just as well provide an interface (or abstract class) that they need to implement/extend.
You could put that interface (and all interfaces/classes visible to plugins) in a separate .jar file. This way they only need that jar file to compile a plugin.
您只需提供包含所有必需的 java 文件的 jar 文件即可。但请记住,一旦发布了 api,就应该非常小心地更改它。
You can just provide a jar file with all necessry java files. But remember, that once you publish the api, you should be very careful with changing it.
或者,您可以不强迫用户扩展类或实现接口,而是让他们提供具有特定签名的函数——然后您可以通过反射调用该函数。在这种情况下,他们不需要您提供任何东西,但是,如果他们的函数错误,显然您将无法调用该插件。
Alternatively you can go around by not forcing your users to extend a class or implement an interface but have them provide a function with a certain signature -- which then you can call via reflection. In this case they won't need anything from you, however, if they get the function wrong obviously you won't be able to call the plugin.
在这些情况下,Java 脚本可能非常有用。使用 Groovy 编写插件,代码可以轻松(下载)加载并在框架中执行,如下所示 http://groovy.codehaus.org/Embedding+Groovy
In these cases Java scripting can be very useful. Have your plugins written in Groovy and the codes can be easily (down)loaded and executed in your framework, something like this http://groovy.codehaus.org/Embedding+Groovy