Skip to content

Commit 787ca9f

Browse files
committed
Don't rely on getKey to ensure bindings are unique
Instead make the binding be directly the key As 2 equals bindings in the map. As long as ashCode and equals are well implemented.
1 parent 21cf78f commit 787ca9f

File tree

1 file changed

+29
-58
lines changed

1 file changed

+29
-58
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@
4747
import com.sun.tools.javac.api.JavacTaskImpl;
4848
import com.sun.tools.javac.api.JavacTrees;
4949
import com.sun.tools.javac.code.Attribute;
50+
import com.sun.tools.javac.code.Attribute.Compound;
5051
import com.sun.tools.javac.code.ClassFinder;
5152
import com.sun.tools.javac.code.Symbol;
52-
import com.sun.tools.javac.code.Symtab;
53-
import com.sun.tools.javac.code.TypeTag;
54-
import com.sun.tools.javac.code.Types;
55-
import com.sun.tools.javac.code.Attribute.Compound;
5653
import com.sun.tools.javac.code.Symbol.ClassSymbol;
5754
import com.sun.tools.javac.code.Symbol.CompletionFailure;
5855
import com.sun.tools.javac.code.Symbol.MethodSymbol;
@@ -62,6 +59,7 @@
6259
import com.sun.tools.javac.code.Symbol.TypeSymbol;
6360
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
6461
import com.sun.tools.javac.code.Symbol.VarSymbol;
62+
import com.sun.tools.javac.code.Symtab;
6563
import com.sun.tools.javac.code.Type.ArrayType;
6664
import com.sun.tools.javac.code.Type.ClassType;
6765
import com.sun.tools.javac.code.Type.ErrorType;
@@ -73,9 +71,10 @@
7371
import com.sun.tools.javac.code.Type.ModuleType;
7472
import com.sun.tools.javac.code.Type.PackageType;
7573
import com.sun.tools.javac.code.Type.TypeVar;
74+
import com.sun.tools.javac.code.TypeTag;
75+
import com.sun.tools.javac.code.Types;
7676
import com.sun.tools.javac.comp.Modules;
7777
import com.sun.tools.javac.tree.JCTree;
78-
import com.sun.tools.javac.tree.TreeInfo;
7978
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
8079
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
8180
import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
@@ -101,6 +100,7 @@
101100
import com.sun.tools.javac.tree.JCTree.JCTypeUnion;
102101
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
103102
import com.sun.tools.javac.tree.JCTree.JCWildcard;
103+
import com.sun.tools.javac.tree.TreeInfo;
104104
import com.sun.tools.javac.util.Context;
105105
import com.sun.tools.javac.util.Names;
106106

@@ -130,26 +130,18 @@ public BindingKeyException(String message, Throwable cause) {
130130
}
131131

