Skip to content

Commit cf7ab4f

Browse files
author
Neha Burnwal
committed
Quick fix to add available matching version for Imported packages
1 parent bcbeb9c commit cf7ab4f

File tree

7 files changed

+120
-4
lines changed

7 files changed

+120
-4
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/BundleErrorReporter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,11 @@ private void validateImportPackageVersion(IHeader header, ManifestElement elemen
15151515
String version = element.getAttribute(Constants.VERSION_ATTRIBUTE);
15161516
int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_MISSING_VERSION_IMP_PKG);
15171517
if (severity != CompilerFlags.IGNORE && version == null) {
1518-
VirtualMarker marker = report(NLS.bind(PDECoreMessages.BundleErrorReporter_MissingVersion, element.getValue()), getPackageLine(header, element), severity, PDEMarkerFactory.CAT_OTHER);
1518+
VirtualMarker marker = report(
1519+
NLS.bind(PDECoreMessages.BundleErrorReporter_MissingVersion, element.getValue()),
1520+
getPackageLine(header, element), severity, PDEMarkerFactory.M_MISSINGVERSION_IMPORT_PACKAGE,
1521+
PDEMarkerFactory.CAT_OTHER);
1522+
marker.setAttribute("bundleId", element.getValue()); //$NON-NLS-1$
15191523
addMarkerAttribute(marker,PDEMarkerFactory.compilerKey, CompilerFlags.P_MISSING_VERSION_IMP_PKG);
15201524
}
15211525
validateVersionAttribute(header, element, true);

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/PDEMarkerFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public class PDEMarkerFactory {
7979
public static final int M_SINGLETON_DIR_CHANGE = 0x1033; // other problem
8080
public static final int M_MISSINGVERSION_REQ_BUNDLE = 0x1034; // other
8181
// problem
82+
public static final int M_MISSINGVERSION_IMPORT_PACKAGE = 0x1035; // other
83+
// problem
8284

8385
// build properties fixes
8486
public static final int B_APPEND_SLASH_FOLDER_ENTRY = 0x2001;

ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/PDEUIMessages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3440,6 +3440,6 @@ public class PDEUIMessages extends NLS {
34403440
public static String ProjectUpdateChange_convert_build_to_bnd;
34413441
public static String ProjectUpdateChange_set_pde_preference;
34423442

3443-
public static String AddMatchingVersion_RequireBundle;
3443+
public static String AddMatchingVersion;
34443444

34453445
}

ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/ResolutionGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public IMarkerResolution[] getNonConfigSevResolutions(IMarker marker) {
155155
case PDEMarkerFactory.M_MISSINGVERSION_REQ_BUNDLE:
156156
return new IMarkerResolution[] {
157157
new VersionMatchResolution(AbstractPDEMarkerResolution.CREATE_TYPE, marker) };
158+
case PDEMarkerFactory.M_MISSINGVERSION_IMPORT_PACKAGE:
159+
return new IMarkerResolution[] {
160+
new VersionMatchImportPackageResolution(AbstractPDEMarkerResolution.CREATE_TYPE, marker) };
158161
}
159162
return NO_RESOLUTIONS;
160163
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025, 2025 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.pde.internal.ui.correction;
16+
17+
import java.util.HashSet;
18+
import java.util.Objects;
19+
import java.util.Set;
20+
21+
import org.eclipse.core.resources.IMarker;
22+
import org.eclipse.osgi.service.resolver.BundleDescription;
23+
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
24+
import org.eclipse.pde.core.plugin.IPluginModelBase;
25+
import org.eclipse.pde.core.plugin.PluginRegistry;
26+
import org.eclipse.pde.core.target.NameVersionDescriptor;
27+
import org.eclipse.pde.internal.core.text.bundle.Bundle;
28+
import org.eclipse.pde.internal.core.text.bundle.BundleModel;
29+
import org.eclipse.pde.internal.core.text.bundle.ImportPackageHeader;
30+
import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject;
31+
import org.eclipse.pde.internal.core.text.bundle.ManifestHeader;
32+
import org.eclipse.pde.internal.core.text.bundle.PackageObject;
33+
import org.eclipse.pde.internal.core.util.VersionUtil;
34+
import org.eclipse.pde.internal.ui.PDEUIMessages;
35+
import org.osgi.framework.Constants;
36+
37+
/**
38+
* Resolution to add available matching version for Imported package in MANIFEST
39+
*/
40+
public class VersionMatchImportPackageResolution extends AbstractManifestMarkerResolution {
41+
42+
public VersionMatchImportPackageResolution(int type, IMarker marker) {
43+
super(type, marker);
44+
}
45+
46+
public String getVersion(Object inputElement) {
47+
IPluginModelBase[] models = PluginRegistry.getActiveModels();
48+
Set<NameVersionDescriptor> nameVersions = new HashSet<>();
49+
for (IPluginModelBase pluginModel : models) {
50+
BundleDescription desc = pluginModel.getBundleDescription();
51+
52+
String id = desc == null ? null : desc.getSymbolicName();
53+
if (id == null) {
54+
continue;
55+
}
56+
ExportPackageDescription[] exported = desc.getExportPackages();
57+
for (ExportPackageDescription exportedPackage : exported) {
58+
String name = exportedPackage.getName();
59+
ManifestHeader mHeader = new ManifestHeader("Export-Package", "", //$NON-NLS-1$//$NON-NLS-2$
60+
new org.eclipse.pde.internal.core.bundle.Bundle(), "\n"); //$NON-NLS-1$
61+
PackageObject po = new PackageObject(mHeader, exportedPackage.getName(),
62+
exportedPackage.getVersion().toString(), "version"); //$NON-NLS-1$
63+
64+
NameVersionDescriptor nameVersion = new NameVersionDescriptor(exportedPackage.getName(),
65+
exportedPackage.getVersion().toString(), NameVersionDescriptor.TYPE_PACKAGE);
66+
exportedPackage.getExporter().getBundle();
67+
68+
if (("java".equals(name) || name.startsWith("java."))) { //$NON-NLS-1$ //$NON-NLS-2$
69+
// $NON-NLS-2$
70+
continue;
71+
}
72+
if (nameVersions.add(nameVersion)) {
73+
if (name.equalsIgnoreCase(inputElement.toString())) {
74+
return po.getVersion();
75+
}
76+
}
77+
}
78+
}
79+
return ""; //$NON-NLS-1$
80+
}
81+
82+
@Override
83+
protected void createChange(BundleModel model) {
84+
String bundleId = Objects.requireNonNull(marker.getAttribute("bundleId", (String) null)); //$NON-NLS-1$
85+
Bundle bundle = (Bundle) model.getBundle();
86+
ImportPackageHeader header = (ImportPackageHeader) bundle.getManifestHeader(Constants.IMPORT_PACKAGE);
87+
if (header != null) {
88+
for (ImportPackageObject importPackage : header.getPackages()) {
89+
if (bundleId.equals(importPackage.getName())) {
90+
String version = getVersion(bundleId);
91+
if (!version.isEmpty()) {
92+
// Sanitize version: Remove a potential qualifier
93+
version = VersionUtil.computeInitialPluginVersion(version);
94+
importPackage.setVersion(version);
95+
}
96+
}
97+
}
98+
}
99+
}
100+
101+
@Override
102+
public String getLabel() {
103+
return PDEUIMessages.AddMatchingVersion;
104+
}
105+
106+
107+
}

ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/VersionMatchResolution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected void createChange(BundleModel model) {
5757

5858
@Override
5959
public String getLabel() {
60-
return PDEUIMessages.AddMatchingVersion_RequireBundle;
60+
return PDEUIMessages.AddMatchingVersion;
6161
}
6262

6363
}

ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/pderesources.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ UpdateClasspathResolution_label=Update the classpath and compliance settings
13011301
UpdateExecutionEnvironment_label=Update the execution environment based on JRE on the classpath
13021302
UpdateClasspathJob_task = Update classpaths...
13031303
UpdateClasspathJob_title = Updating Plug-in Classpaths
1304-
AddMatchingVersion_RequireBundle = Require latest available version
1304+
AddMatchingVersion = Require latest available version
13051305

13061306
RuntimeWorkbenchShortcut_title=Select Configuration
13071307
RuntimeWorkbenchShortcut_select_debug=Select a launch configuration to debug:

0 commit comments

Comments
 (0)