Skip to content

Commit cadf586

Browse files
Guard lookup in BoundSet::allSuperPairsWithCommonGenericTypeRecursive (eclipse-jdt#3411)
correction: + use of .prototype() fails with ParameterizedTypeBinding + instead use the binding id for detecting visited types polish: + clarify logical relationship between code blocks + avoid instantiating lots of lists Contributes to eclipse-jdt#3327 --------- Co-authored-by: Stephan Herrmann <stephan.herrmann@berlin.de>
1 parent e9822a7 commit cadf586

File tree

1 file changed

+22
-17
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup

1 file changed

+22
-17
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/BoundSet.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,33 +1264,38 @@ private boolean superOnlyRaw(TypeBinding g, TypeBinding s, LookupEnvironment env
12641264
}
12651265

12661266
protected List<Pair<TypeBinding>> allSuperPairsWithCommonGenericType(TypeBinding s, TypeBinding t) {
1267-
return allSuperPairsWithCommonGenericTypeRecursive(s, t, new HashSet<>());
1267+
ArrayList<Pair<TypeBinding>> result = new ArrayList<>();
1268+
allSuperPairsWithCommonGenericTypeRecursive(s, t, result, new HashSet<>());
1269+
return result;
12681270
}
12691271

1270-
private List<Pair<TypeBinding>> allSuperPairsWithCommonGenericTypeRecursive(TypeBinding s, TypeBinding t, HashSet<TypeBinding> visited) {
1272+
private void allSuperPairsWithCommonGenericTypeRecursive(TypeBinding s, TypeBinding t, List<Pair<TypeBinding>> result, HashSet<Integer> visited) {
12711273
if (s == null || s.id == TypeIds.T_JavaLangObject || t == null || t.id == TypeIds.T_JavaLangObject)
1272-
return Collections.emptyList();
1273-
if (!visited.add(s.prototype()))
1274-
return Collections.emptyList();
1275-
List<Pair<TypeBinding>> result = new ArrayList<>();
1276-
if (s.isParameterizedType() && t.isParameterizedType() // optimization #1: clients of this method only want to compare type arguments
1277-
&& TypeBinding.equalsEquals(s.original(), t.original())) {
1278-
result.add(new Pair<>(s, t));
1279-
}
1274+
return;
1275+
if (!visited.add(s.id))
1276+
return;
1277+
1278+
// optimization: nothing interesting above equal types
12801279
if (TypeBinding.equalsEquals(s, t))
1281-
return result; // optimization #2: nothing interesting above equal types
1282-
TypeBinding tSuper = t.findSuperTypeOriginatingFrom(s);
1283-
if (tSuper != null && s.isParameterizedType() && tSuper.isParameterizedType()) { // optimization #1 again
1284-
result.add(new Pair<>(s, tSuper));
1280+
return;
1281+
1282+
if (s.isParameterizedType()) { // optimization here and below: clients of this method only want to compare type arguments
1283+
if (TypeBinding.equalsEquals(s.original(), t.original())) {
1284+
if (t.isParameterizedType())
1285+
result.add(new Pair<>(s, t));
1286+
} else {
1287+
TypeBinding tSuper = t.findSuperTypeOriginatingFrom(s);
1288+
if (tSuper != null && tSuper.isParameterizedType())
1289+
result.add(new Pair<>(s, tSuper));
1290+
}
12851291
}
1286-
result.addAll(allSuperPairsWithCommonGenericTypeRecursive(s.superclass(), t, visited));
1292+
allSuperPairsWithCommonGenericTypeRecursive(s.superclass(), t, result, visited);
12871293
ReferenceBinding[] superInterfaces = s.superInterfaces();
12881294
if (superInterfaces != null) {
12891295
for (ReferenceBinding superInterface : superInterfaces) {
1290-
result.addAll(allSuperPairsWithCommonGenericTypeRecursive(superInterface, t, visited));
1296+
allSuperPairsWithCommonGenericTypeRecursive(superInterface, t, result, visited);
12911297
}
12921298
}
1293-
return result;
12941299
}
12951300

12961301
public TypeBinding getEquivalentOuterVariable(InferenceVariable variable, InferenceVariable[] outerVariables) {

0 commit comments

Comments
 (0)