Skip to content

Commit

Permalink
ExceptionHandlingFlowContext: use java.util.IdentityHashMap
Browse files Browse the repository at this point in the history
* avoids raw types
* deletes custom ObjectCache

ObjectCache implemented a HashMap using hashCode() but not equals().
Instead it used == identity. JDK offers a special Map for identity since
1.4, consistently using System.identityHashCode().

relates to eclipse-jdt#3412

Also solved follow up
* LambdaExpression "Redundant specification of type arguments"
  • Loading branch information
EcljpseB0T committed Dec 10, 2024
1 parent d37105b commit bad35f0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@

import static org.eclipse.jdt.internal.compiler.ast.ExpressionContext.INVOCATION_CONTEXT;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -80,7 +78,6 @@
import org.eclipse.jdt.internal.compiler.problem.AbortType;
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;

@SuppressWarnings({"rawtypes", "unchecked"})
public class LambdaExpression extends FunctionalExpression implements IPolyExpression, ReferenceContext, ProblemSeverities {
public Argument [] arguments;
private TypeBinding [] argumentTypes;
Expand All @@ -101,7 +98,7 @@ public class LambdaExpression extends FunctionalExpression implements IPolyExpre
private int outerLocalVariablesSlotSize = 0;
private boolean assistNode = false;
private ReferenceBinding classType;
private Set thrownExceptions;
private Set<TypeBinding> thrownExceptions;
private static final SyntheticArgumentBinding [] NO_SYNTHETIC_ARGUMENTS = new SyntheticArgumentBinding[0];
private static final Block NO_BODY = new Block(0);
private HashMap<TypeBinding, LambdaExpression> copiesPerTargetType;
Expand Down Expand Up @@ -545,7 +542,7 @@ private void analyzeExceptions() {
this.body.analyseCode(this.scope,
ehfc = new ExceptionInferenceFlowContext(null, this, Binding.NO_EXCEPTIONS, null, this.scope, FlowInfo.DEAD_END),
UnconditionalFlowInfo.fakeInitializedFlowInfo(this.firstLocalLocal, this.scope.referenceType().maxFieldCount));
this.thrownExceptions = ehfc.extendedExceptions == null ? Collections.emptySet() : new HashSet<TypeBinding>(ehfc.extendedExceptions);
this.thrownExceptions = ehfc.extendedExceptions == null ? Set.of() : Set.copyOf(ehfc.extendedExceptions);
} catch (Exception e) {
// drop silently.
} finally {
Expand Down Expand Up @@ -1199,7 +1196,7 @@ public void tagAsHavingErrors() {

public Set<TypeBinding> getThrownExceptions() {
if (this.thrownExceptions == null)
return Collections.emptySet();
return Set.of();
return this.thrownExceptions;
}

Expand Down Expand Up @@ -1402,7 +1399,7 @@ public int diagnosticsSourceEnd() {

public TypeBinding[] getMarkerInterfaces() {
if (this.expectedType instanceof IntersectionTypeBinding18) {
Set markerBindings = new LinkedHashSet();
Set<TypeBinding> markerBindings = new LinkedHashSet<>();
IntersectionTypeBinding18 intersectionType = (IntersectionTypeBinding18)this.expectedType;
TypeBinding[] intersectionTypes = intersectionType.intersectingTypes;
TypeBinding samType = intersectionType.getSAMType(this.enclosingScope);
Expand All @@ -1416,7 +1413,7 @@ public TypeBinding[] getMarkerInterfaces() {
markerBindings.add(typeBinding);
}
if (markerBindings.size() > 0) {
return (TypeBinding[])markerBindings.toArray(new TypeBinding[markerBindings.size()]);
return markerBindings.toArray(TypeBinding[]::new);
}
}
return null;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
Expand All @@ -28,7 +29,6 @@
import org.eclipse.jdt.internal.compiler.ast.TryStatement;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.UnionTypeReference;
import org.eclipse.jdt.internal.compiler.codegen.ObjectCache;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.CatchParameterBinding;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
Expand All @@ -42,7 +42,6 @@
* Reflects the context of code analysis, keeping track of enclosing
* try statements, exception handlers, etc...
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class ExceptionHandlingFlowContext extends FlowContext {

public final static int BitCacheSize = 32; // 32 bits per int
Expand All @@ -52,14 +51,14 @@ public class ExceptionHandlingFlowContext extends FlowContext {
int[] isNeeded;
// WARNING: This is an array that maps to catch blocks, not caught exceptions (which could be more than catch blocks in a multi-catch block)
UnconditionalFlowInfo[] initsOnExceptions;
ObjectCache indexes = new ObjectCache();
IdentityHashMap<ReferenceBinding, Integer> indexes = new IdentityHashMap<>();
boolean isMethodContext;

public UnconditionalFlowInfo initsOnReturn;
public FlowContext initializationParent; // special parent relationship only for initialization purpose

// for dealing with anonymous constructor thrown exceptions
public List extendedExceptions;
public List<TypeBinding> extendedExceptions;

private static final Argument[] NO_ARGUMENTS = new Argument[0];
public Argument [] catchArguments;
Expand Down Expand Up @@ -255,7 +254,7 @@ public void mergeUnhandledException(TypeBinding newException){
boolean isRedundant = false;

for(int i = this.extendedExceptions.size()-1; i >= 0; i--){
switch(Scope.compareTypes(newException, (TypeBinding)this.extendedExceptions.get(i))){
switch(Scope.compareTypes(newException, this.extendedExceptions.get(i))){
case Scope.MORE_GENERIC :
this.extendedExceptions.remove(i);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public Object reduce(InferenceContext18 inferenceContext) {
LambdaExpression lambda = ((LambdaExpression) this.left).resolveExpressionExpecting(this.right, inferenceContext.scope, inferenceContext);
if (lambda == null)
return TRUE; // cannot make use of this buggy constraint
Set<TypeBinding> ePrimeSet = lambda.getThrownExceptions();
ePrime = ePrimeSet.toArray(new TypeBinding[ePrimeSet.size()]);
ePrime = lambda.getThrownExceptions().toArray(TypeBinding[]::new);
} else {
ReferenceExpression referenceExpression = ((ReferenceExpression) this.left).resolveExpressionExpecting(this.right, scope, inferenceContext);
MethodBinding method = referenceExpression != null ? referenceExpression.binding : null;
Expand Down

0 comments on commit bad35f0

Please sign in to comment.