Skip to content

Commit d078b2c

Browse files
EcljpseB0Tjukzi
authored andcommitted
ProjectVariableProviderManager: fix concurrency issues
lazyInitialize used wrong double-checked locking idiom
1 parent 6718133 commit d078b2c

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectPathVariableManager.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.net.URISyntaxException;
2121
import java.util.Arrays;
2222
import java.util.HashMap;
23-
import java.util.Iterator;
2423
import java.util.LinkedList;
2524
import java.util.List;
2625
import org.eclipse.core.filesystem.URIUtil;
@@ -48,7 +47,7 @@
4847
public class ProjectPathVariableManager implements IPathVariableManager, IManager {
4948

5049
private final Resource resource;
51-
private ProjectVariableProviderManager.Descriptor variableProviders[] = null;
50+
private final ProjectVariableProviderManager.Descriptor variableProviders[];
5251

5352
/**
5453
* Constructor for the class.
@@ -182,19 +181,14 @@ public String internalGetValue(String varName) {
182181
@Override
183182
public boolean isDefined(String varName) {
184183
for (Descriptor variableProvider : variableProviders) {
185-
// if (variableProviders[i].getName().equals(varName))
186-
// return true;
187-
188184
if (varName.startsWith(variableProvider.getName()))
189185
return true;
190186
}
191187

192188
try {
193189
HashMap<String, VariableDescription> map = ((ProjectDescription) resource.getProject().getDescription()).getVariables();
194190
if (map != null) {
195-
Iterator<String> it = map.keySet().iterator();
196-
while (it.hasNext()) {
197-
String name = it.next();
191+
for (String name : map.keySet()) {
198192
if (name.equals(varName))
199193
return true;
200194
}
@@ -358,9 +352,7 @@ public void setURIValue(String varName, URI newValue) throws CoreException {
358352
}
359353
}
360354
boolean variableExists = currentValue != null;
361-
if (!variableExists && newValue == null)
362-
return;
363-
if (variableExists && currentValue.equals(newValue))
355+
if ((!variableExists && newValue == null) || (variableExists && currentValue.equals(newValue)))
364356
return;
365357

366358
for (Descriptor variableProvider : variableProviders) {

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectVariableProviderManager.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
import org.eclipse.core.resources.IResource;
2424
import org.eclipse.core.resources.ResourcesPlugin;
2525
import org.eclipse.core.resources.variableresolvers.PathVariableResolver;
26-
import org.eclipse.core.runtime.*;
26+
import org.eclipse.core.runtime.CoreException;
27+
import org.eclipse.core.runtime.IConfigurationElement;
28+
import org.eclipse.core.runtime.IExtension;
29+
import org.eclipse.core.runtime.IExtensionPoint;
30+
import org.eclipse.core.runtime.IStatus;
31+
import org.eclipse.core.runtime.Platform;
32+
import org.eclipse.core.runtime.Status;
2733
import org.eclipse.osgi.util.NLS;
2834

2935
/**
@@ -33,20 +39,22 @@
3339
public class ProjectVariableProviderManager {
3440

3541
public static class Descriptor {
36-
PathVariableResolver provider = null;
37-
String name = null;
38-
String value = null;
42+
private final PathVariableResolver provider;
43+
private final String name;
44+
private final String value;
3945

4046
public Descriptor(IExtension extension, IConfigurationElement element) throws RuntimeException, CoreException {
4147
name = element.getAttribute("variable"); //$NON-NLS-1$
4248
value = element.getAttribute("value"); //$NON-NLS-1$
49+
PathVariableResolver p = null;
4350
try {
4451
String classAttribute = "class"; //$NON-NLS-1$
4552
if (element.getAttribute(classAttribute) != null)
46-
provider = (PathVariableResolver) element.createExecutableExtension(classAttribute);
53+
p = (PathVariableResolver) element.createExecutableExtension(classAttribute);
4754
} catch (CoreException e) {
4855
Policy.log(e);
4956
}
57+
provider = p;
5058
if (name == null)
5159
fail(NLS.bind(Messages.mapping_invalidDef, extension.getUniqueIdentifier()));
5260
}
@@ -74,30 +82,25 @@ public String[] getVariableNames(String variable, IResource resource) {
7482
}
7583
}
7684

77-
private static Map<String, Descriptor> descriptors;
78-
private static Descriptor[] descriptorsArray;
79-
private static ProjectVariableProviderManager instance = new ProjectVariableProviderManager();
85+
private static final Map<String, Descriptor> descriptors = getDescriptorMap();
86+
private static final Descriptor[] descriptorsArray = descriptors.values().toArray(Descriptor[]::new);
87+
private static final ProjectVariableProviderManager instance = new ProjectVariableProviderManager();
8088

8189
public static ProjectVariableProviderManager getDefault() {
8290
return instance;
8391
}
8492

8593
public Descriptor[] getDescriptors() {
86-
lazyInitialize();
8794
return descriptorsArray;
8895
}
8996

90-
protected void lazyInitialize() {
91-
if (descriptors != null)
92-
return;
97+
private static Map<String, Descriptor> getDescriptorMap() {
9398
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_VARIABLE_PROVIDERS);
9499
IExtension[] extensions = point.getExtensions();
95-
descriptors = new HashMap<>(extensions.length * 2 + 1);
100+
Map<String, Descriptor> d = new HashMap<>(extensions.length * 2 + 1);
96101
for (IExtension extension : extensions) {
97102
IConfigurationElement[] elements = extension.getConfigurationElements();
98-
int count = elements.length;
99-
for (int j = 0; j < count; j++) {
100-
IConfigurationElement element = elements[j];
103+
for (IConfigurationElement element : elements) {
101104
String elementName = element.getName();
102105
if (elementName.equalsIgnoreCase("variableResolver")) { //$NON-NLS-1$
103106
Descriptor desc = null;
@@ -107,15 +110,14 @@ protected void lazyInitialize() {
107110
Policy.log(e);
108111
}
109112
if (desc != null)
110-
descriptors.put(desc.getName(), desc);
113+
d.put(desc.getName(), desc);
111114
}
112115
}
113116
}
114-
descriptorsArray = descriptors.values().toArray(new Descriptor[descriptors.size()]);
117+
return Map.copyOf(d);
115118
}
116119

117120
public Descriptor findDescriptor(String name) {
118-
lazyInitialize();
119121
Descriptor result = descriptors.get(name);
120122
return result;
121123
}

0 commit comments

Comments
 (0)