11/*******************************************************************************
2- * Copyright (c) 2000, 2022 IBM Corporation and others.
2+ * Copyright (c) 2000, 2024 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 ;
4241import java .util .List ;
4342import java .util .Map ;
4443import java .util .Map .Entry ;
@@ -307,79 +306,6 @@ public void run() throws Exception {
307306 }
308307 }
309308
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-
383309 /**
384310 * Notifies a launch listener (single launch) in a safe runnable to handle
385311 * exceptions.
@@ -631,7 +557,42 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
631557 * Visitor used to process resource deltas,
632558 * to update launch configuration index.
633559 */
634- private LaunchManagerVisitor fgVisitor ;
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+ };
635596
636597 /**
637598 * Visitor used to process a deleted resource,
@@ -640,7 +601,23 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
640601 *
641602 * @since 3.4
642603 */
643- private MappedResourceVisitor fgMRVisitor ;
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+ };
644621
645622 /**
646623 * Whether this manager is listening for resource change events
@@ -1129,31 +1106,6 @@ public IDebugTarget[] getDebugTargets() {
11291106 }
11301107 }
11311108
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-
11571109 @ Override
11581110 public String [] getEnvironment (ILaunchConfiguration configuration ) throws CoreException {
11591111 Map <String , String > configEnv = configuration .getAttribute (ATTR_ENVIRONMENT_VARIABLES , (Map <String , String >) null );
@@ -1314,9 +1266,7 @@ public ILaunchConfiguration[] getLaunchConfigurations(int kinds) {
13141266 return allConfigs .toArray (new ILaunchConfiguration [allConfigs .size ()]);
13151267 } else {
13161268 List <ILaunchConfiguration > select = new ArrayList <>(allConfigs .size ());
1317- Iterator <ILaunchConfiguration > iterator = allConfigs .iterator ();
1318- while (iterator .hasNext ()) {
1319- ILaunchConfiguration config = iterator .next ();
1269+ for (ILaunchConfiguration config : allConfigs ) {
13201270 try {
13211271 if ((config .getKind () & kinds ) > 0 ) {
13221272 select .add (config );
@@ -2126,15 +2076,11 @@ public void removeLaunchListener(ILaunchListener listener) {
21262076 public void resourceChanged (IResourceChangeEvent event ) {
21272077 IResourceDelta delta = event .getDelta ();
21282078 if (delta != null ) {
2129- LaunchManagerVisitor visitor = getDeltaVisitor ();
2130- MappedResourceVisitor v = null ;
2131- if (isDeleteConfigurations ()) {
2132- v = getMappedResourceVisitor ();
2133- }
21342079 try {
2135- delta .accept (visitor );
2136- if (v != null ) {
2137- delta .accept (v );
2080+ boolean deleteConfigurations = isDeleteConfigurations ();
2081+ delta .accept (fgVisitor );
2082+ if (deleteConfigurations ) {
2083+ delta .accept (fgMRVisitor );
21382084 }
21392085 } catch (CoreException e ) {
21402086 DebugPlugin .log (e .getStatus ());
@@ -2150,14 +2096,13 @@ public void resourceChanged(IResourceChangeEvent event) {
21502096 * @param resource the resource to collect launch configurations for
21512097 * @return the list of associated launch configurations
21522098 */
2153- private ArrayList <ILaunchConfiguration > collectAssociatedLaunches (IResource resource ) {
2154- ArrayList <ILaunchConfiguration > list = new ArrayList <>();
2099+ private static List <ILaunchConfiguration > collectAssociatedLaunches (IResource resource ) {
2100+ List <ILaunchConfiguration > list = new ArrayList <>();
21552101 try {
21562102 ILaunchConfiguration [] configs = DebugPlugin .getDefault ().getLaunchManager ().getLaunchConfigurations ();
2157- IResource [] resources = null ;
21582103 for (ILaunchConfiguration config : configs ) {
21592104 if (config .isLocal ()) {
2160- resources = config .getMappedResources ();
2105+ IResource [] resources = config .getMappedResources ();
21612106 if (resources != null ) {
21622107 for (IResource res : resources ) {
21632108 if (resource .equals (res ) ||
0 commit comments