Skip to content

Commit c95b821

Browse files
committed
Clean-up and modernize eclipse debug LaunchManager
Implement the IResourceDeltaVisitor in LaunchManager as lambda's instead of nested classes and more.
1 parent 6dd6732 commit c95b821

File tree

1 file changed

+80
-150
lines changed

1 file changed

+80
-150
lines changed

debug/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/LaunchManager.java

Lines changed: 80 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -38,8 +38,8 @@
3838
import java.util.Comparator;
3939
import java.util.HashMap;
4040
import java.util.HashSet;
41-
import java.util.Iterator;
4241
import java.util.List;
42+
import java.util.Locale;
4343
import java.util.Map;
4444
import java.util.Map.Entry;
4545
import java.util.Set;
@@ -64,7 +64,6 @@
6464
import org.eclipse.core.resources.IResourceChangeListener;
6565
import org.eclipse.core.resources.IResourceDelta;
6666
import org.eclipse.core.resources.IResourceDeltaVisitor;
67-
import org.eclipse.core.resources.IResourceProxy;
6867
import org.eclipse.core.resources.IResourceProxyVisitor;
6968
import org.eclipse.core.resources.ResourcesPlugin;
7069
import org.eclipse.core.runtime.CoreException;
@@ -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.
@@ -433,30 +359,6 @@ public void run() throws Exception {
433359
}
434360
}
435361

436-
/**
437-
* Collects files whose extension matches the launch configuration file
438-
* extension.
439-
*/
440-
static class ResourceProxyVisitor implements IResourceProxyVisitor {
441-
442-
private final List<IResource> fList;
443-
444-
protected ResourceProxyVisitor(List<IResource> list) {
445-
fList= list;
446-
}
447-
448-
@Override
449-
public boolean visit(IResourceProxy proxy) {
450-
if (proxy.getType() == IResource.FILE) {
451-
if (ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION.equalsIgnoreCase(proxy.requestFullPath().getFileExtension()) || ILaunchConfiguration.LAUNCH_CONFIGURATION_PROTOTYPE_FILE_EXTENSION.equalsIgnoreCase(proxy.requestFullPath().getFileExtension())) {
452-
fList.add(proxy.requestResource());
453-
}
454-
return false;
455-
}
456-
return true;
457-
}
458-
}
459-
460362
/**
461363
* Internal class used to hold information about a preferred delegate
462364
*
@@ -627,11 +529,49 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
627529
*/
628530
private ListenerList<ILaunchesListener> fLaunchesListeners = new ListenerList<>();
629531

532+
private static final Set<String> LAUNCH_CONFIG_FILE_EXTENSIONS = Set.of( //
533+
ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION, //
534+
ILaunchConfiguration.LAUNCH_CONFIGURATION_PROTOTYPE_FILE_EXTENSION);
535+
630536
/**
631537
* Visitor used to process resource deltas,
632538
* to update launch configuration index.
633539
*/
634-
private LaunchManagerVisitor fgVisitor;
540+
private final IResourceDeltaVisitor fLaunchManagerVisitor = delta -> {
541+
if (delta == null) {
542+
return false;
543+
}
544+
if (0 != (delta.getFlags() & IResourceDelta.OPEN)) {
545+
if (delta.getResource() instanceof IProject project) {
546+
if (project.isOpen()) {
547+
LaunchManager.this.projectOpened(project);
548+
} else {
549+
LaunchManager.this.projectClosed(project);
550+
}
551+
}
552+
return false;
553+
}
554+
if (delta.getResource() instanceof IFile file) {
555+
if (LAUNCH_CONFIG_FILE_EXTENSIONS.contains(file.getFileExtension())) {
556+
ILaunchConfiguration handle = new LaunchConfiguration(file);
557+
switch (delta.getKind()) {
558+
case IResourceDelta.ADDED:
559+
LaunchManager.this.launchConfigurationAdded(handle);
560+
break;
561+
case IResourceDelta.REMOVED:
562+
LaunchManager.this.launchConfigurationDeleted(handle);
563+
break;
564+
case IResourceDelta.CHANGED:
565+
LaunchManager.this.launchConfigurationChanged(handle);
566+
break;
567+
default:
568+
break;
569+
}
570+
}
571+
return false;
572+
}
573+
return true;
574+
};
635575

636576
/**
637577
* Visitor used to process a deleted resource,
@@ -640,7 +580,23 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
640580
*
641581
* @since 3.4
642582
*/
643-
private MappedResourceVisitor fgMRVisitor;
583+
private final IResourceDeltaVisitor fMappedResourceVisitor = delta -> {
584+
if (0 != (delta.getFlags() & IResourceDelta.OPEN)) {
585+
return false;
586+
}
587+
if (delta.getKind() == IResourceDelta.REMOVED && delta.getFlags() != IResourceDelta.MOVED_TO) {
588+
List<ILaunchConfiguration> configs = collectAssociatedLaunches(delta.getResource());
589+
for (ILaunchConfiguration config : configs) {
590+
try {
591+
config.delete();
592+
} catch (CoreException e) {
593+
DebugPlugin.log(e.getStatus());
594+
}
595+
}
596+
return false;
597+
}
598+
return true;
599+
};
644600