132132
public class Bindings {
133-
private Map<String, JavacAnnotationBinding> annotationBindings = new HashMap<>();
133+
private Map<JavacAnnotationBinding, JavacAnnotationBinding> annotationBindings = new HashMap<>();
134134
public JavacAnnotationBinding getAnnotationBinding(Compound ann, IBinding recipient) {
135135
JavacAnnotationBinding newInstance = new JavacAnnotationBinding(ann, JavacBindingResolver.this, recipient) { };
136-
String k = newInstance.getKey();
137-
if( k != null ) {
138-
annotationBindings.putIfAbsent(k, newInstance);
139-
return annotationBindings.get(k);
140-
}
141-
return null;
136+
annotationBindings.putIfAbsent(newInstance, newInstance);
137+
return annotationBindings.get(newInstance);
142138
}
143139
//
144-
private Map<String, JavacMemberValuePairBinding> memberValuePairBindings = new HashMap<>();
140+
private Map<JavacMemberValuePairBinding, JavacMemberValuePairBinding> memberValuePairBindings = new HashMap<>();
145141
public JavacMemberValuePairBinding getMemberValuePairBinding(MethodSymbol key, Attribute value) {
146142
JavacMemberValuePairBinding newInstance = new JavacMemberValuePairBinding(key, value, JavacBindingResolver.this) { };
147-
String k = newInstance.getKey();
148-
if( k != null ) {
149-
memberValuePairBindings.putIfAbsent(k, newInstance);
150-
return memberValuePairBindings.get(k);
151-
}
152-
return null;
143+
memberValuePairBindings.putIfAbsent(newInstance, newInstance);
144+
return memberValuePairBindings.get(newInstance);
153145
}
154146
//
155147
private Map<JavacMethodBinding, JavacMethodBinding> methodBindings = new HashMap<>();
@@ -179,38 +171,26 @@ private JavacMethodBinding insertAndReturn(JavacMethodBinding newInstance) {
179171
return methodBindings.get(newInstance);
180172
}
181173
//
182-
private Map<String, JavacModuleBinding> moduleBindings = new HashMap<>();
174+
private Map<JavacModuleBinding, JavacModuleBinding> moduleBindings = new HashMap<>();
183175
public JavacModuleBinding getModuleBinding(ModuleType moduleType) {
184176
JavacModuleBinding newInstance = new JavacModuleBinding(moduleType, JavacBindingResolver.this) { };
185-
String k = newInstance.getKey();
186-
if( k != null ) {
187-
moduleBindings.putIfAbsent(k, newInstance);
188-
return moduleBindings.get(k);
189-
}
190-
return null;
177+
moduleBindings.putIfAbsent(newInstance, newInstance);
178+
return moduleBindings.get(newInstance);
191179
}
192180
public JavacModuleBinding getModuleBinding(ModuleSymbol moduleSymbol) {
193181
JavacModuleBinding newInstance = new JavacModuleBinding(moduleSymbol, JavacBindingResolver.this) { };
194-
String k = newInstance.getKey();
195-
if( k != null ) {
196-
moduleBindings.putIfAbsent(k, newInstance);
197-
return moduleBindings.get(k);
198-
}
199-
return null;
182+
moduleBindings.putIfAbsent(newInstance, newInstance);
183+
return moduleBindings.get(newInstance);
200184
}
201185
public JavacModuleBinding getModuleBinding(JCModuleDecl moduleDecl) {
202186
JavacModuleBinding newInstance = new JavacModuleBinding(moduleDecl, JavacBindingResolver.this) { };
203187
// Overwrite existing
204-
String k = newInstance.getKey();
205-
if( k != null ) {
206-
moduleBindings.put(k, newInstance);
207-
return moduleBindings.get(k);
208-
}
209-
return null;
188+
moduleBindings.put(newInstance, newInstance);
189+
return moduleBindings.get(newInstance);
210190
}
211191

212192
//
213-
private Map<String, JavacPackageBinding> packageBindings = new HashMap<>();
193+
private Map<JavacPackageBinding, JavacPackageBinding> packageBindings = new HashMap<>();
214194
public JavacPackageBinding getPackageBinding(PackageSymbol packageSymbol) {
215195
if (!packageSymbol.exists()) {
216196
return null;
@@ -263,15 +243,14 @@ private JavacPackageBinding preferentiallyInsertPackageBinding(JavacPackageBindi
263243
// A package binding may be created while traversing something as simple as a name.
264244
// The binding using name-only logic should be instantiated, but
265245
// when a proper symbol is found, it should be added to that object.
266-
String k = newest == null ? null : newest.getKey();
267-
if( k != null ) {
268-
JavacPackageBinding current = packageBindings.get(k);
246+
if( newest != null ) {
247+
JavacPackageBinding current = packageBindings.get(newest);
269248
if( current == null ) {
270-
packageBindings.putIfAbsent(k, newest);
249+
packageBindings.putIfAbsent(newest, newest);
271250
} else if( current.getPackageSymbol() == null && newest.getPackageSymbol() != null) {
272251
current.setPackageSymbol(newest.getPackageSymbol());
273252
}
274-
return packageBindings.get(k);
253+
return packageBindings.get(newest);
275254
}
276255
return null;
277256
}
@@ -340,29 +319,21 @@ public JavacTypeVariableBinding getTypeVariableBinding(TypeVar typeVar) {
340319
return typeVariableBindings.get(newInstance);
341320
}
342321
//
343-
private Map<String, JavacVariableBinding> variableBindings = new HashMap<>();
322+
private Map<JavacVariableBinding, JavacVariableBinding> variableBindings = new HashMap<>();
344323
public JavacVariableBinding getVariableBinding(VarSymbol varSymbol) {
345324
if (varSymbol == null) {
346325
return null;
347326
}
348327
JavacVariableBinding newInstance = new JavacVariableBinding(varSymbol, JavacBindingResolver.this) { };
349-
String k = newInstance.getKey();
350-
if( k != null ) {
351-
variableBindings.putIfAbsent(k, newInstance);
352-
return variableBindings.get(k);
353-
}
354-
return null;
328+
variableBindings.putIfAbsent(newInstance, newInstance);
329+
return variableBindings.get(newInstance);
355330
}
356331
//
357-
private Map<String, JavacLambdaBinding> lambdaBindings = new HashMap<>();
332+
private Map<JavacLambdaBinding, JavacLambdaBinding> lambdaBindings = new HashMap<>();
358333
public JavacLambdaBinding getLambdaBinding(JavacMethodBinding javacMethodBinding, LambdaExpression lambda) {
359334
JavacLambdaBinding newInstance = new JavacLambdaBinding(javacMethodBinding, lambda);
360-
String k = newInstance.getKey();
361-
if( k != null ) {
362-
lambdaBindings.putIfAbsent(k, newInstance);
363-
return lambdaBindings.get(k);
364-
}
365-
return null;
335+
lambdaBindings.putIfAbsent(newInstance, newInstance);
336+
return lambdaBindings.get(newInstance);
366337
}
367338

368339
public IBinding getBinding(final Symbol owner, final com.sun.tools.javac.code.Type type) {

0 commit comments

Comments
 (0)