1
1
/*******************************************************************************
2
- * Copyright (c) 2000, 2024 IBM Corporation and others.
2
+ * Copyright (c) 2000, 2022 IBM Corporation and others.
3
3
*
4
4
* This program and the accompanying materials
5
5
* are made available under the terms of the Eclipse Public License 2.0
38
38
import java .util .Comparator ;
39
39
import java .util .HashMap ;
40
40
import java .util .HashSet ;
41
+ import java .util .Iterator ;
41
42
import java .util .List ;
42
43
import java .util .Map ;
43
44
import java .util .Map .Entry ;
@@ -306,6 +307,79 @@ public void run() throws Exception {
306
307
}
307
308
}
308
309
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
+
309
383
/**
310
384
* Notifies a launch listener (single launch) in a safe runnable to handle
311
385
* exceptions.
@@ -557,42 +631,7 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
557
631
* Visitor used to process resource deltas,
558
632
* to update launch configuration index.
559
633
*/
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 ;
596
635
597
636
/**
598
637
* Visitor used to process a deleted resource,
@@ -601,23 +640,7 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
601
640
*
602
641
* @since 3.4
603
642
*/
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 ;
621
644
622
645
/**
623
646
* Whether this manager is listening for resource change events
@@ -1106,6 +1129,31 @@ public IDebugTarget[] getDebugTargets() {
1106
1129
}
1107
1130
}
1108
1131
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
+
1109
1157
@ Override
1110
1158
public String [] getEnvironment (ILaunchConfiguration configuration ) throws CoreException {
1111
1159
Map <String , String > configEnv = configuration .getAttribute (ATTR_ENVIRONMENT_VARIABLES , (Map <String , String >) null );
@@ -1266,7 +1314,9 @@ public ILaunchConfiguration[] getLaunchConfigurations(int kinds) {
1266
1314
return allConfigs .toArray (new ILaunchConfiguration [allConfigs .size ()]);
1267
1315
} else {
1268
1316
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 ();
1270
1320
try {
1271
1321
if ((config .getKind () & kinds ) > 0 ) {
1272
1322
select .add (config );
@@ -2076,11 +2126,15 @@ public void removeLaunchListener(ILaunchListener listener) {
2076
2126
public void resourceChanged (IResourceChangeEvent event ) {
2077
2127
IResourceDelta delta = event .getDelta ();
2078
2128
if (delta != null ) {
2129
+ LaunchManagerVisitor visitor = getDeltaVisitor ();
2130
+ MappedResourceVisitor v = null ;
2131
+ if (isDeleteConfigurations ()) {
2132
+ v = getMappedResourceVisitor ();
2133
+ }
2079
2134
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 );
2084
2138
}
2085
2139
} catch (CoreException e ) {
2086
2140
DebugPlugin .log (e .getStatus ());
@@ -2096,13 +2150,14 @@ public void resourceChanged(IResourceChangeEvent event) {
2096
2150
* @param resource the resource to collect launch configurations for
2097
2151
* @return the list of associated launch configurations
2098
2152
*/
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 <>();
2101
2155
try {
2102
2156
ILaunchConfiguration [] configs = DebugPlugin .getDefault ().getLaunchManager ().getLaunchConfigurations ();
2157
+ IResource [] resources = null ;
2103
2158
for (ILaunchConfiguration config : configs ) {
2104
2159
if (config .isLocal ()) {
2105
- IResource [] resources = config .getMappedResources ();
2160
+ resources = config .getMappedResources ();
2106
2161
if (resources != null ) {
2107
2162
for (IResource res : resources ) {
2108
2163
if (resource .equals (res ) ||
0 commit comments