diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/MinimalState.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/MinimalState.java index 4265376fff..3ddb65ffff 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/MinimalState.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/MinimalState.java @@ -20,8 +20,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.Dictionary; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; @@ -71,9 +73,8 @@ public class MinimalState { // this could be due to the system bundle changing location // or initially when the ee list is first created. - private String[] fExecutionEnvironments; // an ordered list of - // known/supported execution - // environments + /** ordered set of known/supported execution environments */ + private Set fExecutionEnvironments; private boolean fNoProfile; @@ -365,19 +366,24 @@ public State getState() { } private void setExecutionEnvironments() { - String[] knownExecutionEnviroments = TargetPlatformHelper.getKnownExecutionEnvironments(); - if (knownExecutionEnviroments.length == 0) { + List knownExecutionEnviroments = TargetPlatformHelper.getKnownExecutionEnvironments(); + if (knownExecutionEnviroments.isEmpty()) { String jreProfile = System.getProperty("pde.jreProfile"); //$NON-NLS-1$ if (jreProfile != null && !jreProfile.isEmpty() && "none".equals(jreProfile)) { //$NON-NLS-1$ fNoProfile = true; } } if (!fNoProfile) { - fExecutionEnvironments = knownExecutionEnviroments; + fExecutionEnvironments = Collections.unmodifiableSet(new LinkedHashSet<>(knownExecutionEnviroments)); } fEEListChanged = true; // always indicate the list has changed } + /** Returns an ordered Set of known/supported execution environments */ + public Set getfProvidedExecutionEnvironments() { + return fExecutionEnvironments; // TODO: use SequencedSet once available + } + public void addBundleDescription(BundleDescription toAdd) { if (toAdd != null) { fState.addBundle(toAdd); diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TargetPlatformHelper.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TargetPlatformHelper.java index 11a270f95b..0503431f52 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TargetPlatformHelper.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/TargetPlatformHelper.java @@ -22,6 +22,7 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Dictionary; import java.util.HashMap; import java.util.Hashtable; @@ -352,8 +353,8 @@ public static Dictionary getTargetEnvironment(MinimalState state } @SuppressWarnings("unchecked") - public static Dictionary[] getPlatformProperties(String[] profilesArr, MinimalState state) { - List profiles = profilesArr != null ? Arrays.asList(profilesArr) : List.of(); + public static Dictionary[] getPlatformProperties(Set profilesArr, MinimalState state) { + Collection profiles = profilesArr != null ? profilesArr : List.of(); // add java profiles for those EE's that have a .profile file in the // current system bundle List> result = new ArrayList<>(profiles.size()); @@ -392,20 +393,16 @@ public static String getSystemPackages(IExecutionEnvironment environment, Proper return org.eclipse.pde.internal.build.site.PDEState.getSystemPackages(environment, profileProperties); } - public static String[] getKnownExecutionEnvironments() { + public static List getKnownExecutionEnvironments() { String jreProfile = System.getProperty("pde.jreProfile"); //$NON-NLS-1$ if (jreProfile != null && jreProfile.length() > 0) { if ("none".equals(jreProfile)) { //$NON-NLS-1$ - return new String[0]; + return List.of(); } - return new String[] { jreProfile }; + return List.of(jreProfile); } IExecutionEnvironment[] environments = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments(); - String[] ids = new String[environments.length]; - for (int i = 0; i < environments.length; i++) { - ids[i] = environments[i].getId(); - } - return ids; + return Arrays.stream(environments).map(IExecutionEnvironment::getId).toList(); } /** diff --git a/ui/org.eclipse.pde.core/text/org/eclipse/pde/internal/core/text/bundle/RequiredExecutionEnvironmentHeader.java b/ui/org.eclipse.pde.core/text/org/eclipse/pde/internal/core/text/bundle/RequiredExecutionEnvironmentHeader.java index 66f1f2a905..8416dce7c2 100644 --- a/ui/org.eclipse.pde.core/text/org/eclipse/pde/internal/core/text/bundle/RequiredExecutionEnvironmentHeader.java +++ b/ui/org.eclipse.pde.core/text/org/eclipse/pde/internal/core/text/bundle/RequiredExecutionEnvironmentHeader.java @@ -14,6 +14,7 @@ package org.eclipse.pde.internal.core.text.bundle; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.eclipse.jdt.launching.environments.IExecutionEnvironment; @@ -33,8 +34,8 @@ protected PDEManifestElement createElement(ManifestElement element) { return new ExecutionEnvironment(this, element.getValue()); } - public void addExecutionEnvironment(IExecutionEnvironment env) { - addManifestElement(new ExecutionEnvironment(this, env.getId())); + public void addExecutionEnvironment(String eeId) { + addManifestElement(new ExecutionEnvironment(this, eeId)); } public void addExecutionEnvironment(ExecutionEnvironment environment, int index) { @@ -60,8 +61,8 @@ public void addExecutionEnvironments(Object[] envs) { } } - public ExecutionEnvironment removeExecutionEnvironment(ExecutionEnvironment env) { - return (ExecutionEnvironment) removeManifestElement(env); + public ExecutionEnvironment removeExecutionEnvironment(String eeId) { + return (ExecutionEnvironment) removeManifestElement(eeId); } /** @@ -71,11 +72,9 @@ public ExecutionEnvironment removeExecutionEnvironmentUnique(ExecutionEnvironmen return (ExecutionEnvironment) removeManifestElement(environment, true); } - public ExecutionEnvironment[] getEnvironments() { + public List getEnvironments() { PDEManifestElement[] elements = getElements(); - ExecutionEnvironment[] result = new ExecutionEnvironment[elements.length]; - System.arraycopy(elements, 0, result, 0, elements.length); - return result; + return Arrays.stream(elements).map(PDEManifestElement::getValue).toList(); } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/model/bundle/ExecutionEnvironmentTestCase.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/model/bundle/ExecutionEnvironmentTestCase.java index 9c8d53ecfa..c5339cb56e 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/model/bundle/ExecutionEnvironmentTestCase.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/model/bundle/ExecutionEnvironmentTestCase.java @@ -16,11 +16,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import org.eclipse.jdt.launching.JavaRuntime; -import org.eclipse.jdt.launching.environments.IExecutionEnvironment; -import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; +import java.util.List; + 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.text.edits.TextEdit; import org.junit.Test; @@ -64,11 +62,11 @@ public void testRemoveExistingExecutionEnvironment() throws Exception { buffer.append("Bundle-RequiredExecutionEnvironment: J2SE-1.4\n"); fDocument.set(buffer.toString()); load(true); - IManifestHeader header = fModel.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); + RequiredExecutionEnvironmentHeader header = getRequiredExecutionEnvironmentHeader(); assertNotNull(header); - ExecutionEnvironment env = ((RequiredExecutionEnvironmentHeader) header).getEnvironments()[0]; - ((RequiredExecutionEnvironmentHeader) header).removeExecutionEnvironment(env); + String env = header.getEnvironments().get(0); + header.removeExecutionEnvironment(env); TextEdit[] ops = fListener.getTextOperations(); assertEquals(1, ops.length); @@ -87,9 +85,8 @@ public void testAddExecutionEnvironment() throws Exception { fDocument.set(buffer.toString()); load(true); - IManifestHeader header = fModel.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); - IExecutionEnvironment env = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("J2SE-1.5"); - ((RequiredExecutionEnvironmentHeader) header).addExecutionEnvironment(env); + RequiredExecutionEnvironmentHeader header = getRequiredExecutionEnvironmentHeader(); + header.addExecutionEnvironment("J2SE-1.5"); TextEdit[] ops = fListener.getTextOperations(); assertEquals(1, ops.length); @@ -114,14 +111,10 @@ public void testAddMulitplieExecutionEnvironmnets() throws Exception { buffer.append("Bundle-RequiredExecutionEnvironment: J2SE-1.4\n"); fDocument.set(buffer.toString()); load(true); - IManifestHeader header = fModel.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); - IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); - IExecutionEnvironment env1 = manager.getEnvironment("CDC-1.1/Foundation-1.1"); - IExecutionEnvironment env2 = manager.getEnvironment("J2SE-1.5"); - IExecutionEnvironment env3 = manager.getEnvironment("OSGi/Minimum-1.1"); - ((RequiredExecutionEnvironmentHeader) header).addExecutionEnvironment(env1); - ((RequiredExecutionEnvironmentHeader) header).addExecutionEnvironment(env2); - ((RequiredExecutionEnvironmentHeader) header).addExecutionEnvironment(env3); + RequiredExecutionEnvironmentHeader header = getRequiredExecutionEnvironmentHeader(); + header.addExecutionEnvironment("CDC-1.1/Foundation-1.1"); + header.addExecutionEnvironment("J2SE-1.5"); + header.addExecutionEnvironment("OSGi/Minimum-1.1"); TextEdit[] ops = fListener.getTextOperations(); assertEquals(1, ops.length); @@ -150,9 +143,9 @@ public void testRemoveExecutionEnvironment() throws Exception { buffer.append(" OSGi/Minimum-1.1\n"); fDocument.set(buffer.toString()); load(true); - IManifestHeader header = fModel.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); - ExecutionEnvironment env = ((RequiredExecutionEnvironmentHeader) header).getEnvironments()[1]; - ((RequiredExecutionEnvironmentHeader) header).removeExecutionEnvironment(env); + RequiredExecutionEnvironmentHeader header = getRequiredExecutionEnvironmentHeader(); + String env = header.getEnvironments().get(1); + header.removeExecutionEnvironment(env); TextEdit[] ops = fListener.getTextOperations(); assertEquals(1, ops.length); @@ -179,10 +172,10 @@ public void testRemoveMultipleExecutionEnvironments() throws Exception { buffer.append(" OSGi/Minimum-1.1\n"); fDocument.set(buffer.toString()); load(true); - IManifestHeader header = fModel.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); - ExecutionEnvironment[] envs = ((RequiredExecutionEnvironmentHeader) header).getEnvironments(); - ((RequiredExecutionEnvironmentHeader) header).removeExecutionEnvironment(envs[1]); - ((RequiredExecutionEnvironmentHeader) header).removeExecutionEnvironment(envs[0]); + RequiredExecutionEnvironmentHeader header = getRequiredExecutionEnvironmentHeader(); + List envs = header.getEnvironments(); + header.removeExecutionEnvironment(envs.get(1)); + header.removeExecutionEnvironment(envs.get(0)); TextEdit[] ops = fListener.getTextOperations(); assertEquals(1, ops.length); @@ -207,9 +200,8 @@ public void testPreserveSpacing() throws Exception { fDocument.set(buffer.toString()); load(true); - IManifestHeader header = fModel.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); - IExecutionEnvironment env = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("OSGi/Minimum-1.1"); - ((RequiredExecutionEnvironmentHeader) header).addExecutionEnvironment(env); + RequiredExecutionEnvironmentHeader header = getRequiredExecutionEnvironmentHeader(); + header.addExecutionEnvironment("OSGi/Minimum-1.1"); TextEdit[] ops = fListener.getTextOperations(); assertEquals(1, ops.length); @@ -224,7 +216,11 @@ public void testPreserveSpacing() throws Exception { expected.append(" J2SE-1.4,\n"); expected.append(" OSGi/Minimum-1.1\n"); assertEquals(expected.toString(), fDocument.get(pos, length)); + } + private RequiredExecutionEnvironmentHeader getRequiredExecutionEnvironmentHeader() { + return (RequiredExecutionEnvironmentHeader) fModel.getBundle() + .getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java index 689d76ffba..89f6f5775f 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java @@ -22,7 +22,6 @@ import org.eclipse.pde.core.target.ITargetPlatformService; import org.eclipse.pde.core.target.LoadTargetDefinitionJob; import org.eclipse.pde.internal.core.PDECore; -import org.eclipse.pde.internal.core.TargetPlatformHelper; import org.eclipse.pde.ui.tests.PDETestCase; import org.eclipse.test.performance.Dimension; import org.eclipse.test.performance.PerformanceTestCase; @@ -37,7 +36,6 @@ public class InitializeModelsPerfTest extends PerformanceTestCase { protected void setUp() throws Exception { super.setUp(); PDETestCase.delete(PDECore.getDefault().getStateLocation().toFile()); - TargetPlatformHelper.getKnownExecutionEnvironments(); } public void testModels() throws Exception { diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetEnvironmentTestCase.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetEnvironmentTestCase.java index 36aefd9186..004a026b63 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetEnvironmentTestCase.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetEnvironmentTestCase.java @@ -138,7 +138,7 @@ public void testResolveOptional() { public void testStateEEProperties() { Dictionary[] platformProps = TargetPlatformHelper.getState().getPlatformProperties(); - String[] profiles = TargetPlatformHelper.getKnownExecutionEnvironments(); + List profiles = TargetPlatformHelper.getKnownExecutionEnvironments(); for (String profile : profiles) { IExecutionEnvironment environment = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(profile); if (environment != null) { diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddDefaultExecutionEnvironmentResolution.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddDefaultExecutionEnvironmentResolution.java index 628c3ac7c0..df65e9006e 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddDefaultExecutionEnvironmentResolution.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddDefaultExecutionEnvironmentResolution.java @@ -15,8 +15,6 @@ package org.eclipse.pde.internal.ui.correction; import org.eclipse.core.resources.IMarker; -import org.eclipse.jdt.launching.JavaRuntime; -import org.eclipse.jdt.launching.environments.IExecutionEnvironment; import org.eclipse.osgi.util.NLS; import org.eclipse.pde.internal.core.ibundle.IManifestHeader; import org.eclipse.pde.internal.core.text.bundle.BundleModel; @@ -26,11 +24,8 @@ public class AddDefaultExecutionEnvironmentResolution extends AbstractManifestMarkerResolution { - - public AddDefaultExecutionEnvironmentResolution(int type, IMarker marker) { super(type, marker); - } @Override @@ -46,9 +41,8 @@ protected void createChange(BundleModel model) { // Get header header = model.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); - if (header != null && header instanceof RequiredExecutionEnvironmentHeader) { - IExecutionEnvironment ee = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(id); - ((RequiredExecutionEnvironmentHeader) header).addExecutionEnvironment(ee); + if (header instanceof RequiredExecutionEnvironmentHeader breeHeader) { + breeHeader.addExecutionEnvironment(id); } } diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/RemoveUnknownExecEnvironments.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/RemoveUnknownExecEnvironments.java index 66728165d1..4aa5e19b53 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/RemoveUnknownExecEnvironments.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/RemoveUnknownExecEnvironments.java @@ -13,12 +13,12 @@ *******************************************************************************/ package org.eclipse.pde.internal.ui.correction; +import java.util.Set; + import org.eclipse.core.resources.IMarker; -import org.eclipse.jdt.launching.JavaRuntime; -import org.eclipse.jdt.launching.environments.IExecutionEnvironment; +import org.eclipse.pde.internal.core.TargetPlatformHelper; import org.eclipse.pde.internal.core.ibundle.IManifestHeader; import org.eclipse.pde.internal.core.text.bundle.BundleModel; -import org.eclipse.pde.internal.core.text.bundle.ExecutionEnvironment; import org.eclipse.pde.internal.core.text.bundle.RequiredExecutionEnvironmentHeader; import org.eclipse.pde.internal.ui.PDEUIMessages; import org.osgi.framework.Constants; @@ -33,18 +33,11 @@ public RemoveUnknownExecEnvironments(int type, IMarker marker) { protected void createChange(BundleModel model) { IManifestHeader header = model.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); if (header instanceof RequiredExecutionEnvironmentHeader reqHeader) { - ExecutionEnvironment[] bundleEnvs = reqHeader.getEnvironments(); - IExecutionEnvironment[] systemEnvs = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments(); - for (ExecutionEnvironment bundleEnv : bundleEnvs) { - boolean found = false; - for (IExecutionEnvironment systemEnv : systemEnvs) { - if (bundleEnv.getName().equals(systemEnv.getId())) { - found = true; - break; - } + Set systemEnvs = TargetPlatformHelper.getPDEState().getfProvidedExecutionEnvironments(); + for (String ee : reqHeader.getEnvironments()) { + if (!systemEnvs.contains(ee)) { + reqHeader.removeExecutionEnvironment(ee); } - if (!found) - reqHeader.removeExecutionEnvironment(bundleEnv); } } } diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/ReplaceExecEnvironment.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/ReplaceExecEnvironment.java index ef1a22ccff..0c380bef14 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/ReplaceExecEnvironment.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/ReplaceExecEnvironment.java @@ -13,20 +13,20 @@ *******************************************************************************/ package org.eclipse.pde.internal.ui.correction; +import java.util.Set; + import org.eclipse.core.resources.IMarker; -import org.eclipse.jdt.launching.JavaRuntime; -import org.eclipse.jdt.launching.environments.IExecutionEnvironment; import org.eclipse.osgi.util.NLS; +import org.eclipse.pde.internal.core.TargetPlatformHelper; import org.eclipse.pde.internal.core.builders.PDEMarkerFactory; import org.eclipse.pde.internal.core.ibundle.IManifestHeader; import org.eclipse.pde.internal.core.text.bundle.BundleModel; -import org.eclipse.pde.internal.core.text.bundle.ExecutionEnvironment; import org.eclipse.pde.internal.core.text.bundle.RequiredExecutionEnvironmentHeader; import org.osgi.framework.Constants; public class ReplaceExecEnvironment extends AbstractManifestMarkerResolution { - String fRequiredEE; + private final String fRequiredEE; public ReplaceExecEnvironment(int type, IMarker marker) { super(type, marker); @@ -40,23 +40,17 @@ public String getLabel() { @Override protected void createChange(BundleModel model) { + @SuppressWarnings("deprecation") IManifestHeader header = model.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); if (header instanceof RequiredExecutionEnvironmentHeader reqHeader) { - ExecutionEnvironment[] bundleEnvs = reqHeader.getEnvironments(); - IExecutionEnvironment[] systemEnvs = JavaRuntime.getExecutionEnvironmentsManager() - .getExecutionEnvironments(); - - for (IExecutionEnvironment systemEnv : systemEnvs) { - if (systemEnv.getId().equals(fRequiredEE)) { - for (ExecutionEnvironment bundleEnv : bundleEnvs) { - reqHeader.removeExecutionEnvironment(bundleEnv); - } - reqHeader.addExecutionEnvironment(systemEnv); + Set systemEnvs = TargetPlatformHelper.getPDEState().getfProvidedExecutionEnvironments(); + if (systemEnvs.contains(fRequiredEE)) { + for (String ee : reqHeader.getEnvironments()) { + reqHeader.removeExecutionEnvironment(ee); } - + reqHeader.addExecutionEnvironment(fRequiredEE); } } - } } diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/BundleSourcePage.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/BundleSourcePage.java index e8e183f7ea..da55a087a2 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/BundleSourcePage.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/BundleSourcePage.java @@ -16,7 +16,6 @@ *******************************************************************************/ package org.eclipse.pde.internal.ui.editor.plugin; -import java.util.ArrayList; import java.util.Map; import org.eclipse.core.runtime.CoreException; @@ -74,7 +73,6 @@ import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; -import org.eclipse.ui.forms.editor.FormEditor; import org.osgi.framework.Constants; public class BundleSourcePage extends KeyValueSourcePage { @@ -100,36 +98,25 @@ public class BundleSourcePage extends KeyValueSourcePage { private PDERefactoringAction fRenameAction; - /** - * BundleOutlineContentProvider - */ private class BundleOutlineContentProvider implements ITreeContentProvider { @Override public Object[] getChildren(Object parent) { // Need an identifying class for label provider - if (parent instanceof ImportPackageHeader) { - return ((ImportPackageHeader) parent).getPackages(); - } else if (parent instanceof ExportPackageHeader) { - return ((ExportPackageHeader) parent).getPackages(); - } else if (parent instanceof RequiredExecutionEnvironmentHeader) { - return ((RequiredExecutionEnvironmentHeader) parent).getEnvironments(); - } else if (parent instanceof RequireBundleHeader) { - return ((RequireBundleHeader) parent).getRequiredBundles(); + if (parent instanceof ImportPackageHeader importHeader) { + return importHeader.getPackages(); + } else if (parent instanceof ExportPackageHeader exportHeader) { + return exportHeader.getPackages(); + } else if (parent instanceof RequiredExecutionEnvironmentHeader breeHeader) { + return breeHeader.getElements(); + } else if (parent instanceof RequireBundleHeader requireBundleHeader) { + return requireBundleHeader.getRequiredBundles(); } else if (parent instanceof BundleClasspathHeader) { - return getPluginLibraries(); + return getBundleClasspathLibraries(); } return new Object[0]; } - private Object[] getPluginLibraries() { - IPluginLibrary[] libraries = getBundleClasspathLibraries(); - if ((libraries == null) || (libraries.length == 0)) { - return new Object[0]; - } - return libraries; - } - @Override public boolean hasChildren(Object parent) { return getChildren(parent).length > 0; @@ -144,13 +131,7 @@ public Object getParent(Object child) { public Object[] getElements(Object parent) { if (parent instanceof BundleModel model) { Map manifest = ((Bundle) model.getBundle()).getHeaders(); - ArrayList keys = new ArrayList<>(); - for (IManifestHeader header : manifest.values()) { - if (header.getOffset() > -1) { - keys.add(header); - } - } - return keys.toArray(); + return manifest.values().stream().filter(header -> header.getOffset() > -1).toArray(); } return new Object[0]; } @@ -159,13 +140,14 @@ public Object[] getElements(Object parent) { private IPluginLibrary[] getBundleClasspathLibraries() { // The bundle classpath header has no model data members // Retrieve the plug-in library equivalents from the editor model - FormEditor editor = getEditor(); - if (editor instanceof PDEFormEditor formEditor) { - IBaseModel baseModel = formEditor.getAggregateModel(); - if (baseModel instanceof IPluginModelBase) { - IPluginLibrary[] libraries = ((IPluginModelBase) baseModel).getPluginBase().getLibraries(); - return libraries; - } + IPluginModelBase pluginModel = getPluginModel(); + return pluginModel != null ? pluginModel.getPluginBase().getLibraries() : new IPluginLibrary[0]; + } + + private IPluginModelBase getPluginModel() { + if (getEditor() instanceof PDEFormEditor formEditor + && formEditor.getAggregateModel() instanceof IPluginModelBase pluginModel) { + return pluginModel; } return null; } @@ -174,14 +156,14 @@ private class BundleLabelProvider extends LabelProvider { // TODO: MP: QO: LOW: Move to PDELabelProvider @Override public String getText(Object obj) { - if (obj instanceof PackageObject) { - return ((PackageObject) obj).getName(); - } else if (obj instanceof ExecutionEnvironment) { - return ((ExecutionEnvironment) obj).getName(); - } else if (obj instanceof RequireBundleObject) { - return getTextRequireBundle(((RequireBundleObject) obj)); - } else if (obj instanceof ManifestHeader) { - return ((ManifestHeader) obj).getName(); + if (obj instanceof PackageObject packageObject) { + return packageObject.getName(); + } else if (obj instanceof ExecutionEnvironment ee) { + return ee.getName(); + } else if (obj instanceof RequireBundleObject requireBundle) { + return getTextRequireBundle(requireBundle); + } else if (obj instanceof ManifestHeader header) { + return header.getName(); } return super.getText(obj); } @@ -225,9 +207,9 @@ public Image getImage(Object obj) { return labelProvider.get(PDEPluginImages.DESC_PACKAGE_OBJ); } else if (obj instanceof ExecutionEnvironment) { return labelProvider.get(PDEPluginImages.DESC_JAVA_LIB_OBJ); - } else if (obj instanceof RequireBundleObject) { + } else if (obj instanceof RequireBundleObject requireBundle) { int flags = SharedLabelProvider.F_EXTERNAL; - if (((RequireBundleObject) obj).isReexported()) { + if (requireBundle.isReexported()) { flags = flags | SharedLabelProvider.F_EXPORT; } return labelProvider.get(PDEPluginImages.DESC_REQ_PLUGIN_OBJ, flags); @@ -319,16 +301,9 @@ public IDocumentRange getRangeElement(int offset, boolean searchChildren) { * @return true if the offset is within the range; false, otherwise */ private boolean isWithinCurrentRange(int offset, IDocumentRange range) { - - if (range == null) { - // Range not set - return false; - } else if (offset >= range.getOffset() && (offset <= (range.getOffset() + range.getLength()))) { - // Offset within range - return true; - } - // Offset not within range - return false; + return range != null // + && offset >= range.getOffset() // + && (offset <= range.getOffset() + range.getLength()); } /** @@ -338,26 +313,13 @@ private boolean isWithinCurrentRange(int offset, IDocumentRange range) { * and before the current range (e.g. the previous ranges parameters) */ private boolean isWithinPreviousRange(int offset, IDocumentRange current_range, IDocumentRange previous_range) { - - if ((current_range == null) || (previous_range == null)) { - // Range not set - return false; - } else if ((offset >= previous_range.getOffset() + previous_range.getLength()) && ((offset <= current_range.getOffset()))) { - // Offset within range - return true; - } - // Offset not within range - return false; + return current_range != null && previous_range != null + && (offset >= previous_range.getOffset() + previous_range.getLength()) + && offset <= current_range.getOffset(); } private boolean isBeforePreviousRange(int offset, IDocumentRange previousRange) { - - if (previousRange == null) { - return false; - } else if (offset < previousRange.getOffset()) { - return true; - } - return false; + return previousRange != null && offset < previousRange.getOffset(); } private IDocumentRange getRangeElementChild(IBundleModel model, int offset, CompositeManifestHeader header) { @@ -406,12 +368,9 @@ private IDocumentRange getRangeElementChild(IBundleModel model, int offset, Comp } private boolean isWithinLastElementParamRange(int offset, IDocumentRange currentRange, IDocumentRange headerRange) { - if (currentRange == null) { - return false; - } else if ((offset >= currentRange.getOffset() + currentRange.getLength()) && (offset <= (headerRange.getOffset() + headerRange.getLength()))) { - return true; - } - return false; + return currentRange != null // + && (offset >= currentRange.getOffset() + currentRange.getLength()) + && (offset <= (headerRange.getOffset() + headerRange.getLength())); } private void setChildTargetOutlineSelection(String headerName, PDEManifestElement element) { @@ -429,12 +388,7 @@ private void setChildTargetOutlineSelection(String headerName, PDEManifestElemen * objects */ private Object getBundleClasspathOutlineSelection(PDEManifestElement manifestElement) { - IPluginLibrary[] libraries = getBundleClasspathLibraries(); - // Ensure there are libraries - if ((libraries == null) || (libraries.length == 0)) { - return null; - } // Linearly search for the equivalent library object for (IPluginLibrary library : libraries) { if (manifestElement.getValue().equals(library.getName())) { @@ -479,30 +433,30 @@ public IDocumentRange findRange() { Object selection = getSelection(); - if (selection instanceof ImportObject) { - IPluginModelBase base = ((ImportObject) selection).getImport().getPluginModel(); - if (base instanceof IBundlePluginModelBase) - return getSpecificRange(((IBundlePluginModelBase) base).getBundleModel(), Constants.REQUIRE_BUNDLE, ((ImportObject) selection).getId()); - } else if (selection instanceof ImportPackageObject) { - return getSpecificRange(((ImportPackageObject) selection).getModel(), Constants.IMPORT_PACKAGE, ((ImportPackageObject) selection).getValue()); - } else if (selection instanceof ExportPackageObject) { - return getSpecificRange(((ExportPackageObject) selection).getModel(), Constants.EXPORT_PACKAGE, ((ExportPackageObject) selection).getValue()); - } else if (selection instanceof IPluginLibrary) { - IPluginModelBase base = ((IPluginLibrary) selection).getPluginModel(); - if (base instanceof IBundlePluginModelBase) - return getSpecificRange(((IBundlePluginModelBase) base).getBundleModel(), Constants.BUNDLE_CLASSPATH, ((IPluginLibrary) selection).getName()); - } else if (selection instanceof ExecutionEnvironment) { - return getSpecificRange(((ExecutionEnvironment) selection).getModel(), Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, ((ExecutionEnvironment) selection).getValue()); - } else if (selection instanceof RequireBundleObject) { - return getSpecificRange(((RequireBundleObject) selection).getModel(), Constants.REQUIRE_BUNDLE, ((RequireBundleObject) selection).getId()); + if (selection instanceof ImportObject importObject) { + if (importObject.getImport().getPluginModel() instanceof IBundlePluginModelBase pluginModel) { + return getSpecificRange(pluginModel.getBundleModel(), Constants.REQUIRE_BUNDLE, importObject.getId()); + } + } else if (selection instanceof ImportPackageObject packageImport) { + return getSpecificRange(packageImport.getModel(), Constants.IMPORT_PACKAGE, packageImport.getValue()); + } else if (selection instanceof ExportPackageObject packageExport) { + return getSpecificRange(packageExport.getModel(), Constants.EXPORT_PACKAGE, packageExport.getValue()); + } else if (selection instanceof IPluginLibrary library) { + 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 RequireBundleObject requiredBundle) { + return getSpecificRange(requiredBundle.getModel(), Constants.REQUIRE_BUNDLE, requiredBundle.getId()); } return null; } public static IDocumentRange getSpecificRange(IBundleModel model, IManifestHeader header, String element) { - if (header == null || !(model instanceof IEditingModel)) + if (header == null || !(model instanceof IEditingModel)) { return null; - + } final int[] range = new int[] {-1, -1}; // { offset, length } try { int start = header.getOffset() + header.getName().length(); @@ -578,11 +532,11 @@ protected boolean isSelectionListener() { return true; } - @SuppressWarnings("unchecked") @Override public T getAdapter(Class adapter) { - if (IHyperlinkDetector.class.equals(adapter)) - return (T) new BundleHyperlinkDetector(this); + if (IHyperlinkDetector.class.equals(adapter)) { + return adapter.cast(new BundleHyperlinkDetector(this)); + } return super.getAdapter(adapter); } @@ -593,9 +547,9 @@ public void updateSelection(Object object) { setSelectedObject(object); // Highlight the selection if it is a manifest header - if (object instanceof IDocumentKey) { - setHighlightRange((IDocumentKey) object); - setCurrentHighlightRangeOffset(((IDocumentKey) object).getOffset()); + if (object instanceof IDocumentKey key) { + setHighlightRange(key); + setCurrentHighlightRangeOffset(key.getOffset()); // We don't set the selected range because it will cause the // manifest header and all its value to be selected return; @@ -610,14 +564,14 @@ public void updateSelection(Object object) { // Get the model IBaseModel model = getInputContext().getModel(); // Ensure we have an editing model - if ((model instanceof AbstractEditingModel) == false) { + if (!(model instanceof AbstractEditingModel editingModel)) { return; } // If the range offset is undefined or the source viewer is dirty, // forcibly adjust the offsets and try to find the range again if ((range.getOffset() == -1) || isDirty()) { try { - ((AbstractEditingModel) model).adjustOffsets(((AbstractEditingModel) model).getDocument()); + editingModel.adjustOffsets(editingModel.getDocument()); } catch (CoreException e) { // Ignore } @@ -634,21 +588,21 @@ protected void handleSelectionChangedSourcePage(SelectionChangedEvent event) { super.handleSelectionChangedSourcePage(event); ISelection selection = event.getSelection(); // Ensure we have a selection - if (selection.isEmpty() || ((selection instanceof ITextSelection) == false)) { + if (selection.isEmpty() || !(selection instanceof ITextSelection textSelection)) { return; } // If the page has been edited, adjust the offsets; otherwise, our // caculated ranges will be out of sync IBaseModel model = getInputContext().getModel(); - if (model instanceof AbstractEditingModel && isDirty()) { + if (model instanceof AbstractEditingModel editingModel && isDirty()) { try { - ((AbstractEditingModel) model).adjustOffsets(((AbstractEditingModel) model).getDocument()); + editingModel.adjustOffsets(editingModel.getDocument()); } catch (CoreException e) { // Ignore } } // Synchronize using the current cursor position in this page - synchronizeOutlinePage(((ITextSelection) selection).getOffset()); + synchronizeOutlinePage(textSelection.getOffset()); } @Override diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/ExecutionEnvironmentSection.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/ExecutionEnvironmentSection.java index ebbb34e067..35595d1d7c 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/ExecutionEnvironmentSection.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/plugin/ExecutionEnvironmentSection.java @@ -141,7 +141,7 @@ protected void createClient(Section section, FormToolkit toolkit) { @SuppressWarnings("deprecation") IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); if (header instanceof RequiredExecutionEnvironmentHeader breeHeader) { - return breeHeader.getEnvironments(); + return breeHeader.getElements(); } } return new Object[0]; @@ -275,8 +275,8 @@ private void handleRemove() { IStructuredSelection ssel = fEETable.getStructuredSelection(); if (!ssel.isEmpty()) { for (Object object : ssel) { - if (object instanceof ExecutionEnvironment) { - getHeader().removeExecutionEnvironment((ExecutionEnvironment) object); + if (object instanceof ExecutionEnvironment ee) { + getHeader().removeExecutionEnvironment(ee.getName()); } } }