Skip to content

Commit

Permalink
Consider EE requirements in EE-Section of ManifestEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jul 7, 2024
1 parent 7c27f5c commit 7ddc4e9
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.ui.ISharedImages;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.resource.ImageDescriptor;
Expand Down Expand Up @@ -81,7 +82,6 @@
import org.eclipse.pde.internal.core.isite.ISiteFeature;
import org.eclipse.pde.internal.core.plugin.ImportObject;
import org.eclipse.pde.internal.core.schema.SchemaRegistry;
import org.eclipse.pde.internal.core.text.bundle.ExecutionEnvironment;
import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject;
import org.eclipse.pde.internal.core.text.bundle.PackageObject;
Expand Down Expand Up @@ -171,8 +171,8 @@ public String getText(Object obj) {
if (obj instanceof PackageObject) {
return getObjectText((PackageObject) obj);
}
if (obj instanceof ExecutionEnvironment) {
return getObjectText((ExecutionEnvironment) obj);
if (obj instanceof IExecutionEnvironment ee) {
return preventNull(ee.getId());
}
if (obj instanceof Locale) {
return getObjectText((Locale) obj);
Expand All @@ -183,10 +183,6 @@ public String getText(Object obj) {
return super.getText(obj);
}

private String getObjectText(ExecutionEnvironment environment) {
return preventNull(environment.getName());
}

public String getObjectText(IPluginBase pluginBase) {
String name = isFullNameModeEnabled() ? pluginBase.getTranslatedName() : pluginBase.getId();
name = preventNull(name);
Expand Down Expand Up @@ -529,8 +525,8 @@ public Image getImage(Object obj) {
if (obj instanceof PackageObject) {
return getObjectImage((PackageObject) obj);
}
if (obj instanceof ExecutionEnvironment) {
return getObjectImage((ExecutionEnvironment) obj);
if (obj instanceof IExecutionEnvironment) {
return get(PDEPluginImages.DESC_JAVA_LIB_OBJ);
}
if (obj instanceof ResolverError) {
return getObjectImage((ResolverError) obj);
Expand All @@ -554,10 +550,6 @@ private Image getObjectImage(ResolverError obj) {
};
}

private Image getObjectImage(ExecutionEnvironment environment) {
return get(PDEPluginImages.DESC_JAVA_LIB_OBJ);
}

private Image getObjectImage(IPlugin plugin) {
return getObjectImage(plugin, false, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package org.eclipse.pde.internal.ui.editor.plugin;

import java.util.Map;
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextSelection;
Expand All @@ -33,6 +36,7 @@
import org.eclipse.pde.core.plugin.IPluginLibrary;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.core.ibundle.IBundleModel;
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModel;
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
import org.eclipse.pde.internal.core.plugin.ImportObject;
Expand Down Expand Up @@ -74,6 +78,7 @@
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.osgi.framework.Constants;
import org.osgi.framework.namespace.ExecutionEnvironmentNamespace;

public class BundleSourcePage extends KeyValueSourcePage {

Expand Down Expand Up @@ -108,7 +113,12 @@ public Object[] getChildren(Object parent) {
} else if (parent instanceof ExportPackageHeader exportHeader) {
return exportHeader.getPackages();
} else if (parent instanceof RequiredExecutionEnvironmentHeader breeHeader) {
return breeHeader.getElements();
return toEEArray(breeHeader.getEnvironments().stream());
} else if (parent instanceof IManifestHeader header
&& Constants.REQUIRE_CAPABILITY.equals(header.getName())) {
Stream.Builder<String> eeRequirements = Stream.builder();
ExecutionEnvironmentSection.addRequiredEEs(header, eeRequirements);
return toEEArray(eeRequirements.build());
} else if (parent instanceof RequireBundleHeader requireBundleHeader) {
return requireBundleHeader.getRequiredBundles();
} else if (parent instanceof BundleClasspathHeader) {
Expand All @@ -117,6 +127,10 @@ public Object[] getChildren(Object parent) {
return new Object[0];
}

private Object[] toEEArray(Stream<String> stream) {
return stream.map(JavaRuntime.getExecutionEnvironmentsManager()::getEnvironment).toArray();
}

@Override
public boolean hasChildren(Object parent) {
return getChildren(parent).length > 0;
Expand Down Expand Up @@ -158,8 +172,8 @@ private class BundleLabelProvider extends LabelProvider {
public String getText(Object obj) {
if (obj instanceof PackageObject packageObject) {
return packageObject.getName();
} else if (obj instanceof ExecutionEnvironment ee) {
return ee.getName();
} else if (obj instanceof IExecutionEnvironment ee) {
return ee.getId();
} else if (obj instanceof RequireBundleObject requireBundle) {
return getTextRequireBundle(requireBundle);
} else if (obj instanceof ManifestHeader header) {
Expand Down Expand Up @@ -205,7 +219,7 @@ public Image getImage(Object obj) {
PDELabelProvider labelProvider = PDEPlugin.getDefault().getLabelProvider();
if (obj instanceof PackageObject) {
return labelProvider.get(PDEPluginImages.DESC_PACKAGE_OBJ);
} else if (obj instanceof ExecutionEnvironment) {
} else if (obj instanceof IExecutionEnvironment) {
return labelProvider.get(PDEPluginImages.DESC_JAVA_LIB_OBJ);
} else if (obj instanceof RequireBundleObject requireBundle) {
int flags = SharedLabelProvider.F_EXTERNAL;
Expand Down Expand Up @@ -405,7 +419,7 @@ private String getHeaderName(PDEManifestElement element) {
return Constants.EXPORT_PACKAGE;
} else if (element instanceof ImportPackageObject) {
return Constants.IMPORT_PACKAGE;
} else if (element instanceof ExecutionEnvironment) {
} else if (element instanceof ExecutionEnvironment || element instanceof IExecutionEnvironment) {
return Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT;

Check warning on line 423 in ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/BundleSourcePage.java

View check run for this annotation

Jenkins - eclipse-pde / Compiler and API Tools

Deprecation

NORMAL: The field Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT is deprecated
} else if (element instanceof RequireBundleObject) {
return Constants.REQUIRE_BUNDLE;
Expand Down Expand Up @@ -445,14 +459,60 @@ public IDocumentRange findRange() {
if (library.getPluginModel() instanceof IBundlePluginModelBase pluginModel) {
return getSpecificRange(pluginModel.getBundleModel(), Constants.BUNDLE_CLASSPATH, library.getName());
}
} else if (selection instanceof ExecutionEnvironment ee) {
return getSpecificRange(ee.getModel(), Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, ee.getName());
} else if (selection instanceof IExecutionEnvironment ee) {
if (getPluginModel() instanceof IBundlePluginModel bundlePlugin) {
return getEEsSpecificRange(ee, bundlePlugin.getBundleModel());
}
} else if (selection instanceof RequireBundleObject requiredBundle) {
return getSpecificRange(requiredBundle.getModel(), Constants.REQUIRE_BUNDLE, requiredBundle.getId());
}
return null;
}

private IDocumentRange getEEsSpecificRange(IExecutionEnvironment ee, IBundleModel bundleModel) {
IManifestHeader breeHeader = bundleModel.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);

Check warning on line 473 in ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/BundleSourcePage.java

View check run for this annotation

Jenkins - eclipse-pde / Compiler and API Tools

Deprecation

NORMAL: The field Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT is deprecated
IDocumentRange range = getSpecificRange(bundleModel, breeHeader, ee.getId());
if (range != null && range.getOffset() != breeHeader.getOffset()
&& range.getLength() != breeHeader.getName().length()) {
return range;
}
IManifestHeader requireCapaHeader = bundleModel.getBundle().getManifestHeader(Constants.REQUIRE_CAPABILITY);

String attribute = ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE;
IDocumentRange attributeRange = findAttributeRange(bundleModel, requireCapaHeader, attribute);
if (attributeRange != null) {
return attributeRange;
}
// Not found, just mark the (probably) originating header
IManifestHeader header = breeHeader != null ? breeHeader : requireCapaHeader;
return newDocumentRange(header.getOffset(), header.getName().length());
}

private IDocumentRange findAttributeRange(IBundleModel bundleModel, IManifestHeader header, String attribute) {
try {
int start = header.getOffset() + header.getName().length();
int length = header.getLength() - header.getName().length();
String headerValue = ((IEditingModel) bundleModel).getDocument().get(start, length);
int attributeStart = headerValue.indexOf(attribute);
if (attributeStart > -1) {
boolean inQotes = false;
int i = attributeStart + attribute.length();
for (; i < headerValue.length(); i++) {
char charAt = headerValue.charAt(i);
if (!inQotes && charAt == ',') {
break;
}
if (charAt == '"') {
inQotes = !inQotes;
}
}
return newDocumentRange(start + attributeStart, i - attributeStart - 1);
}
} catch (BadLocationException e) {
}
return null;
}

public static IDocumentRange getSpecificRange(IBundleModel model, IManifestHeader header, String element) {
if (header == null || !(model instanceof IEditingModel)) {
return null;
Expand Down Expand Up @@ -509,20 +569,24 @@ public static IDocumentRange getSpecificRange(IBundleModel model, IManifestHeade
// header value will be included in the selection
range[1] = header.getName().length();
}
return newDocumentRange(range[0], range[1]);
}

private static IDocumentRange newDocumentRange(int offset, int length) {
return new IDocumentRange() {
@Override
public int getOffset() {
return range[0];
return offset;
}

@Override
public int getLength() {
return range[1];
return length;
}
};
}

public static IDocumentRange getSpecificRange(IBundleModel model, String headerName, String search) {
private static IDocumentRange getSpecificRange(IBundleModel model, String headerName, String search) {
IManifestHeader header = model.getBundle().getManifestHeader(headerName);
return getSpecificRange(model, header, search);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
*******************************************************************************/
package org.eclipse.pde.internal.ui.editor.plugin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
Expand All @@ -40,6 +42,7 @@
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.pde.core.IModelChangedEvent;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
Expand All @@ -49,6 +52,7 @@
import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
import org.eclipse.pde.internal.core.text.bundle.ExecutionEnvironment;
import org.eclipse.pde.internal.core.text.bundle.RequiredExecutionEnvironmentHeader;
import org.eclipse.pde.internal.core.util.ManifestUtils;
import org.eclipse.pde.internal.ui.IHelpContextIds;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEPluginImages;
Expand All @@ -75,7 +79,10 @@
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.namespace.ExecutionEnvironmentNamespace;
import org.osgi.resource.Namespace;

public class ExecutionEnvironmentSection extends TableSection {

Expand Down Expand Up @@ -137,12 +144,7 @@ protected void createClient(Section section, FormToolkit toolkit) {
fEETable = tablePart.getTableViewer();
fEETable.setContentProvider((IStructuredContentProvider) inputElement -> {
if (inputElement instanceof IBundleModel model) {
IBundle bundle = model.getBundle();
@SuppressWarnings("deprecation")
IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
if (header instanceof RequiredExecutionEnvironmentHeader breeHeader) {
return breeHeader.getElements();
}
return getRequiredEEs(model.getBundle()).toArray();
}
return new Object[0];
});
Expand Down Expand Up @@ -275,8 +277,8 @@ private void handleRemove() {
IStructuredSelection ssel = fEETable.getStructuredSelection();
if (!ssel.isEmpty()) {
for (Object object : ssel) {
if (object instanceof ExecutionEnvironment ee) {
getHeader().removeExecutionEnvironment(ee.getName());
if (object instanceof IExecutionEnvironment ee) {
getHeader().removeExecutionEnvironment(ee.getId());
}
}
}
Expand All @@ -298,14 +300,8 @@ private void handleAdd() {

@SuppressWarnings("deprecation")
private void addExecutionEnvironments(Object[] result) {
List<String> ees = Arrays.stream(result).map(resultObject -> {
if (resultObject instanceof IExecutionEnvironment ee) {
return ee.getId();
} else if (resultObject instanceof ExecutionEnvironment ee) {
return ee.getName();
}
return null;
}).filter(Objects::nonNull).toList();
List<String> ees = Arrays.stream(result).filter(IExecutionEnvironment.class::isInstance)
.map(IExecutionEnvironment.class::cast).map(IExecutionEnvironment::getId).toList();

IManifestHeader header = getHeader();
if (header == null) {
Expand All @@ -326,14 +322,45 @@ private String getLineDelimiter() {
}

private IExecutionEnvironment[] getEnvironments() {
RequiredExecutionEnvironmentHeader header = getHeader();
IExecutionEnvironmentsManager eeManager = JavaRuntime.getExecutionEnvironmentsManager();
IExecutionEnvironment[] envs = eeManager.getExecutionEnvironments();
if (header == null) {
return envs;
IBundle bundle = getBundle();
if (bundle != null) {
List<IExecutionEnvironment> requiredEEs = getRequiredEEs(bundle);
if (!requiredEEs.isEmpty()) {
return Arrays.stream(envs).filter(ee -> !requiredEEs.contains(ee))
.toArray(IExecutionEnvironment[]::new);
}
}
return envs;
}

private List<IExecutionEnvironment> getRequiredEEs(IBundle bundle) {
List<String> requiredEEs = new ArrayList<>(1);
RequiredExecutionEnvironmentHeader breeHeader = getHeader();
if (breeHeader != null) {
requiredEEs.addAll(breeHeader.getEnvironments());
}
IManifestHeader requiredCapabilitiesHeader = bundle.getManifestHeader(Constants.REQUIRE_CAPABILITY);
if (requiredCapabilitiesHeader != null) {
addRequiredEEs(requiredCapabilitiesHeader, requiredEEs::add);
}
return requiredEEs.stream().map(JavaRuntime.getExecutionEnvironmentsManager()::getEnvironment).toList();
}

static void addRequiredEEs(IManifestHeader requiredCapabilitiesHeader, Consumer<String> eeCollector) {
String eeRequirement = requiredCapabilitiesHeader.getValue();
try {
ManifestElement[] required = ManifestElement.parseHeader(Constants.REQUIRE_CAPABILITY, eeRequirement);
for (ManifestElement requiredCapability : required) {
if (ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE.equals(requiredCapability.getValue())) {
String filter = requiredCapability.getDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
ManifestUtils.parseRequiredEEsFromFilter(filter, eeCollector);
}
}
} catch (BundleException e) {
ILog.get().error("Failed to parse " + Constants.REQUIRE_CAPABILITY + " header: " + eeRequirement, e); //$NON-NLS-1$//$NON-NLS-2$
}
List<IExecutionEnvironment> ees = header.getElementNames().stream().map(eeManager::getEnvironment).toList();
return Arrays.stream(envs).filter(ee -> !ees.contains(ee)).toArray(IExecutionEnvironment[]::new);
}

@Override
Expand Down

0 comments on commit 7ddc4e9

Please sign in to comment.