简体   繁体   English

与eclipse的插件开发有关的疑问

[英]doubts related to plugin development for eclipse

Very new to eclipse plugin development. eclipse插件开发很新。 I have converted jar project into eclipse plugin. 我已经将jar项目转换为eclipse插件。 But I really dont know, how to make use of it. 但我真的不知道,如何利用它。 Some basic doubts, 一些基本的疑惑,

  1. How to call a method available in plugin in our program?? 如何在我们的程序中调用插件中可用的方法?
  2. Should every exposed method should be public, in order to use it in our program?? 每个暴露的方法都应该是公开的,以便在我们的程序中使用它吗?

My idea is something like a plugin to sum two numbers. 我的想法就像一个插入两个数字的插件。 And user installs the plugin and call add(x,y) method in this plugin. 用户安装插件并在此插件中调用add(x,y)方法。 Just like calling a method from an included jar. 就像从包含jar中调用方法一样。

There are many tutorials explaining how to create a plugin, but i didn't found how to use the same. 有许多教程解释如何创建插件,但我没有找到如何使用相同的。

What you are describing is a plain OSGi bundle, with no Eclipse-specific features. 您所描述的是一个简单的OSGi包,没有特定于Eclipse的功能。 In terms of the New Plug-in wizard, yours "doesn't contribute to the UI". 就新插件向导而言,您的“无助于UI”。 Technically, it means that it doesn't need plugin.xml . 从技术上讲,这意味着它不需要plugin.xml

The way your outside code perceives the bundle is just as if it was a regular jar: you can access its classes, instantiate them, and call their methods. 外部代码感知bundle的方式就像它是一个普通的jar:你可以访问它的类,实例化它们,并调用它们的方法。 Or you can call static methods, just like you are used to. 或者你可以调用静态方法,就像你习惯的那样。

The additional layer provided by OSGi means you can identify which Java packages your bundle exports to its users. OSGi提供的附加层意味着您可以识别捆绑包导出到其用户的Java包。 Therefore a class which is public, but doesn't reside in an exported package, is not accessible to other bundles (this applies only to the strict mode, however; otherwise you only get an Access Restriction warning). 因此,其他bundle无法访问一个公共类但不驻留在导出包中的类(但这仅适用于严格模式;否则您只会获得访问限制警告)。

I think this is the situation you are describing... 我认为这是你所描述的情况......

You have a plugin that you want Eclipse Java (JDT) users to install. 您有一个希望Eclipse Java(JDT)用户安装的插件。 In their Java projects, you want them to be able to use some of the Java classes in your plugin. 在他们的Java项目中,您希望他们能够使用插件中的一些Java类。

In Java, a class has to be found on a classpath by a class loader. 在Java中,必须通过类加载器在类路径中找到类。 JDT manages the classpath for projects through "class path containers." JDT通过“类路径容器”管理项目的类路径。 The first example of this is when you create a Java project, JDT will add "JRE System Library" as a container. 第一个例子是当你创建一个Java项目时,JDT会将“JRE System Library”添加为容器。 You can see it under the project in the Package Explorer. 您可以在Package Explorer中的项目下查看它。

Another example of this is the JUnit plugin. 另一个例子是JUnit插件。 You'll notice that when you add a JUnit Test Case to JDT project the first time, a dialog will ask about adding the JUnit library to the build path. 您会注意到,当您第一次向JDT项目添加JUnit测试用例时,会出现一个对话框,询问是否将JUnit库添加到构建路径。 (This is an explicit behavior of the JUnit plugin's New File Wizard.) If you agree, you'll see the "JUnit 4" container in the Package Explorer. (这是JUnit插件的新文件向导的显式行为。)如果您同意,您将在Package Explorer中看到“JUnit 4”容器。

Yet another example: PDE expands on what JDT does. 又一个例子:PDE扩展了JDT的功能。 When you create a Plugin project, PDE adds a "Plug-in Dependencies" container that it manages based on the plugin dependencies you declare in the plugin manifest. 当您创建插件项目时,PDE会根据您在插件清单中声明的​​插件依赖项添加一个“插件依赖项”容器。

Users can create and reference their own classpath containers for their favorite libraries. 用户可以为自己喜欢的库创建和引用自己的类路径容器。

But, of course, as a library provider, you want to give them one like the JUnit plugin does. 但是,当然,作为一个库提供者,你想给它们一个像JUnit插件那样的。 To do that, in your plugin: 为此,在您的插件中:

  1. Add a dependency on JDT Core 添加对JDT Core的依赖
  2. Extend from this extension point: org.eclipse.jdt.core.classpathContainerInitializer 从此扩展点扩展:org.eclipse.jdt.core.classpathContainerInitializer

If you want a wizard page to create or edit a classpath container entry: 如果需要向导页面来创建或编辑类路径容器条目:

  1. Add a dependency on JDT UI 在JDT UI上添加依赖项
  2. Extend from this extension point: org.eclipse.jdt.ui.classpathContainerPage 从此扩展点扩展: org.eclipse.jdt.ui.classpathContainerPage

Some plugins use the wizard page to customize the container (JUnit allows picking JUnit 3 or 4); 一些插件使用向导页面来自定义容器(JUnit允许选择JUnit 3或4); Others just use the page to provide information about the container. 其他人只是使用该页面来提供有关容器的信息。

See the JDT documentation topic Setting the Java build path and cross-reference the source code of any examples that familiar to you. 请参阅JDT文档主题设置Java构建路径并交叉引用您熟悉的任何示例的源代码。

Here is a good article: Simplify Eclipse classpaths using classpath containers 这是一篇很好的文章: 使用类路径容器简化Eclipse类路径

To answer your questions: 回答你的问题:

  1. You have to add the classes to the classpath using the initialize method of your subclass of ClasspathContainerInitializer . 您必须使用ClasspathContainerInitializer的子类的initialize方法将类添加到类ClasspathContainerInitializer
  2. Yes, methods that you want clients to call must be public and be members of the classes you add to the classpath. 是的,您希望客户端调用的方法必须是公共的,并且是您添加到类路径的类的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM