Skip to content

Commit

Permalink
Revert "Make LaunchManager's IResourceDeltaVisitors more light weight"
Browse files Browse the repository at this point in the history
This reverts commit 8d5bfec.
  • Loading branch information
trancexpress committed Nov 13, 2024
1 parent 0abe5a5 commit 74aac75
Showing 1 changed file with 117 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -38,6 +38,7 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -306,6 +307,79 @@ public void run() throws Exception {
}
}

/**
* Visitor for handling a resource begin deleted, and the need to check mapped configurations
* for auto-deletion
* @since 3.4
*/
class MappedResourceVisitor implements IResourceDeltaVisitor {

@Override
public boolean visit(IResourceDelta delta) throws CoreException {
if (0 != (delta.getFlags() & IResourceDelta.OPEN)) {
return false;
}
if(delta.getKind() == IResourceDelta.REMOVED && delta.getFlags() != IResourceDelta.MOVED_TO) {
ArrayList<ILaunchConfiguration> configs = collectAssociatedLaunches(delta.getResource());
for (ILaunchConfiguration config : configs) {
try {
config.delete();
} catch (CoreException e) {
DebugPlugin.log(e.getStatus());
}
}
return false;
}
return true;
}
}

/**
* Visitor for handling resource deltas.
*/
class LaunchManagerVisitor implements IResourceDeltaVisitor {

@Override
public boolean visit(IResourceDelta delta) {
if (delta == null) {
return false;
}
if (0 != (delta.getFlags() & IResourceDelta.OPEN)) {
if (delta.getResource() instanceof IProject) {
IProject project = (IProject)delta.getResource();
if (project.isOpen()) {
LaunchManager.this.projectOpened(project);
} else {
LaunchManager.this.projectClosed(project);
}
}
return false;
}
IResource resource = delta.getResource();
if (resource instanceof IFile) {
IFile file = (IFile)resource;
if (ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION.equals(file.getFileExtension()) || ILaunchConfiguration.LAUNCH_CONFIGURATION_PROTOTYPE_FILE_EXTENSION.equals(file.getFileExtension())) {
ILaunchConfiguration handle = new LaunchConfiguration(file);
switch (delta.getKind()) {
case IResourceDelta.ADDED :
LaunchManager.this.launchConfigurationAdded(handle);
break;
case IResourceDelta.REMOVED :
LaunchManager.this.launchConfigurationDeleted(handle);
break;
case IResourceDelta.CHANGED :
LaunchManager.this.launchConfigurationChanged(handle);
break;
default:
break;
}
}
return false;
}
return true;
}
}

/**
* Notifies a launch listener (single launch) in a safe runnable to handle
* exceptions.
Expand Down Expand Up @@ -557,42 +631,7 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
* Visitor used to process resource deltas,
* to update launch configuration index.
*/
private final IResourceDeltaVisitor fgVisitor = delta -> {
if (delta == null) {
return false;
}
if (0 != (delta.getFlags() & IResourceDelta.OPEN)) {
if (delta.getFullPath().segmentCount() == 1 && delta.getResource() instanceof IProject project) {
if (project.isOpen()) {
LaunchManager.this.projectOpened(project);
} else {
LaunchManager.this.projectClosed(project);
}
}
return false;
}
String fileExtension = delta.getFullPath().getFileExtension();
if (ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION.equals(fileExtension) || ILaunchConfiguration.LAUNCH_CONFIGURATION_PROTOTYPE_FILE_EXTENSION.equals(fileExtension)) {
if (delta.getResource() instanceof IFile file) {
ILaunchConfiguration handle = new LaunchConfiguration(file);
switch (delta.getKind()) {
case IResourceDelta.ADDED:
LaunchManager.this.launchConfigurationAdded(handle);
break;
case IResourceDelta.REMOVED:
LaunchManager.this.launchConfigurationDeleted(handle);
break;
case IResourceDelta.CHANGED:
LaunchManager.this.launchConfigurationChanged(handle);
break;
default:
break;
}
}
return false;
}
return true;
};
private LaunchManagerVisitor fgVisitor;

