Skip to content

Commit

Permalink
Quick-fix for import package resolution failure due to missing mandatory
Browse files Browse the repository at this point in the history
attributes:
  • Loading branch information
alshamams committed Jul 21, 2023
1 parent 0abc73b commit ae118d5
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class PDECoreMessages extends NLS {

public static String BuildErrorReporter_cannotFindJar;

public static String BundleErrorReporter_MissingMandatoryDirective;

public static String BundleErrorReporter_ConflictingAutoModule;

public static String BundleErrorReporter_badFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,39 @@ private void validateImportPackage(IProgressMonitor monitor) {
addMarkerAttribute(marker,PDEMarkerFactory.compilerKey,CompilerFlags.P_UNRESOLVED_IMPORTS);
return;
}
} else {

Map<String, Object> exportAttributes = export.getAttributes();
Map<String, Object> exportDirectives = export.getDirectives();
if (exportDirectives != null) {
if (exportDirectives.containsKey(Constants.RESOLUTION_MANDATORY)) {
Map<String, Object> importAttributes = importSpec.getAttributes();
String[] mandatories = (String[]) exportDirectives.get(Constants.RESOLUTION_MANDATORY);
for (String mandatory : mandatories) {
if (importAttributes == null || !importAttributes.containsKey(mandatory)
|| importAttributes.get(mandatory).toString() != exportAttributes
.get(mandatory).toString()) {
VirtualMarker marker = report(
NLS.bind(PDECoreMessages.BundleErrorReporter_MissingMandatoryDirective,
name),
getPackageLine(header, element), severity,
PDEMarkerFactory.M_NO_MANDATORY_ATTR_IMPORT_PACKAGE,
PDEMarkerFactory.CAT_FATAL);
addMarkerAttribute(marker, PDEMarkerFactory.compilerKey,
CompilerFlags.P_UNRESOLVED_IMPORTS);

if (marker != null) {
marker.setAttribute("packageName", name); //$NON-NLS-1$
marker.setAttribute("mandatoryattrkey", //$NON-NLS-1$
mandatory);
marker.setAttribute("mandatoryvalue", //$NON-NLS-1$
exportAttributes.get(mandatory));
}
}
}
}
}

} else {
VirtualMarker marker = report(NLS.bind(PDECoreMessages.BundleErrorReporter_unresolvedExporter,new String[] { export.getSupplier().getSymbolicName(), name }),getPackageLine(header, element), severity, PDEMarkerFactory.CAT_OTHER);
addMarkerAttribute(marker,PDEMarkerFactory.compilerKey, CompilerFlags.P_UNRESOLVED_IMPORTS);
return;
Expand All @@ -1244,6 +1276,7 @@ private void validateImportPackage(IProgressMonitor monitor) {
PDEMarkerFactory.CAT_FATAL);
addMarkerAttribute(marker, PDEMarkerFactory.compilerKey, CompilerFlags.P_UNRESOLVED_IMPORTS);
if (marker != null) {
// marker.setAttribute(PDEMarkerFactory.M_NO_MANDATORY_ATTR_IMPORT_PACKAGE);
marker.setAttribute("packageName", name); //$NON-NLS-1$
if (optional) {
marker.setAttribute("optional", true); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class PDEMarkerFactory {
public static final int M_EXEC_ENV_TOO_LOW = 0x1029; // other problem
public static final int M_CONFLICTING_AUTOMATIC_MODULE = 0x1030; // other
// problem
public static final int M_NO_MANDATORY_ATTR_IMPORT_PACKAGE = 0x1031; // fatal
// 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 @@ -2609,6 +2609,10 @@ public class PDEUIMessages extends NLS {

public static String RemoveImportPkgResolution_label;

public static String AddMandatoryAttrResolution_label;

public static String AddMandatoryAttrResolution_description;

public static String NoLineTerminationResolutionCreate_description;

public static String NoLineTerminationResolutionCreate_label;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2023 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
*Qui
* Contributors:
* IBM Corporation - initial API and implementation
* Alshama M S <[email protected]> Initial implementation
*******************************************************************************/

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

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
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.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.osgi.framework.Constants;

/**
* <p>
* Represents a resolution to the problem of import package resolution failure
* due to missing mandatory attributes
* </p>
*/
public class AddMandatoryAttributeImportPackageResolution extends AbstractManifestMarkerResolution {
/**
* Creates a new resolution
*/
public AddMandatoryAttributeImportPackageResolution(IMarker marker) {
super(AbstractPDEMarkerResolution.CREATE_TYPE, marker);
}

/**
* Resolves the problem by adding mandatory attribute
*/
@Override
protected void createChange(BundleModel model) {
try {
String packagename = (String) marker.getAttribute("packageName"); //$NON-NLS-1$
Bundle bundle = (Bundle) model.getBundle();
ImportPackageHeader header = (ImportPackageHeader) bundle.getManifestHeader(Constants.IMPORT_PACKAGE);
if (header != null) {
ImportPackageObject obj = header.getPackage(packagename);
obj.setAttribute((String) marker.getAttribute("mandatoryattrkey"), //$NON-NLS-1$
(String) marker.getAttribute("mandatoryvalue"));//$NON-NLS-1$
header.removePackage(packagename);
header.addPackage(obj);
}
} catch (CoreException e) {
PDEPlugin.log(e);
}
}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public IMarkerResolution[] getNonConfigSevResolutions(IMarker marker) {
return new IMarkerResolution[] {new NoLineTerminationResolution(AbstractPDEMarkerResolution.REMOVE_TYPE, marker)};
case PDEMarkerFactory.M_R4_SYNTAX_IN_R3_BUNDLE :
return new IMarkerResolution[] {new AddBundleManifestVersionResolution(marker)};
case PDEMarkerFactory.M_NO_MANDATORY_ATTR_IMPORT_PACKAGE:
return new IMarkerResolution[] { new AddMandatoryAttributeImportPackageResolution(marker) };
case PDEMarkerFactory.M_SERVICECOMPONENT_MISSING_LAZY:
return new IMarkerResolution[] {
new AddActivationPolicyResolution(AbstractPDEMarkerResolution.CREATE_TYPE, marker) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,9 @@ ReplaceBuildEntryResolution_replaceToken=Change the value of ''{1}'' build entry
RemoveImportPkgResolution_label=Remove the ''{0}'' package from the list
OptionalImportPkgResolution_description=Mark ''{0}'' as an optional imported package
OptionalImportPkgResolution_label=Mark package ''{0}'' as an optional imported package
AddMandatoryAttrResolution_label=Adds mandatory attribute value
AddMandatoryAttrResolution_description=Add mandatory attribute value
BundleErrorReporter_MissingMandatoryDirective=Mandatory attribute missing for the imported package
OptionalRequireBundleResolution_description=Mark bundle ''{0}'' as optional
OptionalRequireBundleResolution_label=Mark bundle ''{0}'' as optional
AddBundleManifestVersionResolution_description=Adds the 'Bundle-ManifestVersion: 2' header to support OSGi R4 headers
Expand Down

0 comments on commit ae118d5

Please sign in to comment.