简体   繁体   中英

doubts related to plugin development for eclipse

Very new to eclipse plugin development. I have converted jar project into eclipse plugin. 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. Just like calling a method from an included 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. In terms of the New Plug-in wizard, yours "doesn't contribute to the UI". Technically, it means that it doesn't need 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. 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. 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).

I think this is the situation you are describing...

You have a plugin that you want Eclipse Java (JDT) users to install. In their Java projects, you want them to be able to use some of the Java classes in your plugin.

In Java, a class has to be found on a classpath by a class loader. JDT manages the classpath for projects through "class path containers." The first example of this is when you create a Java project, JDT will add "JRE System Library" as a container. You can see it under the project in the Package Explorer.

Another example of this is the JUnit plugin. 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. (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.

Yet another example: PDE expands on what JDT does. 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.

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. To do that, in your plugin:

  1. Add a dependency on JDT Core
  2. Extend from this extension point: 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
  2. Extend from this extension point: org.eclipse.jdt.ui.classpathContainerPage

Some plugins use the wizard page to customize the container (JUnit allows picking JUnit 3 or 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.

Here is a good article: Simplify Eclipse classpaths using classpath containers

To answer your questions:

  1. You have to add the classes to the classpath using the initialize method of your subclass of ClasspathContainerInitializer .
  2. Yes, methods that you want clients to call must be public and be members of the classes you add to the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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