Skip to content

Commit 5c89aec

Browse files
committed
Remove listeners in MinimalState if bundle is stopped
This change adapts the listeners that have been added with 24e9bb5 so that they are removed again, once the bundle is shut down and thus makes the classloader eligible for garbage collection.
1 parent 2decd24 commit 5c89aec

2 files changed

Lines changed: 52 additions & 33 deletions

File tree

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/MinimalState.java

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2005, 2024 IBM Corporation and others.
2+
* Copyright (c) 2005, 2025 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
@@ -37,6 +37,7 @@
3737
import org.eclipse.core.runtime.Status;
3838
import org.eclipse.core.runtime.jobs.Job;
3939
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
40+
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
4041
import org.eclipse.core.runtime.preferences.InstanceScope;
4142
import org.eclipse.jdt.core.IJavaProject;
4243
import org.eclipse.jdt.core.JavaCore;
@@ -82,6 +83,38 @@ public class MinimalState {
8283

8384
protected String fSystemBundle = IPDEBuildConstants.BUNDLE_OSGI;
8485

86+
private static final IPreferenceChangeListener PREF_CHANGE_LISTENER = e -> {
87+
if (e.getKey().equals("org.eclipse.jdt.launching.PREF_DEFAULT_ENVIRONMENTS_XML")) { //$NON-NLS-1$
88+
Object oldValue = e.getOldValue() == null ? "" : e.getOldValue(); //$NON-NLS-1$
89+
Object newValue = e.getNewValue() == null ? "" : e.getNewValue(); //$NON-NLS-1$
90+
if (!oldValue.equals(newValue)) {
91+
triggerSystemPackagesReload();
92+
}
93+
}
94+
};
95+
96+
private static final IVMInstallChangedListener VM_CHANGED_LISTENER = new IVMInstallChangedListener() {
97+
@Override
98+
public void vmRemoved(IVMInstall vm) {
99+
triggerSystemPackagesReload();
100+
}
101+
102+
@Override
103+
public void vmChanged(PropertyChangeEvent event) {
104+
triggerSystemPackagesReload();
105+
}
106+
107+
@Override
108+
public void vmAdded(IVMInstall vm) {
109+
triggerSystemPackagesReload();
110+
}
111+
112+
@Override
113+
public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {
114+
triggerSystemPackagesReload();
115+
}
116+
};
117+
85118
static {
86119
stateObjectFactory = BundleHelper.getPlatformAdmin().getFactory();
87120
}
@@ -268,39 +301,23 @@ protected boolean initializePlatformProperties() {
268301
static {
269302
// Listen to changes in the available VMInstalls and
270303
// ExecutionEnvironment defaults
271-
@SuppressWarnings("restriction")
272-
String nodeQualifier = org.eclipse.jdt.internal.launching.LaunchingPlugin.ID_PLUGIN;
273-
IEclipsePreferences launchingNode = InstanceScope.INSTANCE.getNode(nodeQualifier);
274-
launchingNode.addPreferenceChangeListener(e -> {
275-
if (e.getKey().equals("org.eclipse.jdt.launching.PREF_DEFAULT_ENVIRONMENTS_XML")) { //$NON-NLS-1$
276-
Object oldValue = e.getOldValue() == null ? "" : e.getOldValue(); //$NON-NLS-1$
277-
Object newValue = e.getNewValue() == null ? "" : e.getNewValue(); //$NON-NLS-1$
278-
if (!oldValue.equals(newValue)) {
279-
triggerSystemPackagesReload();
280-
}
281-
}
282-
});
283-
JavaRuntime.addVMInstallChangedListener(new IVMInstallChangedListener() {
284-
@Override
285-
public void vmRemoved(IVMInstall vm) {
286-
triggerSystemPackagesReload();
287-
}
288-
289-
@Override
290-
public void vmChanged(PropertyChangeEvent event) {
291-
triggerSystemPackagesReload();
292-
}
304+
IEclipsePreferences launchingNode = getJdtLaunchingPreferences();
305+
launchingNode.addPreferenceChangeListener(PREF_CHANGE_LISTENER);
306+
JavaRuntime.addVMInstallChangedListener(VM_CHANGED_LISTENER);
307+
}
293308

294-
@Override
295-
public void vmAdded(IVMInstall vm) {
296-
triggerSystemPackagesReload();
297-
}
309+
public static void shutdown() {
310+
IEclipsePreferences launchingNode = getJdtLaunchingPreferences();
311+
launchingNode.removePreferenceChangeListener(PREF_CHANGE_LISTENER);
312+
JavaRuntime.removeVMInstallChangedListener(VM_CHANGED_LISTENER);
313+
}
298314

299-
@Override
300-
public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {
301-
triggerSystemPackagesReload();
302-
}
303-
});
315+
private static IEclipsePreferences getJdtLaunchingPreferences() {
316+
// Listen to changes in the available VMInstalls and
317+
// ExecutionEnvironment defaults
318+
@SuppressWarnings("restriction")
319+
String nodeQualifier = org.eclipse.jdt.internal.launching.LaunchingPlugin.ID_PLUGIN;
320+
return InstanceScope.INSTANCE.getNode(nodeQualifier);
304321
}
305322

306323
public static void triggerSystemPackagesReload() {

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/PDECore.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2018 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 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
@@ -424,6 +424,8 @@ public void stop(BundleContext context) throws CoreException {
424424
IWorkspace workspace = ResourcesPlugin.getWorkspace();
425425
workspace.removeSaveParticipant(PLUGIN_ID);
426426
workspace.removeResourceChangeListener(bndResourceChangeListener);
427+
428+
MinimalState.shutdown();
427429
}
428430

429431
/**

0 commit comments

Comments
 (0)