Skip to content

Commit 14f66ae

Browse files
ptzieglerHannesWell
authored andcommitted
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 ce6d9c2 commit 14f66ae

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

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

Lines changed: 46 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;
@@ -65,6 +66,38 @@
6566

6667
public class MinimalState {
6768

69+
private static final IPreferenceChangeListener PREF_CHANGE_LISTENER = e -> {
70+
if (e.getKey().equals("org.eclipse.jdt.launching.PREF_DEFAULT_ENVIRONMENTS_XML")) { //$NON-NLS-1$
71+
Object oldValue = e.getOldValue() == null ? "" : e.getOldValue(); //$NON-NLS-1$
72+
Object newValue = e.getNewValue() == null ? "" : e.getNewValue(); //$NON-NLS-1$
73+
if (!oldValue.equals(newValue)) {
74+
triggerSystemPackagesReload();
75+
}
76+
}
77+
};
78+
79+
private static final IVMInstallChangedListener VM_CHANGED_LISTENER = new IVMInstallChangedListener() {
80+
@Override
81+
public void vmRemoved(IVMInstall vm) {
82+
triggerSystemPackagesReload();
83+
}
84+
85+
@Override
86+
public void vmChanged(PropertyChangeEvent event) {
87+
triggerSystemPackagesReload();
88+
}
89+
90+
@Override
91+
public void vmAdded(IVMInstall vm) {
92+
triggerSystemPackagesReload();
93+
}
94+
95+
@Override
96+
public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {
97+
triggerSystemPackagesReload();
98+
}
99+
};
100+
68101
protected State fState;
69102

70103
protected long fId;
@@ -268,39 +301,20 @@ 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+
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+
@SuppressWarnings("restriction")
316+
private static IEclipsePreferences getJdtLaunchingPreferences() {
317+
return InstanceScope.INSTANCE.getNode(org.eclipse.jdt.internal.launching.LaunchingPlugin.ID_PLUGIN);
304318
}
305319

306320
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)