alx
alx

Reputation: 57

Is it possible to use the same package and class name in Eclipse plugins with different ID?

I want to create a "clone" of the plug-in. I'm going to change the plug-in ID to load them simultaneously but should I change the names of the classes? Can Eclipse use a different class path for each plug-in or it just loads all classes from the bundles in the single heap?

Upvotes: 0

Views: 933

Answers (2)

Konstantin Komissarchik
Konstantin Komissarchik

Reputation: 29139

Whether or not this will work depends on factors you haven't specified in your question, so I will give you the background of how class loading works in Eclipse and you can decide.

Eclipse runs on top OSGi. Each Eclipse plugin is an OSGi bundle. Each bundle has its own classloader. This classloader sees the classes contained in the bundle as well as classes from bundle's dependencies. There are two ways for a bundle to specify a dependency. Both methods work via entries in bundle's MANIFEST.MF file. One method is to use Require-Bundle construct to depend on a whole other bundle. Using this method, the bundle will see all packages that its dependency exports via Export-Package. The second method is to use Import-Package. Using this method, the bundle will locate such a package that any bundle exports via Export-Package. You can also version bundles and packages on the producer end and constrain versions on dependency end.

So, armed with this knowledge, you need to ask yourself these questions...

  1. Is the bundle that you are cloning exporting any packages via Export-Package. If it doesn't, then you will be fine.

  2. What bundles currently specify your original bundle and will specify the clone via Require-Bundle? What you trying to avoid is a situation where a single bundle specifies both Bundle.Original and Bundle.Clone in Require-Bundle. If such arrangement is necessary, you will have to rename the cloned packages.

  3. The final thing to check is whether any bundle uses Import-Package on any of your export packages. If it does, you will need to version packages in original and clone differently. This happens in Export-Package directive. You will also need to carefully specify version constrains in the Import-Package directives so that it matches the version of the original or the clone. Failing to version packages and constrain imports will result in seemingly random behavior as to which version is found at runtime. Again, if a single downstream bundle needs to import both original and cloned packages, you will need to rename the cloned packages.

Upvotes: 3

nitind
nitind

Reputation: 20023

While you can have different plug-ins with the same class names and and all plug-ins have different classloaders and classpaths, the JVM will not allow you to actually load two classes with the same name.

Upvotes: 2

Related Questions