Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,11 @@ private void validateImportPackageVersion(IHeader header, ManifestElement elemen
String version = element.getAttribute(Constants.VERSION_ATTRIBUTE);
int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_MISSING_VERSION_IMP_PKG);
if (severity != CompilerFlags.IGNORE && version == null) {
VirtualMarker marker = report(NLS.bind(PDECoreMessages.BundleErrorReporter_MissingVersion, element.getValue()), getPackageLine(header, element), severity, PDEMarkerFactory.CAT_OTHER);
VirtualMarker marker = report(
NLS.bind(PDECoreMessages.BundleErrorReporter_MissingVersion, element.getValue()),
getPackageLine(header, element), severity, PDEMarkerFactory.M_MISSINGVERSION_IMPORT_PACKAGE,
PDEMarkerFactory.CAT_OTHER);
marker.setAttribute("pkgName", element.getValue()); //$NON-NLS-1$
addMarkerAttribute(marker,PDEMarkerFactory.compilerKey, CompilerFlags.P_MISSING_VERSION_IMP_PKG);
}
validateVersionAttribute(header, element, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class PDEMarkerFactory {
public static final int M_SINGLETON_DIR_CHANGE = 0x1033; // other problem
public static final int M_MISSINGVERSION_REQ_BUNDLE = 0x1034; // other
// problem
public static final int M_MISSINGVERSION_IMPORT_PACKAGE = 0x1035; // other
// problem

// build properties fixes
public static final int B_APPEND_SLASH_FOLDER_ENTRY = 0x2001;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -67,8 +66,6 @@
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.Version;
import org.osgi.framework.VersionRange;
import org.osgi.framework.namespace.ExecutionEnvironmentNamespace;
import org.osgi.resource.Namespace;
import org.osgi.resource.Requirement;
Expand Down Expand Up @@ -399,16 +396,6 @@ public static void parseRequiredEEsFromFilter(String eeFilter, Consumer<String>
}
}

public static Optional<VersionRange> createConsumerRequirementRange(Version version) {
if (version != null && !Version.emptyVersion.equals(version)) {
return Optional.ofNullable(new VersionRange(VersionRange.LEFT_CLOSED, //
new Version(version.getMajor(), version.getMinor(), 0), //
new Version(version.getMajor() + 1, 0, 0), //
VersionRange.RIGHT_OPEN));
}
return Optional.empty();
}

/**
* Return the value of "Eclipse-SourceReferences" in MANIFEST.MF from the
* given bundle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.pde.internal.core.util;

import java.util.Comparator;
import java.util.Optional;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
Expand Down Expand Up @@ -120,4 +121,14 @@ public static String computeInitialPluginVersion(String version) {
return version;
}

public static Optional<VersionRange> createConsumerRequirementRange(Version version) {
if (version != null && !Version.emptyVersion.equals(version)) {
return Optional.ofNullable(new VersionRange(VersionRange.LEFT_CLOSED, //
new Version(version.getMajor(), version.getMinor(), 0), //
new Version(version.getMajor() + 1, 0, 0), //
VersionRange.RIGHT_OPEN));
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.eclipse.pde.internal.core.PDEState;
import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
import org.eclipse.pde.internal.core.ibundle.IBundleModel;
import org.eclipse.pde.internal.core.util.ManifestUtils;
import org.eclipse.pde.internal.core.util.VersionUtil;
import org.osgi.framework.Constants;
import org.osgi.framework.VersionRange;

Expand All @@ -33,7 +33,7 @@ public class ImportPackageObject extends PackageObject {
private static final long serialVersionUID = 1L;

private static String getVersion(ExportPackageDescription desc) {
return ManifestUtils.createConsumerRequirementRange(desc.getVersion()).map(VersionRange::toString).orElse(null);
return VersionUtil.createConsumerRequirementRange(desc.getVersion()).map(VersionRange::toString).orElse(null);
}

public ImportPackageObject(ManifestHeader header, ManifestElement element, String versionAttribute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3453,6 +3453,7 @@ public class PDEUIMessages extends NLS {
public static String ProjectUpdateChange_convert_build_to_bnd;
public static String ProjectUpdateChange_set_pde_preference;

public static String AddMatchingVersion_RequireBundle;
public static String AddMatchingVersion;
public static String AddMatchingVersionDescription;

}
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,13 @@ public IMarkerResolution[] getNonConfigSevResolutions(IMarker marker) {
new UpdateCorrectHeaderName(AbstractPDEMarkerResolution.RENAME_TYPE, marker) };
case PDEMarkerFactory.M_MISSINGVERSION_REQ_BUNDLE:
return new IMarkerResolution[] {
new VersionMatchResolution(AbstractPDEMarkerResolution.CREATE_TYPE, marker) };
new VersionMatchRequireBundleResolution(AbstractPDEMarkerResolution.CREATE_TYPE, marker) };
case PDEMarkerFactory.M_NO_SPACE_AFTER_COLON:
return new IMarkerResolution[] {
new AddSpaceBeforeValue(AbstractPDEMarkerResolution.CREATE_TYPE, marker) };
case PDEMarkerFactory.M_MISSINGVERSION_IMPORT_PACKAGE:
return new IMarkerResolution[] {
new VersionMatchImportPackageResolution(AbstractPDEMarkerResolution.CREATE_TYPE, marker) };
}
return NO_RESOLUTIONS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*******************************************************************************
* Copyright (c) 2025, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.pde.internal.ui.correction;

import java.util.Objects;
import java.util.Optional;

import org.eclipse.core.resources.IMarker;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.core.text.bundle.Bundle;
import org.eclipse.pde.internal.core.text.bundle.BundleModel;
import org.eclipse.pde.internal.core.text.bundle.ImportPackageHeader;
import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject;
import org.eclipse.pde.internal.core.util.VersionUtil;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.osgi.framework.Constants;
import org.osgi.framework.Version;
import org.osgi.framework.VersionRange;

/**
* Resolution to add available matching version for Imported package in MANIFEST
*/
public class VersionMatchImportPackageResolution extends AbstractManifestMarkerResolution {

public VersionMatchImportPackageResolution(int type, IMarker marker) {
super(type, marker);
}

public Version getVersion(Object inputElement) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this parameter be of type String instead of Object?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

versionRange is calculated based on version object(in line 79). If we change the type to string, we will have to change the type again there.

IPluginModelBase[] models = PluginRegistry.getActiveModels();
Version highest = null;
for (IPluginModelBase pluginModel : models) {
BundleDescription desc = pluginModel.getBundleDescription();

String id = desc == null ? null : desc.getSymbolicName();
if (id == null) {
continue;
}
ExportPackageDescription[] exported = desc.getExportPackages();
for (ExportPackageDescription exportedPackage : exported) {
String name = exportedPackage.getName();
if (("java".equals(name) || name.startsWith("java."))) { //$NON-NLS-1$ //$NON-NLS-2$
// $NON-NLS-2$
continue;
}
if (name.equals(inputElement.toString())) {
Version ver = exportedPackage.getVersion();
if (ver != null) {
if (highest == null || ver.compareTo(highest) > 0) {
highest = ver;
}
}
}
}
}
return highest;
}

@Override
protected void createChange(BundleModel model) {
String pkgName = Objects.requireNonNull(marker.getAttribute("pkgName", (String) null)); //$NON-NLS-1$
Bundle bundle = (Bundle) model.getBundle();
ImportPackageHeader header = (ImportPackageHeader) bundle.getManifestHeader(Constants.IMPORT_PACKAGE);
if (header != null) {
for (ImportPackageObject importPackage : header.getPackages()) {
if (pkgName.equals(importPackage.getName())) {
Version version = getVersion(pkgName);
if (version == null) {
return;
}
// Sanitize version: Remove a potential qualifier
Optional<VersionRange> versionRange = VersionUtil.createConsumerRequirementRange(version);
importPackage.setVersion(versionRange.map(VersionRange::toString).orElse(null));
}
}
}
}

@Override
public String getLabel() {
return PDEUIMessages.AddMatchingVersion;
}

@Override
public String getDescription() {
return PDEUIMessages.AddMatchingVersionDescription;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.pde.internal.ui.correction;

import java.util.Objects;
import java.util.Optional;

import org.eclipse.core.resources.IMarker;
import org.eclipse.pde.core.plugin.IPluginModelBase;
Expand All @@ -26,12 +27,14 @@
import org.eclipse.pde.internal.core.util.VersionUtil;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.osgi.framework.Constants;
import org.osgi.framework.Version;
import org.osgi.framework.VersionRange;

/**
* Resolution to add available matching version for Required bundles in MANIFEST
*/
public class VersionMatchResolution extends AbstractManifestMarkerResolution {
public VersionMatchResolution(int type, IMarker marker) {
public class VersionMatchRequireBundleResolution extends AbstractManifestMarkerResolution {
public VersionMatchRequireBundleResolution(int type, IMarker marker) {
super(type, marker);
}

Expand All @@ -47,8 +50,9 @@ protected void createChange(BundleModel model) {
if (modelBase != null) {
String version = modelBase.getPluginBase().getVersion();
// Sanitize version: Remove a potential qualifier
version = VersionUtil.computeInitialPluginVersion(version);
requiredBundle.setVersion(version);
Optional<VersionRange> versionRange = VersionUtil
.createConsumerRequirementRange(new Version(version));
requiredBundle.setVersion(versionRange.map(VersionRange::toString).orElse(null));
}
}
}
Expand All @@ -57,7 +61,12 @@ protected void createChange(BundleModel model) {

@Override
public String getLabel() {
return PDEUIMessages.AddMatchingVersion_RequireBundle;
return PDEUIMessages.AddMatchingVersion;
}

@Override
public String getDescription() {
return PDEUIMessages.AddMatchingVersionDescription;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
import org.eclipse.pde.internal.core.text.bundle.ImportPackageHeader;
import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject;
import org.eclipse.pde.internal.core.util.ManifestUtils;
import org.eclipse.pde.internal.core.util.VersionUtil;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEPluginImages;
import org.eclipse.pde.internal.ui.PDEUIMessages;
Expand Down Expand Up @@ -195,7 +195,7 @@ protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws Co
}
BundleDescription requiredBundle = getChangeObject();
String pluginId = requiredBundle.getSymbolicName();
VersionRange versionRange = ManifestUtils
VersionRange versionRange = VersionUtil
.createConsumerRequirementRange(requiredBundle.getVersion()).orElse(null);
IPluginImport[] imports = base.getPluginBase().getImports();
if (!isUndo()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,8 @@ UpdateClasspathResolution_label=Update the classpath and compliance settings
UpdateExecutionEnvironment_label=Update the execution environment based on JRE on the classpath
UpdateClasspathJob_task = Update classpaths...
UpdateClasspathJob_title = Updating Plug-in Classpaths
AddMatchingVersion_RequireBundle = Require latest available version
AddMatchingVersion = Require latest available version range
AddMatchingVersionDescription = Add a version range aligned with the latest compatible available version

RuntimeWorkbenchShortcut_title=Select Configuration
RuntimeWorkbenchShortcut_select_debug=Select a launch configuration to debug:
Expand Down
Loading