Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider EE requirements in EE-Section of ManifestEditor #1318

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -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 @@ -18,7 +18,10 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.pde.internal.core.util.VMUtil;
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.TransferData;

Expand Down Expand Up @@ -50,12 +53,12 @@ public ModelDataTransfer() {

@Override
protected int[] getTypeIds() {
return new int[] {TYPEID};
return new int[] { TYPEID };
}

@Override
protected String[] getTypeNames() {
return new String[] {TYPE_NAME};
return new String[] { TYPE_NAME };
}

@Override
Expand All @@ -72,6 +75,9 @@ protected void javaToNative(Object data, TransferData transferData) {

//write each object
for (Object object : objects) {
if (object instanceof IExecutionEnvironment ee) {
object = new EESerializationSurogate(ee.getId());
}
objectOut.writeObject(object);
}

Expand All @@ -87,17 +93,28 @@ protected void javaToNative(Object data, TransferData transferData) {
@Override
protected Object nativeToJava(TransferData transferData) {
byte[] bytes = (byte[]) super.nativeToJava(transferData);
if (bytes == null)
if (bytes == null) {
return null;
}
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
int count = in.readInt();
Object[] objects = new Object[count];
for (int i = 0; i < count; i++) {
objects[i] = in.readObject();
Object object = in.readObject();
if (object instanceof EESerializationSurogate ee) {
object = VMUtil.getExecutionEnvironment(ee.eeId());
}
objects[i] = object;
}
return objects;
} catch (ClassNotFoundException | IOException e) {
return null;
}
}

private record EESerializationSurogate(String eeId) implements Serializable {
// JDT doesn't want to make IExecutionEnvironment serializable, so we
// need special handling in PDE:
// https://github.com/eclipse-jdt/eclipse.jdt.debug/pull/456
}
}
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 @@
} 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 @@
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 @@
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 @@
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,8 +419,8 @@
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;
} else {
Expand Down Expand Up @@ -445,14 +459,60 @@
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 @@
// 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
Loading
Loading