1717import java .io .File ;
1818import java .util .ArrayList ;
1919import java .util .Arrays ;
20+ import java .util .List ;
2021import java .util .Map ;
2122import java .util .Objects ;
2223import java .util .Optional ;
4344import org .eclipse .jdt .launching .IVMRunner ;
4445import org .eclipse .jdt .launching .JavaRuntime ;
4546import org .eclipse .jdt .launching .VMRunnerConfiguration ;
47+ import org .eclipse .osgi .service .resolver .BundleDescription ;
4648import org .eclipse .pde .core .plugin .IPluginModelBase ;
4749import org .eclipse .pde .core .plugin .TargetPlatform ;
4850import org .eclipse .pde .internal .core .ICoreConstants ;
@@ -112,8 +114,7 @@ public String showCommandLine(ILaunchConfiguration configuration, String mode, I
112114
113115 VMRunnerConfiguration runnerConfig = new VMRunnerConfiguration (getMainClass (), getClasspath (configuration ));
114116 IVMInstall launcher = VMHelper .createLauncher (configuration );
115- boolean isModular = JavaRuntime .isModularJava (launcher );
116- runnerConfig .setVMArguments (updateVMArgumentWithAdditionalArguments (getVMArguments (configuration ), isModular , configuration ));
117+ runnerConfig .setVMArguments (updateVMArgumentWithAdditionalArguments (getVMArguments (configuration ), launcher ));
117118 runnerConfig .setProgramArguments (getProgramArguments (configuration ));
118119 runnerConfig .setWorkingDirectory (getWorkingDirectory (configuration ).getAbsolutePath ());
119120 runnerConfig .setEnvironment (getEnvironment (configuration ));
@@ -148,8 +149,7 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun
148149
149150 VMRunnerConfiguration runnerConfig = new VMRunnerConfiguration (getMainClass (), getClasspath (configuration ));
150151 IVMInstall launcher = VMHelper .createLauncher (configuration );
151- boolean isModular = JavaRuntime .isModularJava (launcher );
152- runnerConfig .setVMArguments (updateVMArgumentWithAdditionalArguments (getVMArguments (configuration ), isModular , configuration ));
152+ runnerConfig .setVMArguments (updateVMArgumentWithAdditionalArguments (getVMArguments (configuration ), launcher ));
153153 runnerConfig .setProgramArguments (getProgramArguments (configuration ));
154154 runnerConfig .setWorkingDirectory (getWorkingDirectory (configuration ).getAbsolutePath ());
155155 runnerConfig .setEnvironment (getEnvironment (configuration ));
@@ -167,82 +167,42 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun
167167
168168 }
169169
170- private String [] updateVMArgumentWithAdditionalArguments (String [] args , boolean isModular , ILaunchConfiguration configuration ) {
171- String modAllSystem = "--add-modules=ALL-SYSTEM" ; //$NON-NLS-1$
172- String allowSecurityManager = "-Djava.security.manager=allow" ; //$NON-NLS-1$
173- boolean addModuleSystem = false ;
174- boolean addAllowSecurityManager = false ;
175- int argLength = args .length ;
176- if (isModular && !argumentContainsAttribute (args , modAllSystem )) {
177- addModuleSystem = true ;
178- argLength ++; // Need to add the argument
170+ private String [] updateVMArgumentWithAdditionalArguments (String [] args , IVMInstall vmInstall ) {
171+ List <String > arguments = new ArrayList <>(Arrays .asList (args ));
172+ boolean isModular = JavaRuntime .isModularJava (vmInstall );
173+ if (isModular ) {
174+ VMHelper .addNewArgument (arguments , "--add-modules" , "ALL-SYSTEM" ); //$NON-NLS-1$//$NON-NLS-2$
179175 }
180-
181- if (isEclipseBundleGreaterThanVersion (4 , 24 )) { // Don't add allow flags for eclipse before 4.24
182- try {
183- IVMInstall vmInstall = VMHelper .getVMInstall (configuration );
184- if (vmInstall instanceof AbstractVMInstall ) {
185- AbstractVMInstall install = (AbstractVMInstall ) vmInstall ;
186- String vmver = install .getJavaVersion ();
187- if (vmver != null && JavaCore .compareJavaVersions (vmver , JavaCore .VERSION_17 ) >= 0 ) {
188- if (!argumentContainsAttribute (args , allowSecurityManager )) {
189- addAllowSecurityManager = true ;
190- argLength ++; // Need to add the argument
191- }
192- }
193- }
194- } catch (CoreException e ) {
195- PDELaunchingPlugin .log (e );
196- }
197- }
198- if (addModuleSystem || addAllowSecurityManager ) {
199- args = Arrays .copyOf (args , argLength );
200- if (addAllowSecurityManager ) {
201- args [--argLength ] = allowSecurityManager ;
202- }
203- if (addModuleSystem ) {
204- args [--argLength ] = modAllSystem ;
176+ if (isEclipseBundleGreaterThanVersion (4 , 24 ) // Don't add allow flags for eclipse before 4.24
177+ && vmInstall instanceof AbstractVMInstall install ) {
178+ String vmver = install .getJavaVersion ();
179+ if (vmver != null && JavaCore .compareJavaVersions (vmver , JavaCore .VERSION_17 ) >= 0 ) {
180+ VMHelper .addNewArgument (arguments , "-Djava.security.manager" , "allow" ); //$NON-NLS-1$ //$NON-NLS-2$
205181 }
206182 }
207183 if (!isModular ) {
208- ArrayList <String > arrayList = new ArrayList <>(Arrays .asList (args ));
209- arrayList .remove (modAllSystem );
210- arrayList .trimToSize ();
211- args = arrayList .toArray (new String [arrayList .size ()]);
184+ arguments .remove ("--add-modules=ALL-SYSTEM" ); //$NON-NLS-1$
212185 }
213- return args ;
186+ return arguments . toArray ( String []:: new ) ;
214187 }
215188
216189
217190 private boolean isEclipseBundleGreaterThanVersion (int major , int minor ) {
218191 PDEState pdeState = TargetPlatformHelper .getPDEState ();
219192 if (pdeState != null ) {
220193 try {
221- 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$
222- .findFirst ();
223- if (platformBaseModel .isPresent ()) {
224- Version version = platformBaseModel .get ().getBundleDescription ().getVersion ();
225- Version comparedVersion = new Version (major , minor , 0 );
226- if (version != null && version .compareTo (comparedVersion ) >= 0 ) {
227- return true ;
228- }
229- }
230- }
231- catch (Exception ex ) {
194+ Optional <BundleDescription > model = Arrays .stream (pdeState .getTargetModels ()) //
195+ .map (IPluginModelBase ::getBundleDescription ).filter (Objects ::nonNull ) //
196+ .filter (x -> "org.eclipse.platform" .equals (x .getSymbolicName ())).findFirst (); //$NON-NLS-1$
197+ return model .map (BundleDescription ::getVersion ).filter (v -> v .compareTo (new Version (major , minor , 0 )) >= 0 ).isPresent ();
198+ } catch (Exception ex ) {
232199 PDELaunchingPlugin .log (ex );
233200 }
234201 }
235202 return false ;
236-
237203 }
238204
239- private boolean argumentContainsAttribute (String [] args , String modAllSystem ) {
240- for (String string : args ) {
241- if (string .equals (modAllSystem ))
242- return true ;
243- }
244- return false ;
245- }
205+
246206
247207 /**
248208 * Returns the VM runner for the given launch mode to use when launching the
0 commit comments