645601
/**
646602
* Whether this manager is listening for resource change events
@@ -842,7 +798,16 @@ protected List<ILaunchConfiguration> findLaunchConfigurations(IContainer contain
842798
return Collections.emptyList();
843799
}
844800
List<IResource> list = new ArrayList<>(10);
845-
ResourceProxyVisitor visitor= new ResourceProxyVisitor(list);
801+
IResourceProxyVisitor visitor = proxy -> {
802+
if (proxy.getType() == IResource.FILE) {
803+
String fileExtension = proxy.requestFullPath().getFileExtension();
804+
if (LAUNCH_CONFIG_FILE_EXTENSIONS.contains(fileExtension.toLowerCase(Locale.ENGLISH))) {
805+
list.add(proxy.requestResource());
806+
}
807+
return false;
808+
}
809+
return true;
810+
};
846811
try {
847812
container.accept(visitor, IResource.NONE);
848813
} catch (CoreException ce) {
@@ -1129,31 +1094,6 @@ public IDebugTarget[] getDebugTargets() {
11291094
}
11301095
}
11311096

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-
11571097
@Override
11581098
public String[] getEnvironment(ILaunchConfiguration configuration) throws CoreException {
11591099
Map<String, String> configEnv = configuration.getAttribute(ATTR_ENVIRONMENT_VARIABLES, (Map<String, String>) null);
@@ -1314,9 +1254,7 @@ public ILaunchConfiguration[] getLaunchConfigurations(int kinds) {
13141254
return allConfigs.toArray(new ILaunchConfiguration[allConfigs.size()]);
13151255
} else {
13161256
List<ILaunchConfiguration> select = new ArrayList<>(allConfigs.size());
1317-
Iterator<ILaunchConfiguration> iterator = allConfigs.iterator();
1318-
while (iterator.hasNext()) {
1319-
ILaunchConfiguration config = iterator.next();
1257+
for (ILaunchConfiguration config : allConfigs) {
13201258
try {
13211259
if ((config.getKind() & kinds) > 0) {
13221260
select.add(config);
@@ -1499,19 +1437,16 @@ private synchronized void initializePreferredDelegates() {
14991437
try {
15001438
Element root = DebugPlugin.parseDocument(preferred);
15011439
NodeList nodes = root.getElementsByTagName(IConfigurationElementConstants.DELEGATE);
1502-
Element element = null;
1503-
String typeid = null;
1504-
Set<String> modeset = null;
15051440
for(int i = 0; i < nodes.getLength(); i++) {
1506-
element = (Element) nodes.item(i);
1441+
Element element = (Element) nodes.item(i);
15071442
String delegateid = element.getAttribute(IConfigurationElementConstants.ID);
1508-
typeid = element.getAttribute(IConfigurationElementConstants.TYPE_ID);
1443+
String typeid = element.getAttribute(IConfigurationElementConstants.TYPE_ID);
15091444
String[] modes = element.getAttribute(IConfigurationElementConstants.MODES).split(","); //$NON-NLS-1$
1510-
modeset = new HashSet<>(Arrays.asList(modes));
1445+
Set<String> modeset = new HashSet<>(Arrays.asList(modes));
15111446
LaunchDelegate delegate = getLaunchDelegateExtension(typeid, delegateid, modeset);
15121447
if (delegate != null) {
15131448
//take type id, modeset, delegate and create entry
1514-
if(!IInternalDebugCoreConstants.EMPTY_STRING.equals(typeid) && modeset != null) {
1449+
if (!typeid.isEmpty() && modeset != null) {
15151450
fPreferredDelegates.add(new PreferredDelegate(delegate, typeid, modeset));
15161451
}
15171452
}
@@ -2126,15 +2061,11 @@ public void removeLaunchListener(ILaunchListener listener) {
21262061
public void resourceChanged(IResourceChangeEvent event) {
21272062
IResourceDelta delta = event.getDelta();
21282063
if (delta != null) {
2129-
LaunchManagerVisitor visitor = getDeltaVisitor();
2130-
MappedResourceVisitor v = null;
2131-
if (isDeleteConfigurations()) {
2132-
v = getMappedResourceVisitor();
2133-
}
21342064
try {
2135-
delta.accept(visitor);
2136-
if (v != null) {
2137-
delta.accept(v);
2065+
boolean deleteConfigurations = isDeleteConfigurations();
2066+
delta.accept(fLaunchManagerVisitor);
2067+
if (deleteConfigurations) {
2068+
delta.accept(fMappedResourceVisitor);
21382069
}
21392070
} catch (CoreException e) {
21402071
DebugPlugin.log(e.getStatus());
@@ -2150,14 +2081,13 @@ public void resourceChanged(IResourceChangeEvent event) {
21502081
* @param resource the resource to collect launch configurations for
21512082
* @return the list of associated launch configurations
21522083
*/
2153-
private ArrayList<ILaunchConfiguration> collectAssociatedLaunches(IResource resource) {
2154-
ArrayList<ILaunchConfiguration> list = new ArrayList<>();
2084+
private static List<ILaunchConfiguration> collectAssociatedLaunches(IResource resource) {
2085+
List<ILaunchConfiguration> list = new ArrayList<>();
21552086
try {
21562087
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
2157-
IResource[] resources = null;
21582088
for (ILaunchConfiguration config : configs) {
21592089
if(config.isLocal()) {
2160-
resources = config.getMappedResources();
2090+
IResource[] resources = config.getMappedResources();
21612091
if(resources != null) {
21622092
for (IResource res : resources) {
21632093
if(resource.equals(res) ||

0 commit comments

Comments
 (0)