Skip to content

Commit

Permalink
Migrate to reworked PluginRegistry.findModel() API
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Aug 15, 2024
1 parent 68e294f commit 4a9c2a1
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 68 deletions.
1 change: 1 addition & 0 deletions org.eclipse.pde.doc.user/forceQualifierUpdate.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# To force a version qualifier update add the issue here
Fix deprecation tag 'since' value
Modernize pde.project description interfaces: https://github.com/eclipse-pde/eclipse.pde/pull/1341
Rework PluginRegistry API and introduce VersionMatchRule enum : https://github.com/eclipse-pde/eclipse.pde/pull/1163
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class FeatureImport extends VersionableObject implements IFeatureImport {

public IPlugin getPlugin() {
if (id != null && fType == PLUGIN) {
IPluginModelBase model = PluginRegistry.findModel(id, version, fMatch, null);
IPluginModelBase model = PluginRegistry.findModel(id, version, VersionUtil.matchRuleFromLiteral(fMatch));
return model instanceof IPluginModel ? ((IPluginModel) model).getPlugin() : null;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void writeDelimeter(PrintWriter writer) {
protected IPluginModelBase findModel() {
String version = iimport.getVersion();
VersionRange range = new VersionRange(version);
return PluginRegistry.findModel(getId(), range, null);
return PluginRegistry.findModel(getId(), range);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class PluginJavaSearchUtil {
public static IPluginModelBase[] getPluginImports(IPluginImport dep) {
HashSet<IPluginModelBase> set = new HashSet<>();
VersionRange range = new VersionRange(dep.getVersion());
collectAllPrerequisites(PluginRegistry.findModel(dep.getId(), range, null), set);
collectAllPrerequisites(PluginRegistry.findModel(dep.getId(), range), set);
return set.toArray(new IPluginModelBase[set.size()]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.eclipse.pde.core.plugin.IMatchRules;
import org.eclipse.pde.core.plugin.IPluginBase;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.core.plugin.PluginRegistry.PluginFilter;
import org.eclipse.pde.core.plugin.VersionMatchRule;
import org.eclipse.pde.core.project.IBundleProjectDescription;
import org.junit.Test;
import org.osgi.framework.FrameworkUtil;
Expand All @@ -42,124 +43,96 @@ public class PluginRegistryTests {

@Test
public void testMatchNone() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", null, IMatchRules.NONE, null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", null, (VersionMatchRule) null);
assertNotNull(model);
assertEquals("org.eclipse.jdt.debug", model.getPluginBase().getId());
}

@Test
public void testMatchGreaterOrEqual() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", "3.0.0",
IMatchRules.GREATER_OR_EQUAL, null);
VersionMatchRule.GREATER_OR_EQUAL);
assertNotNull(model);
assertEquals("org.eclipse.jdt.debug", model.getPluginBase().getId());
}

@Test
public void testMatchPerfect() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", "3.0.0", IMatchRules.PERFECT, null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", "3.0.0", VersionMatchRule.PERFECT);
assertNull(model);
}

@Test
public void testMatchCompatible() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", "3.6.0", IMatchRules.COMPATIBLE,
null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", "3.6.0",
VersionMatchRule.COMPATIBLE);
assertNotNull(model);
assertEquals("org.eclipse.jdt.debug", model.getPluginBase().getId());
}

@Test
public void testMatchCompatibleNone() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.pde.core", "2.6.0", IMatchRules.COMPATIBLE,
null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.pde.core", "2.6.0", VersionMatchRule.COMPATIBLE);
assertNull(model);
}

@Test
public void testMatchPrefix() {
@SuppressWarnings("removal") // Test special behavior of old API
IPluginModelBase model = PluginRegistry.findModel("org.eclipse", "3.6.0", IMatchRules.PREFIX, null);
// prefix rule is not supported by this API, should return null
assertNull(model);
}

@Test
public void testRangeNone() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", null, (PluginFilter) null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", null);
assertNotNull(model);
assertEquals("org.eclipse.jdt.debug", model.getPluginBase().getId());
}

@Test
public void testOverlapRange() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", new VersionRange("[2.0.0,4.0.0)"),
null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", new VersionRange("[2.0.0,4.0.0)"));
assertNotNull(model);
assertEquals("org.eclipse.jdt.debug", model.getPluginBase().getId());
}

@Test
public void testMinRange() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", new VersionRange("3.0.0"), null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", new VersionRange("3.0.0"));
assertNotNull(model);
assertEquals("org.eclipse.jdt.debug", model.getPluginBase().getId());
}

