From bad35f02e58e599b4d55d738407086642e2bcdcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Mon, 9 Dec 2024 16:48:45 +0100 Subject: [PATCH] ExceptionHandlingFlowContext: use java.util.IdentityHashMap * 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 https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3412 Also solved follow up * LambdaExpression "Redundant specification of type arguments" --- .../compiler/ast/LambdaExpression.java | 13 +- .../compiler/codegen/ObjectCache.java | 162 ------------------ .../flow/ExceptionHandlingFlowContext.java | 9 +- .../lookup/ConstraintExceptionFormula.java | 3 +- 4 files changed, 10 insertions(+), 177 deletions(-) delete mode 100644 org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/codegen/ObjectCache.java diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/LambdaExpression.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/LambdaExpression.java index 8eadf846dad..90504dd3d53 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/LambdaExpression.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/LambdaExpression.java @@ -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; @@ -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; @@ -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 thrownExceptions; private static final SyntheticArgumentBinding [] NO_SYNTHETIC_ARGUMENTS = new SyntheticArgumentBinding[0]; private static final Block NO_BODY = new Block(0); private HashMap copiesPerTargetType; @@ -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(ehfc.extendedExceptions); + this.thrownExceptions = ehfc.extendedExceptions == null ? Set.of() : Set.copyOf(ehfc.extendedExceptions); } catch (Exception e) { // drop silently. } finally { @@ -1199,7 +1196,7 @@ public void tagAsHavingErrors() { public Set getThrownExceptions() { if (this.thrownExceptions == null) - return Collections.emptySet(); + return Set.of(); return this.thrownExceptions; } @@ -1402,7 +1399,7 @@ public int diagnosticsSourceEnd() { public TypeBinding[] getMarkerInterfaces() { if (this.expectedType instanceof IntersectionTypeBinding18) { - Set markerBindings = new LinkedHashSet(); + Set markerBindings = new LinkedHashSet<>(); IntersectionTypeBinding18 intersectionType = (IntersectionTypeBinding18)this.expectedType; TypeBinding[] intersectionTypes = intersectionType.intersectingTypes; TypeBinding samType = intersectionType.getSAMType(this.enclosingScope); @@ -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; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/codegen/ObjectCache.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/codegen/ObjectCache.java deleted file mode 100644 index 42fef985aa3..00000000000 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/codegen/ObjectCache.java +++ /dev/null @@ -1,162 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.jdt.internal.compiler.codegen; - -public class ObjectCache { - public Object keyTable[]; - public int valueTable[]; - int elementSize; - int threshold; -/** - * Constructs a new, empty hashtable. A default capacity is used. - * Note that the hashtable will automatically grow when it gets full. - */ -public ObjectCache() { - this(13); -} -/** - * Constructs a new, empty hashtable with the specified initial - * capacity. - * @param initialCapacity int - * the initial number of buckets - */ -public ObjectCache(int initialCapacity) { - this.elementSize = 0; - this.threshold = (int) (initialCapacity * 0.66f); - this.keyTable = new Object[initialCapacity]; - this.valueTable = new int[initialCapacity]; -} -/** - * Clears the hash table so that it has no more elements in it. - */ -public void clear() { - for (int i = this.keyTable.length; --i >= 0;) { - this.keyTable[i] = null; - this.valueTable[i] = 0; - } - this.elementSize = 0; -} -/** Returns true if the collection contains an element for the key. - * - * @param key char[] the key that we are looking for - * @return boolean - */ -public boolean containsKey(Object key) { - int index = hashCode(key), length = this.keyTable.length; - while (this.keyTable[index] != null) { - if (this.keyTable[index] == key) - return true; - if (++index == length) { - index = 0; - } - } - return false; -} -/** Gets the object associated with the specified key in the - * hashtable. - * @param key char[] the specified key - * @return int the element for the key or -1 if the key is not - * defined in the hash table. - */ -public int get(Object key) { - int index = hashCode(key), length = this.keyTable.length; - while (this.keyTable[index] != null) { - if (this.keyTable[index] == key) - return this.valueTable[index]; - if (++index == length) { - index = 0; - } - } - return -1; -} -/** - * Return the hashcode for the key parameter - * - * @param key org.eclipse.jdt.internal.compiler.lookup.MethodBinding - * @return int - */ -public int hashCode(Object key) { - return (key.hashCode() & 0x7FFFFFFF) % this.keyTable.length; -} -/** - * Puts the specified element into the hashtable, using the specified - * key. The element may be retrieved by doing a get() with the same key. - * The key and the element cannot be null. - * - * @param key Object the specified key in the hashtable - * @param value int the specified element - * @return int the old value of the key, or -1 if it did not have one. - */ -public int put(Object key, int value) { - int index = hashCode(key), length = this.keyTable.length; - while (this.keyTable[index] != null) { - if (this.keyTable[index] == key) - return this.valueTable[index] = value; - if (++index == length) { - index = 0; - } - } - this.keyTable[index] = key; - this.valueTable[index] = value; - - // assumes the threshold is never equal to the size of the table - if (++this.elementSize > this.threshold) - rehash(); - return value; -} -/** - * Rehashes the content of the table into a bigger table. - * This method is called automatically when the hashtable's - * size exceeds the threshold. - */ -private void rehash() { - ObjectCache newHashtable = new ObjectCache(this.keyTable.length * 2); - for (int i = this.keyTable.length; --i >= 0;) - if (this.keyTable[i] != null) - newHashtable.put(this.keyTable[i], this.valueTable[i]); - - this.keyTable = newHashtable.keyTable; - this.valueTable = newHashtable.valueTable; - this.threshold = newHashtable.threshold; -} -/** - * Returns the number of elements contained in the hashtable. - * - * @return int The size of the table - */ -public int size() { - return this.elementSize; -} -/** - * Converts to a rather lengthy String. - * - * @return String the ascii representation of the receiver - */ -@Override -public String toString() { - int max = size(); - StringBuilder buf = new StringBuilder(); - buf.append("{"); //$NON-NLS-1$ - for (int i = 0; i < max; ++i) { - if (this.keyTable[i] != null) { - buf.append(this.keyTable[i]).append("->").append(this.valueTable[i]); //$NON-NLS-1$ - } - if (i < max) { - buf.append(", "); //$NON-NLS-1$ - } - } - buf.append("}"); //$NON-NLS-1$ - return buf.toString(); -} -} diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/flow/ExceptionHandlingFlowContext.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/flow/ExceptionHandlingFlowContext.java index 8eeccfb7ff2..5d8834aeed0 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/flow/ExceptionHandlingFlowContext.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/flow/ExceptionHandlingFlowContext.java @@ -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; @@ -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; @@ -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 @@ -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 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 extendedExceptions; private static final Argument[] NO_ARGUMENTS = new Argument[0]; public Argument [] catchArguments; @@ -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; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ConstraintExceptionFormula.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ConstraintExceptionFormula.java index 04e33d08660..7e38870175f 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ConstraintExceptionFormula.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ConstraintExceptionFormula.java @@ -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 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;