在没有父类体的情况下用Java编译插件代码

发布于 2024-11-08 17:24:15 字数 174 浏览 3 评论 0原文

我正在尝试向我的应用程序添加插件系统。我有一个插件必须扩展的抽象类。这个类提供了有用的方法,所以我真的需要它。问题是这些插件可以由任何人编写,所以我认为他们需要抽象类代码才能编译他们的代码。

我不想让创建插件的过程变得复杂。有没有一种方法可以在不知道抽象类主体(仅其方法)的情况下编译代码?

提前致谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

热风软妹 2024-11-15 17:24:15

有没有一种方法可以在不知道抽象类主体(仅其方法)的情况下编译代码?

没有。为了编译声明的类,

class A extends B

您必须在源路径中具有源格式的 B 或在类路径中具有 .class 格式。

(如果只知道方法就足以编写插件,那么听起来更像是在追求接口而不是抽象类。)

我不想让创建插件的过程变得复杂。

提供 B 的已编译 .class 文件完全不复杂,而且可能是最好的在这个场景中进行练习。

实际上,通过包含相关类和接口的 .jar 文件获得 API 可能是标准。

需要明确的是:

  1. 获取与插件开发相关的类,编译它们并将它们放入,例如 pluginapi.jar
  2. 分发 .jar 并告诉插件开发人员他们的插件应该编译,前提是pluginapi.jar
  3. 要求插件开发人员为您提供 plugin.jar (不一定包括pluginapi 类)
  4. 例如,在您的应用程序中,确保存在插件 API 类通过将它们包含在类路径中。
  5. 加载插件类。

Is there a way to compile the code without know the abstract class body (only its methods)?

No. In order to to compile a class declared as

class A extends B

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.)

I don´t want to complicate the process of creating a plugin.

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:

  1. Take the classes that are relevant for plugin-development, compile them, and put them in, say pluginapi.jar
  2. Distribute the .jar and tell plugin developers that their plugins should compile, provided the pluginapi.jar
  3. Ask the plugindevelopers to provide you with plugin.jar (not necessarily including pluginapi classes)
  4. In your application, make sure that the plugin API classes are present, for instance by including them the class path.
  5. Load the plugin classes.
生生不灭 2024-11-15 17:24:15

可以要求您的插件作者为您提供一些具有某些指定方法的类,并使用反射调用这些类。这意味着他们可以编写一个无需访问您的任何代码即可编译的插件。

但是这也会严重限制它们的可能性:如果插件无法调用它,那么插件应该如何与您的系统交互?由于提供这一点的唯一明智的方法是使某些类(或接口)可访问,因此您也可以提供它们需要实现/扩展的接口(或抽象类)。

您可以将该接口(以及插件可见的所有接口/类)放在单独的 .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.

你是我的挚爱i 2024-11-15 17:24:15

您只需提供包含所有必需的 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.

剪不断理还乱 2024-11-15 17:24:15

或者,您可以不强迫用户扩展类或实现接口,而是让他们提供具有特定签名的函数——然后您可以通过反射调用该函数。在这种情况下,他们不需要您提供任何东西,但是,如果他们的函数错误,显然您将无法调用该插件。

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.

只是在用心讲痛 2024-11-15 17:24:15

在这些情况下,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文