@Test
public void testUnmatchedRange() {
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.pde.core", new VersionRange("[1.0.0,2.0.0)"),
null);
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.pde.core", new VersionRange("[1.0.0,2.0.0)"));
assertNull(model);
}

@Test
public void testRangeWithFilterMatch() {
PluginFilter filter = new PluginFilter() {
@Override
public boolean accept(IPluginModelBase model) {
IPluginBase base = model.getPluginBase();
if (base != null) {
String id = base.getId();
if (id != null) {
return id.startsWith("org.eclipse");
}
}
return false;
}
};
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", new VersionRange("[2.0.0,4.0.0)"),
filter);
IPluginModelBase model = PluginRegistry.findModels("org.eclipse.jdt.debug", new VersionRange("[2.0.0,4.0.0)"))
.filter(m -> m.getPluginBase().getId().startsWith("org.eclipse")).findFirst().orElse(null);
assertNotNull(model);
assertEquals("org.eclipse.jdt.debug", model.getPluginBase().getId());
}

@Test
public void testRangeWithFilterNoMatch() {
PluginFilter filter = new PluginFilter() {
@Override
public boolean accept(IPluginModelBase model) {
IPluginBase base = model.getPluginBase();
if (base != null) {
String id = base.getId();
if (id != null) {
return id.startsWith("xyz");
}
}
return false;
}
};
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.jdt.debug", new VersionRange("[2.0.0,4.0.0)"),
filter);
IPluginModelBase model = PluginRegistry.findModels("org.eclipse.jdt.debug", new VersionRange("[2.0.0,4.0.0)"))
.filter(m -> m.getPluginBase().getId().startsWith("xyz")).findFirst().orElse(null);
assertNull(model);
}

@Test
public void testSingleRangeMatch() {
IPluginModelBase[] models = PluginRegistry.findModels("org.eclipse.jdt.debug",
new VersionRange("[1.0.0,4.0.0)"), null);
List<IPluginModelBase> models = PluginRegistry
.findModels("org.eclipse.jdt.debug", new VersionRange("[1.0.0,4.0.0)")).toList();
assertNotNull(models);
assertEquals(1, models.length);
assertEquals("org.eclipse.jdt.debug", models[0].getPluginBase().getId());
assertEquals(1, models.size());
assertEquals("org.eclipse.jdt.debug", models.get(0).getPluginBase().getId());
}

