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

[Launching] Improve addition of default VM arguments for Eclipse apps #1515

Closed
Closed
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 @@ -2,7 +2,7 @@
<feature
id="org.eclipse.pde.unittest.junit"
label="%featureName"
version="1.0.900.qualifier"
version="1.0.1000.qualifier"
provider-name="%providerName"
license-feature="org.eclipse.license"
license-feature-version="0.0.0">
Expand Down
2 changes: 1 addition & 1 deletion ui/org.eclipse.pde.launching/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %name
Bundle-SymbolicName: org.eclipse.pde.launching;singleton:=true
Bundle-Version: 3.13.200.qualifier
Bundle-Version: 3.13.300.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-Vendor: %provider-name
Require-Bundle: org.eclipse.jdt.junit.core;bundle-version="[3.6.0,4.0.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,10 @@ public static IRuntimeClasspathEntry getJREEntry(ILaunchConfiguration configurat
return JavaRuntime.newRuntimeContainerClasspathEntry(containerPath, IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
}

public static void addNewArgument(List<String> arguments, String key, String value) {
if (arguments.stream().noneMatch(a -> a.startsWith(key + "="))) { //$NON-NLS-1$
arguments.add(key + "=" + value); //$NON-NLS-1$
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -43,6 +44,7 @@
import org.eclipse.jdt.launching.IVMRunner;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMRunnerConfiguration;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.TargetPlatform;
import org.eclipse.pde.internal.core.ICoreConstants;
Expand Down Expand Up @@ -112,8 +114,7 @@ public String showCommandLine(ILaunchConfiguration configuration, String mode, I

VMRunnerConfiguration runnerConfig = new VMRunnerConfiguration(getMainClass(), getClasspath(configuration));
IVMInstall launcher = VMHelper.createLauncher(configuration);
boolean isModular = JavaRuntime.isModularJava(launcher);
runnerConfig.setVMArguments(updateVMArgumentWithAdditionalArguments(getVMArguments(configuration), isModular, configuration));
runnerConfig.setVMArguments(updateVMArgumentWithAdditionalArguments(getVMArguments(configuration), launcher));
runnerConfig.setProgramArguments(getProgramArguments(configuration));
runnerConfig.setWorkingDirectory(getWorkingDirectory(configuration).getAbsolutePath());
runnerConfig.setEnvironment(getEnvironment(configuration));
Expand Down Expand Up @@ -148,8 +149,7 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun

VMRunnerConfiguration runnerConfig = new VMRunnerConfiguration(getMainClass(), getClasspath(configuration));
IVMInstall launcher = VMHelper.createLauncher(configuration);
boolean isModular = JavaRuntime.isModularJava(launcher);
runnerConfig.setVMArguments(updateVMArgumentWithAdditionalArguments(getVMArguments(configuration), isModular, configuration));
runnerConfig.setVMArguments(updateVMArgumentWithAdditionalArguments(getVMArguments(configuration), launcher));
runnerConfig.setProgramArguments(getProgramArguments(configuration));
runnerConfig.setWorkingDirectory(getWorkingDirectory(configuration).getAbsolutePath());
runnerConfig.setEnvironment(getEnvironment(configuration));
Expand All @@ -167,79 +167,32 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun

}

private String[] updateVMArgumentWithAdditionalArguments(String[] args, boolean isModular, ILaunchConfiguration configuration) {
String modAllSystem= "--add-modules=ALL-SYSTEM"; //$NON-NLS-1$
String allowSecurityManager = "-Djava.security.manager=allow"; //$NON-NLS-1$
boolean addModuleSystem = false;
boolean addAllowSecurityManager = false;
int argLength = args.length;
if (isModular && !argumentContainsAttribute(args, modAllSystem)) {
addModuleSystem = true;
argLength++; // Need to add the argument
private String[] updateVMArgumentWithAdditionalArguments(String[] args, IVMInstall vmInstall) {
List<String> arguments = new ArrayList<>(Arrays.asList(args));
boolean isModular = JavaRuntime.isModularJava(vmInstall);
if (isModular) {
VMHelper.addNewArgument(arguments, "--add-modules", "ALL-SYSTEM"); //$NON-NLS-1$//$NON-NLS-2$
}

if (isEclipseBundleGreaterThanVersion(4, 24)) { // Don't add allow flags for eclipse before 4.24
try {
IVMInstall vmInstall = VMHelper.getVMInstall(configuration);
if (vmInstall instanceof AbstractVMInstall) {
AbstractVMInstall install = (AbstractVMInstall) vmInstall;
String vmver = install.getJavaVersion();
if (vmver != null && JavaCore.compareJavaVersions(vmver, JavaCore.VERSION_17) >= 0) {
if (!argumentContainsAttribute(args, allowSecurityManager)) {
addAllowSecurityManager = true;
argLength++; // Need to add the argument
}
}
}
} catch (CoreException e) {
PDELaunchingPlugin.log(e);
}
}
if (addModuleSystem || addAllowSecurityManager) {
args = Arrays.copyOf(args, argLength);
if (addAllowSecurityManager) {
args[--argLength] = allowSecurityManager;
}
if (addModuleSystem) {
args[--argLength] = modAllSystem;
if (isEclipseBundleGreaterThanVersion(4, 24) // Don't add allow flags for eclipse before 4.24
&& vmInstall instanceof AbstractVMInstall install) {
String vmver = install.getJavaVersion();
if (vmver != null && JavaCore.compareJavaVersions(vmver, JavaCore.VERSION_17) >= 0) {
VMHelper.addNewArgument(arguments, "-Djava.security.manager", "allow"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
if (!isModular) {
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(args));
arrayList.remove(modAllSystem);
arrayList.trimToSize();
args = arrayList.toArray(new String[arrayList.size()]);
arguments.remove("--add-modules=ALL-SYSTEM"); //$NON-NLS-1$
}
return args;
return arguments.toArray(String[]::new);
}


private boolean isEclipseBundleGreaterThanVersion(int major, int minor) {
PDEState pdeState = TargetPlatformHelper.getPDEState();
if (pdeState != null) {
try {
Optional<IPluginModelBase> platformBaseModel = Arrays.stream(pdeState.getTargetModels()).filter(x -> Objects.nonNull(x.getBundleDescription())).filter(x -> ("org.eclipse.platform").equals(x.getBundleDescription().getSymbolicName()))//$NON-NLS-1$
.findFirst();
if (platformBaseModel.isPresent()) {
Version version = platformBaseModel.get().getBundleDescription().getVersion();
Version comparedVersion = new Version(major, minor, 0);
if (version != null && version.compareTo(comparedVersion) >= 0) {
return true;
}
}
}
catch (Exception ex) {
PDELaunchingPlugin.log(ex);
}
}
return false;

}

private boolean argumentContainsAttribute(String[] args, String modAllSystem) {
for (String string : args) {
if (string.equals(modAllSystem))
return true;
Optional<BundleDescription> bundle = Arrays.stream(pdeState.getTargetModels()) //
.map(IPluginModelBase::getBundleDescription).filter(Objects::nonNull) //
.filter(x -> "org.eclipse.platform".equals(x.getSymbolicName())).findFirst(); //$NON-NLS-1$
return bundle.map(BundleDescription::getVersion).filter(v -> v.compareTo(new Version(major, minor, 0)) >= 0).isPresent();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ protected void collectExecutionArguments(ILaunchConfiguration configuration, Lis
IVMInstall launcher = VMHelper.createLauncher(configuration);
boolean isModular = JavaRuntime.isModularJava(launcher);
if (isModular) {
String modAllSystem = "--add-modules=ALL-SYSTEM"; //$NON-NLS-1$
if (!vmArguments.contains(modAllSystem))
vmArguments.add(modAllSystem);
VMHelper.addNewArgument(vmArguments, "--add-modules", "ALL-SYSTEM"); //$NON-NLS-1$//$NON-NLS-2$
}
// if element is a test class annotated with @RunWith(JUnitPlatform.class, we add this in program arguments
@SuppressWarnings("restriction")
Expand Down
2 changes: 1 addition & 1 deletion ui/org.eclipse.pde.unittest.junit/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.pde.unittest.junit
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.pde.unittest.junit;singleton:=true
Bundle-Version: 1.1.500.qualifier
Bundle-Version: 1.1.600.qualifier
Bundle-Activator: org.eclipse.pde.unittest.junit.JUnitPluginTestPlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: %providerName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,7 @@ protected void collectExecutionArguments(ILaunchConfiguration configuration, Lis
IVMInstall launcher = VMHelper.createLauncher(configuration);
boolean isModular = JavaRuntime.isModularJava(launcher);
if (isModular) {
String modAllSystem = "--add-modules=ALL-SYSTEM"; //$NON-NLS-1$
if (!vmArguments.contains(modAllSystem))
vmArguments.add(modAllSystem);
VMHelper.addNewArgument(vmArguments, "--add-modules", "ALL-SYSTEM"); //$NON-NLS-1$//$NON-NLS-2$
}
// if element is a test class annotated with @RunWith(JUnitPlatform.class, we
// add this in program arguments
Expand Down
Loading