/**
* Visitor used to process a deleted resource,
Expand All @@ -601,23 +640,7 @@ public static String serializeDocument(Document doc, String lineDelimiter) throw
*
* @since 3.4
*/
private final IResourceDeltaVisitor fgMRVisitor = delta -> {
if (0 != (delta.getFlags() & IResourceDelta.OPEN)) {
return false;
}
if (delta.getKind() == IResourceDelta.REMOVED && delta.getFlags() != IResourceDelta.MOVED_TO) {
List<ILaunchConfiguration> configs = collectAssociatedLaunches(delta.getResource());
for (ILaunchConfiguration config : configs) {
try {
config.delete();
} catch (CoreException e) {
DebugPlugin.log(e.getStatus());
}
}
return false;
}
return true;
};
private MappedResourceVisitor fgMRVisitor;

/**
* Whether this manager is listening for resource change events
Expand Down Expand Up @@ -1106,6 +1129,31 @@ public IDebugTarget[] getDebugTargets() {
}
}

/**
* Returns the resource delta visitor for the launch manager.
*
* @return the resource delta visitor for the launch manager
*/
private LaunchManagerVisitor getDeltaVisitor() {
if (fgVisitor == null) {
fgVisitor= new LaunchManagerVisitor();
}
return fgVisitor;
}

/**
* Returns the resource delta visitor for auto-removal of mapped launch configurations
* @return the resource delta visitor for auto-removal of mapped launch configurations
*
* @since 3.4
*/
private MappedResourceVisitor getMappedResourceVisitor() {
if(fgMRVisitor == null) {
fgMRVisitor = new MappedResourceVisitor();
}
return fgMRVisitor;
}

@Override
public String[] getEnvironment(ILaunchConfiguration configuration) throws CoreException {
Map<String, String> configEnv = configuration.getAttribute(ATTR_ENVIRONMENT_VARIABLES, (Map<String, String>) null);
Expand Down Expand Up @@ -1266,7 +1314,9 @@ public ILaunchConfiguration[] getLaunchConfigurations(int kinds) {
return allConfigs.toArray(new ILaunchConfiguration[allConfigs.size()]);
} else {
List<ILaunchConfiguration> select = new ArrayList<>(allConfigs.size());
for (ILaunchConfiguration config : allConfigs) {
Iterator<ILaunchConfiguration> iterator = allConfigs.iterator();
while (iterator.hasNext()) {
ILaunchConfiguration config = iterator.next();
try {
if ((config.getKind() & kinds) > 0) {
select.add(config);
Expand Down Expand Up @@ -2076,11 +2126,15 @@ public void removeLaunchListener(ILaunchListener listener) {
public void resourceChanged(IResourceChangeEvent event) {
IResourceDelta delta = event.getDelta();
if (delta != null) {
LaunchManagerVisitor visitor = getDeltaVisitor();
MappedResourceVisitor v = null;
if (isDeleteConfigurations()) {
v = getMappedResourceVisitor();
}
try {
boolean deleteConfigurations = isDeleteConfigurations();
delta.accept(fgVisitor);
if (deleteConfigurations) {
delta.accept(fgMRVisitor);
delta.accept(visitor);
if (v != null) {
delta.accept(v);
}
} catch (CoreException e) {
DebugPlugin.log(e.getStatus());
Expand All @@ -2096,13 +2150,14 @@ public void resourceChanged(IResourceChangeEvent event) {
* @param resource the resource to collect launch configurations for
* @return the list of associated launch configurations
*/
private static List<ILaunchConfiguration> collectAssociatedLaunches(IResource resource) {
List<ILaunchConfiguration> list = new ArrayList<>();
private ArrayList<ILaunchConfiguration> collectAssociatedLaunches(IResource resource) {
ArrayList<ILaunchConfiguration> list = new ArrayList<>();
try {
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
IResource[] resources = null;
for (ILaunchConfiguration config : configs) {
if(config.isLocal()) {
IResource[] resources = config.getMappedResources();
resources = config.getMappedResources();
if(resources != null) {
for (IResource res : resources) {
if(resource.equals(res) ||
Expand Down

0 comments on commit 74aac75

Please sign in to comment.