@Test
Expand All @@ -175,11 +148,12 @@ public void testWorkspaceOverTarget() throws CoreException {

waitForBuild();

IPluginModelBase[] models = PluginRegistry.findModels("org.junit", new VersionRange("[3.8.2,4.8.2]"), null);
List<IPluginModelBase> models = PluginRegistry.findModels("org.junit", new VersionRange("[3.8.2,4.8.2]"))
.toList();
assertNotNull(models);
assertEquals(1, models.length);
assertEquals("org.junit", models[0].getPluginBase().getId());
assertEquals(project, models[0].getUnderlyingResource().getProject());
assertEquals(1, models.size());
assertEquals("org.junit", models.get(0).getPluginBase().getId());
assertEquals(project, models.get(0).getUnderlyingResource().getProject());

} finally {
if (project.exists()) {
Expand All @@ -194,8 +168,7 @@ public void testMatchEquivalent() {
Version testsVersion = FrameworkUtil.getBundle(PluginRegistryTests.class).getVersion();
IPluginModelBase model = PluginRegistry.findModel("org.eclipse.pde.ui.tests",
String.format("%s.%s.%s", testsVersion.getMajor(), testsVersion.getMinor(), testsVersion.getMicro()),
IMatchRules.EQUIVALENT,
null);
VersionMatchRule.EQUIVALENT);
assertNotNull("NOTE: This test might also fail because the version of the bundle got changed.", model);
assertEquals("org.eclipse.pde.ui.tests", model.getPluginBase().getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.eclipse.pde.core.build.IBuildEntry;
import org.eclipse.pde.core.plugin.IFragment;
import org.eclipse.pde.core.plugin.IFragmentModel;
import org.eclipse.pde.core.plugin.IMatchRules;
import org.eclipse.pde.core.plugin.IPlugin;
import org.eclipse.pde.core.plugin.IPluginAttribute;
import org.eclipse.pde.core.plugin.IPluginBase;
Expand All @@ -44,6 +43,7 @@
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.IPluginObject;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.core.plugin.VersionMatchRule;
import org.eclipse.pde.internal.core.ICoreConstants;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.TargetPlatformHelper;
Expand Down Expand Up @@ -385,7 +385,8 @@ public String getObjectText(ISiteFeature obj) {
}

public String getObjectText(ISiteBundle obj) {
IPluginModelBase modelBase = PluginRegistry.findModel(obj.getId(), obj.getVersion(), IMatchRules.COMPATIBLE, null);
IPluginModelBase modelBase = PluginRegistry.findModel(obj.getId(), obj.getVersion(),
VersionMatchRule.COMPATIBLE);
if (modelBase != null)
return getObjectText(modelBase.getPluginBase());
return preventNull(obj.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.eclipse.pde.internal.ui.editor.category;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.pde.core.plugin.IMatchRules;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.core.plugin.VersionMatchRule;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.isite.ISiteBundle;
import org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition;
Expand Down Expand Up @@ -65,7 +65,7 @@ public Image getImage(Object element) {
}
if (element instanceof SiteBundleAdapter) {
ISiteBundle bundle = ((SiteBundleAdapter) element).bundle;
if (PluginRegistry.findModel(bundle.getId(), bundle.getVersion(), IMatchRules.COMPATIBLE, null) == null) {
if (PluginRegistry.findModel(bundle.getId(), bundle.getVersion(), VersionMatchRule.COMPATIBLE) == null) {
return this.fMissingSiteBundleImage;
}
return this.fSiteBundleImage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.pde.core.IBaseModel;
import org.eclipse.pde.core.plugin.IMatchRules;
import org.eclipse.pde.core.plugin.IPluginBase;
import org.eclipse.pde.core.plugin.IPluginModel;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.core.plugin.VersionMatchRule;
import org.eclipse.pde.internal.core.builders.DependencyLoop;
import org.eclipse.pde.internal.core.builders.DependencyLoopFinder;
import org.eclipse.pde.internal.ui.PDELabelProvider;
Expand Down Expand Up @@ -102,7 +102,7 @@ private IPluginModelBase getStatePlugin(IPluginModelBase pluginModel) {
// The pluginModel set for this page does not have a BundleDescriptor
// set, therefore search the pluginModel from the registry that has it
IPluginBase pluginBase = pluginModel.getPluginBase();
return PluginRegistry.findModel(pluginBase.getId(), pluginBase.getVersion(), IMatchRules.PERFECT, null);
return PluginRegistry.findModel(pluginBase.getId(), pluginBase.getVersion(), VersionMatchRule.PERFECT);
}

private void doFindLoops(IPluginModelBase pluginModelBase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.core.plugin.IMatchRules;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.core.plugin.VersionMatchRule;
import org.eclipse.pde.internal.core.DependencyManager;
import org.eclipse.pde.internal.core.DependencyManager.Options;
import org.eclipse.pde.internal.core.ICoreConstants;
Expand Down Expand Up @@ -217,7 +217,7 @@ public static void handleAddRequired(IProductPlugin[] plugins, boolean includeOp
}
List<BundleDescription> list = Stream.of(plugins).map(plugin -> {
String version = VersionUtil.isEmptyVersion(plugin.getVersion()) ? null : plugin.getVersion();
return PluginRegistry.findModel(plugin.getId(), version, IMatchRules.PERFECT, null);
return PluginRegistry.findModel(plugin.getId(), version, VersionMatchRule.PERFECT);
}).filter(Objects::nonNull).map(IPluginModelBase::getBundleDescription).toList();

DependencyManager.Options[] options = includeOptional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.core.plugin.IMatchRules;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.core.plugin.VersionMatchRule;
import org.eclipse.pde.internal.core.DependencyManager;
import org.eclipse.pde.internal.core.FeatureModelManager;
import org.eclipse.pde.internal.core.PDECore;
Expand Down Expand Up @@ -382,7 +382,7 @@ private static void addFeaturePlugins(IFeature feature, Set<IPluginModelBase> la
if (id == null || version == null) {
continue;
}
IPluginModelBase model = PluginRegistry.findModel(id, version, IMatchRules.EQUIVALENT, null);
IPluginModelBase model = PluginRegistry.findModel(id, version, VersionMatchRule.EQUIVALENT);
if (model == null) {
model = PluginRegistry.findModel(id);
}
Expand Down

0 comments on commit 4a9c2a1

Please sign in to comment.