Skip to content

Commit

Permalink
Reduce indirection via (I)ExecutionEnvironment in BREEHeader handling
Browse files Browse the repository at this point in the history
And apply some minor general code clean-ups.
  • Loading branch information
HannesWell committed Jul 6, 2024
1 parent f931d18 commit de33737
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> fExecutionEnvironments;

private boolean fNoProfile;

Expand Down Expand Up @@ -365,19 +366,24 @@ public State getState() {
}

private void setExecutionEnvironments() {
String[] knownExecutionEnviroments = TargetPlatformHelper.getKnownExecutionEnvironments();
if (knownExecutionEnviroments.length == 0) {
List<String> 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<String> getfProvidedExecutionEnvironments() {
return fExecutionEnvironments; // TODO: use SequencedSet once available
}

public void addBundleDescription(BundleDescription toAdd) {
if (toAdd != null) {
fState.addBundle(toAdd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -352,8 +353,8 @@ public static Dictionary<String, String> getTargetEnvironment(MinimalState state
}

@SuppressWarnings("unchecked")
public static Dictionary<String, String>[] getPlatformProperties(String[] profilesArr, MinimalState state) {
List<String> profiles = profilesArr != null ? Arrays.asList(profilesArr) : List.of();
public static Dictionary<String, String>[] getPlatformProperties(Set<String> profilesArr, MinimalState state) {
Collection<String> profiles = profilesArr != null ? profilesArr : List.of();
// add java profiles for those EE's that have a .profile file in the
// current system bundle
List<Dictionary<String, String>> result = new ArrayList<>(profiles.size());
Expand Down Expand Up @@ -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<String> 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand All @@ -71,11 +72,9 @@ public ExecutionEnvironment removeExecutionEnvironmentUnique(ExecutionEnvironmen
return (ExecutionEnvironment) removeManifestElement(environment, true);
}

public ExecutionEnvironment[] getEnvironments() {
public List<String> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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<String> envs = header.getEnvironments();
header.removeExecutionEnvironment(envs.get(1));
header.removeExecutionEnvironment(envs.get(0));

TextEdit[] ops = fListener.getTextOperations();
assertEquals(1, ops.length);
Expand All @@ -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);

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void testResolveOptional() {
public void testStateEEProperties() {
Dictionary<?, ?>[] platformProps = TargetPlatformHelper.getState().getPlatformProperties();

String[] profiles = TargetPlatformHelper.getKnownExecutionEnvironments();
List<String> profiles = TargetPlatformHelper.getKnownExecutionEnvironments();
for (String profile : profiles) {
IExecutionEnvironment environment = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(profile);
if (environment != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,11 +24,8 @@

public class AddDefaultExecutionEnvironmentResolution extends AbstractManifestMarkerResolution {



public AddDefaultExecutionEnvironmentResolution(int type, IMarker marker) {
super(type, marker);

}

@Override
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> systemEnvs = TargetPlatformHelper.getPDEState().getfProvidedExecutionEnvironments();
for (String ee : reqHeader.getEnvironments()) {
if (!systemEnvs.contains(ee)) {
reqHeader.removeExecutionEnvironment(ee);
}
if (!found)
reqHeader.removeExecutionEnvironment(bundleEnv);
}
}
}
Expand Down
Loading

0 comments on commit de33737

Please sign in to comment.