11/*******************************************************************************
2- * Copyright (c) 2000, 2024 IBM Corporation and others.
2+ * Copyright (c) 2000, 2022 IBM Corporation and others.
33 *
44 * This program and the accompanying materials
55 * are made available under the terms of the Eclipse Public License 2.0
3838import java .util .Comparator ;
3939import java .util .HashMap ;
4040import java .util .HashSet ;
41+ import java .util .Iterator ;
4142import java .util .List ;
4243import java .util .Map ;
4344import java .util .Map .Entry ;
@@ -306,6 +307,79 @@ public void run() throws Exception {
306307 }
307308 }
308309
310+ /**
311+ * Visitor for handling a resource begin deleted, and the need to check mapped configurations
312+ * for auto-deletion
313+ * @since 3.4
314+ */
315+ class MappedResourceVisitor implements IResourceDeltaVisitor {
316+
317+ @ Override
318+ public boolean visit (IResourceDelta delta ) throws CoreException {
319+ if (0 != (delta .getFlags () & IResourceDelta .OPEN )) {
320+ return false ;
321+ }
322+ if (delta .getKind () == IResourceDelta .REMOVED && delta .getFlags () != IResourceDelta .MOVED_TO ) {
323+ ArrayList <ILaunchConfiguration > configs = collectAssociatedLaunches (delta .getResource ());
324+ for (ILaunchConfiguration config : configs ) {
325+ try {
326+ config .delete ();
327+ } catch (CoreException e ) {
328+ DebugPlugin .log (e .getStatus ());
329+ }
330+ }
331+ return false ;
332+ }
333+ return true ;
334+ }
335+ }
336+
337+ /**
338+ * Visitor for handling resource deltas.
339+ */
340+ class LaunchManagerVisitor implements IResourceDeltaVisitor {
341+
342+ @ Override
343+ public boolean visit (IResourceDelta delta ) {
344+ if (delta == null ) {
345+ return false ;
346+ }
347+ if (0 != (delta .getFlags () & IResourceDelta .OPEN )) {
348+ if (delta .getResource () instanceof IProject ) {
349+ IProject project = (IProject )delta .getResource ();
350+ if (project .isOpen ()) {
351+ LaunchManager .this .projectOpened (project );
352+ } else {
353+ LaunchManager .this .projectClosed (project );
354+ }
355+ }
356+ return false ;
357+ }
358+ IResource resource = delta .getResource ();
359+ if (resource instanceof IFile ) {
360+ IFile file = (IFile )resource ;
361+ if (ILaunchConfiguration .LAUNCH_CONFIGURATION_FILE_EXTENSION .equals (file .getFileExtension ()) || ILaunchConfiguration .LAUNCH_CONFIGURATION_PROTOTYPE_FILE_EXTENSION .equals (file .getFileExtension ())) {
362+ ILaunchConfiguration handle = new LaunchConfiguration (file );
363+ switch (delta .getKind ()) {
364+ case IResourceDelta .ADDED :
365+ LaunchManager .this .launchConfigurationAdded (handle );
366+ break ;
367+ case IResourceDelta .REMOVED :
368+ LaunchManager .this .launchConfigurationDeleted (handle );
369+ break ;
370+ case IResourceDelta .CHANGED :
371+ LaunchManager .this .launchConfigurationChanged (handle );
372+ break ;
373+ default :
374+ break ;
375+ }
376+ }
377+ return false ;
378+ }
379+ return true ;
380+ }
381+ }
382+
309383 /**
310384 * Notifies a launch listener (single launch) in a safe runnable to handle
311385 * exceptions.
@@ -557,42 +631,7 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
557631 * Visitor used to process resource deltas,
558632 * to update launch configuration index.
559633 */
560- private final IResourceDeltaVisitor fgVisitor = delta -> {
561- if (delta == null ) {
562- return false ;
563- }
564- if (0 != (delta .getFlags () & IResourceDelta .OPEN )) {
565- if (delta .getFullPath ().segmentCount () == 1 && delta .getResource () instanceof IProject project ) {
566- if (project .isOpen ()) {
567- LaunchManager .this .projectOpened (project );
568- } else {
569- LaunchManager .this .projectClosed (project );
570- }
571- }
572- return false ;
573- }
574- String fileExtension = delta .getFullPath ().getFileExtension ();
575- if (ILaunchConfiguration .LAUNCH_CONFIGURATION_FILE_EXTENSION .equals (fileExtension ) || ILaunchConfiguration .LAUNCH_CONFIGURATION_PROTOTYPE_FILE_EXTENSION .equals (fileExtension )) {
576- if (delta .getResource () instanceof IFile file ) {
577- ILaunchConfiguration handle = new LaunchConfiguration (file );
578- switch (delta .getKind ()) {
579- case IResourceDelta .ADDED :
580- LaunchManager .this .launchConfigurationAdded (handle );
581- break ;
582- case IResourceDelta .REMOVED :
583- LaunchManager .this .launchConfigurationDeleted (handle );
584- break ;
585- case IResourceDelta .CHANGED :
586- LaunchManager .this .launchConfigurationChanged (handle );
587- break ;
588- default :
589- break ;
590- }
591- }
592- return false ;
593- }
594- return true ;
595- };
634+ private LaunchManagerVisitor fgVisitor ;
596635
597636 /**
598637 * Visitor used to process a deleted resource,
@@ -601,23 +640,7 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
601640 *
602641 * @since 3.4
603642 */
604- private final IResourceDeltaVisitor fgMRVisitor = delta -> {
605- if (0 != (delta .getFlags () & IResourceDelta .OPEN )) {
606- return false ;
607- }
608- if (delta .getKind () == IResourceDelta .REMOVED && delta .getFlags () != IResourceDelta .MOVED_TO ) {
609- List <ILaunchConfiguration > configs = collectAssociatedLaunches (delta .getResource ());
610- for (ILaunchConfiguration config : configs ) {
611- try {
612- config .delete ();
613- } catch (CoreException e ) {
614- DebugPlugin .log (e .getStatus ());
615- }
616- }
617- return false ;
618- }
619- return true ;
620- };
643+ private MappedResourceVisitor fgMRVisitor ;
621644
622645 /**
623646 * Whether this manager is listening for resource change events
@@ -1106,6 +1129,31 @@ public IDebugTarget[] getDebugTargets() {
11061129 }
11071130 }
11081131
1132+ /**
1133+ * Returns the resource delta visitor for the launch manager.
1134+ *
1135+ * @return the resource delta visitor for the launch manager
1136+ */
1137+ private LaunchManagerVisitor getDeltaVisitor () {
1138+ if (fgVisitor == null ) {
1139+ fgVisitor = new LaunchManagerVisitor ();
1140+ }
1141+ return fgVisitor ;
1142+ }
1143+
1144+ /**
1145+ * Returns the resource delta visitor for auto-removal of mapped launch configurations
1146+ * @return the resource delta visitor for auto-removal of mapped launch configurations
1147+ *
1148+ * @since 3.4
1149+ */
1150+ private MappedResourceVisitor getMappedResourceVisitor () {
1151+ if (fgMRVisitor == null ) {
1152+ fgMRVisitor = new MappedResourceVisitor ();
1153+ }
1154+ return fgMRVisitor ;
1155+ }
1156+
11091157 @ Override
11101158 public String [] getEnvironment (ILaunchConfiguration configuration ) throws CoreException {
11111159 Map <String , String > configEnv = configuration .getAttribute (ATTR_ENVIRONMENT_VARIABLES , (Map <String , String >) null );
@@ -1266,7 +1314,9 @@ public ILaunchConfiguration[] getLaunchConfigurations(int kinds) {
12661314 return allConfigs .toArray (new ILaunchConfiguration [allConfigs .size ()]);
12671315 } else {
12681316 List <ILaunchConfiguration > select = new ArrayList <>(allConfigs .size ());
1269- for (ILaunchConfiguration config : allConfigs ) {
1317+ Iterator <ILaunchConfiguration > iterator = allConfigs .iterator ();
1318+ while (iterator .hasNext ()) {
1319+ ILaunchConfiguration config = iterator .next ();
12701320 try {
12711321 if ((config .getKind () & kinds ) > 0 ) {
12721322 select .add (config );
@@ -2076,11 +2126,15 @@ public void removeLaunchListener(ILaunchListener listener) {
20762126 public void resourceChanged (IResourceChangeEvent event ) {
20772127 IResourceDelta delta = event .getDelta ();
20782128 if (delta != null ) {
2129+ LaunchManagerVisitor visitor = getDeltaVisitor ();
2130+ MappedResourceVisitor v = null ;
2131+ if (isDeleteConfigurations ()) {
2132+ v = getMappedResourceVisitor ();
2133+ }
20792134 try {
2080- boolean deleteConfigurations = isDeleteConfigurations ();
2081- delta .accept (fgVisitor );
2082- if (deleteConfigurations ) {
2083- delta .accept (fgMRVisitor );
2135+ delta .accept (visitor );
2136+ if (v != null ) {
2137+ delta .accept (v );
20842138 }
20852139 } catch (CoreException e ) {
20862140 DebugPlugin .log (e .getStatus ());
@@ -2096,13 +2150,14 @@ public void resourceChanged(IResourceChangeEvent event) {
20962150 * @param resource the resource to collect launch configurations for
20972151 * @return the list of associated launch configurations
20982152 */
2099- private static List <ILaunchConfiguration > collectAssociatedLaunches (IResource resource ) {
2100- List <ILaunchConfiguration > list = new ArrayList <>();
2153+ private ArrayList <ILaunchConfiguration > collectAssociatedLaunches (IResource resource ) {
2154+ ArrayList <ILaunchConfiguration > list = new ArrayList <>();
21012155 try {
21022156 ILaunchConfiguration [] configs = DebugPlugin .getDefault ().getLaunchManager ().getLaunchConfigurations ();
2157+ IResource [] resources = null ;
21032158 for (ILaunchConfiguration config : configs ) {
21042159 if (config .isLocal ()) {
2105- IResource [] resources = config .getMappedResources ();
2160+ resources = config .getMappedResources ();
21062161 if (resources != null ) {
21072162 for (IResource res : resources ) {
21082163 if (resource .equals (res ) ||
0 commit comments