From 75188459b67c14c46ad4fde2548ea8dabbef1390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Fri, 6 Dec 2024 14:14:00 +0100 Subject: [PATCH] When is ReferenceBinding.hashCode() used outside HashMap? https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3412 --- .../compiler/ast/AllocationExpression.java | 14 ++--- .../internal/compiler/ast/MessageSend.java | 14 ++--- .../flow/ExceptionHandlingFlowContext.java | 5 +- .../compiler/lookup/BinaryTypeBinding.java | 9 +-- .../compiler/lookup/LookupEnvironment.java | 58 +++++++++-------- .../compiler/lookup/MethodVerifier.java | 38 ++++++----- .../compiler/lookup/MethodVerifier15.java | 9 +-- .../compiler/lookup/ModuleBinding.java | 25 ++++---- .../compiler/lookup/ReferenceBinding.java | 38 +++++++---- .../jdt/internal/compiler/lookup/Scope.java | 17 +++-- .../compiler/lookup/SourceModuleBinding.java | 6 +- .../compiler/lookup/SourceTypeBinding.java | 8 +-- .../compiler/lookup/TypeBindingVisitor.java | 9 +-- .../internal/compiler/lookup/TypeSystem.java | 21 +++---- .../codeassist/ThrownExceptionFinder.java | 63 ++++++++----------- .../jdt/internal/core/builder/State.java | 20 ++---- 16 files changed, 169 insertions(+), 185 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java index fdd29789637..a0c69c9a652 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java @@ -59,6 +59,7 @@ import static org.eclipse.jdt.internal.compiler.ast.ExpressionContext.VANILLA_CONTEXT; import java.util.HashMap; +import java.util.Map; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.internal.compiler.ASTVisitor; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; @@ -71,7 +72,6 @@ import org.eclipse.jdt.internal.compiler.impl.JavaFeature; import org.eclipse.jdt.internal.compiler.lookup.*; import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; public class AllocationExpression extends Expression implements IPolyExpression, Invocation { @@ -89,7 +89,7 @@ public class AllocationExpression extends Expression implements IPolyExpression, public ExpressionContext expressionContext = VANILLA_CONTEXT; // hold on to this context from invocation applicability inference until invocation type inference (per method candidate): - private SimpleLookupTable/**/ inferenceContexts; + private Map inferenceContexts; public HashMap solutionsPerTargetType; private InferenceContext18 outerInferenceContext; // resolving within the context of an outer (lambda) inference? public boolean argsContainCast; @@ -826,7 +826,7 @@ public Expression[] arguments() { @Override public void registerInferenceContext(ParameterizedGenericMethodBinding method, InferenceContext18 infCtx18) { if (this.inferenceContexts == null) - this.inferenceContexts = new SimpleLookupTable(); + this.inferenceContexts = new HashMap<>(); this.inferenceContexts.put(method, infCtx18); } @@ -843,16 +843,16 @@ public void registerResult(TypeBinding targetType, MethodBinding method) { public InferenceContext18 getInferenceContext(ParameterizedMethodBinding method) { if (this.inferenceContexts == null) return null; - return (InferenceContext18) this.inferenceContexts.get(method); + return this.inferenceContexts.get(method); } @Override public void cleanUpInferenceContexts() { if (this.inferenceContexts == null) return; - for (Object value : this.inferenceContexts.valueTable) - if (value != null) - ((InferenceContext18) value).cleanUp(); + for (InferenceContext18 inferenceContext : this.inferenceContexts.values()) { + inferenceContext.cleanUp(); + } this.inferenceContexts = null; this.outerInferenceContext = null; this.solutionsPerTargetType = null; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/MessageSend.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/MessageSend.java index 7932215dd15..388bc846796 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/MessageSend.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/MessageSend.java @@ -70,6 +70,7 @@ import static org.eclipse.jdt.internal.compiler.ast.ExpressionContext.VANILLA_CONTEXT; import java.util.HashMap; +import java.util.Map; import java.util.function.BiConsumer; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ASTVisitor; @@ -86,7 +87,6 @@ import org.eclipse.jdt.internal.compiler.impl.ReferenceContext; import org.eclipse.jdt.internal.compiler.lookup.*; import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; public class MessageSend extends Expression implements IPolyExpression, Invocation { @@ -106,7 +106,7 @@ public class MessageSend extends Expression implements IPolyExpression, Invocati public ExpressionContext expressionContext = VANILLA_CONTEXT; // hold on to this context from invocation applicability inference until invocation type inference (per method candidate): - private SimpleLookupTable/**/ inferenceContexts; + private Map inferenceContexts; private HashMap solutionsPerTargetType; private InferenceContext18 outerInferenceContext; // resolving within the context of an outer (lambda) inference? @@ -1312,7 +1312,7 @@ public void registerInferenceContext(ParameterizedGenericMethodBinding method, I System.out.println("Register inference context of "+this+" for "+method+":\n"+infCtx18); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } if (this.inferenceContexts == null) - this.inferenceContexts = new SimpleLookupTable(); + this.inferenceContexts = new HashMap<>(); this.inferenceContexts.put(method, infCtx18); } @@ -1331,7 +1331,7 @@ public void registerResult(TypeBinding targetType, MethodBinding method) { public InferenceContext18 getInferenceContext(ParameterizedMethodBinding method) { InferenceContext18 context = null; if (this.inferenceContexts != null) - context = (InferenceContext18) this.inferenceContexts.get(method); + context = this.inferenceContexts.get(method); if (InferenceContext18.DEBUG) { System.out.println("Retrieve inference context of "+this+" for "+method+":\n"+context); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } @@ -1341,9 +1341,9 @@ public InferenceContext18 getInferenceContext(ParameterizedMethodBinding method) public void cleanUpInferenceContexts() { if (this.inferenceContexts == null) return; - for (Object value : this.inferenceContexts.valueTable) - if (value != null) - ((InferenceContext18) value).cleanUp(); + for (InferenceContext18 inferenceContext : this.inferenceContexts.values()) { + inferenceContext.cleanUp(); + } this.inferenceContexts = null; this.outerInferenceContext = null; this.solutionsPerTargetType = null; 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..80818d2831e 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,7 +20,9 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Argument; @@ -28,7 +30,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; @@ -52,7 +53,7 @@ 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(); + Map indexes = new HashMap<>(); boolean isMethodContext; public UnconditionalFlowInfo initsOnReturn; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java index e5f6ffa46a2..09fa2b086d7 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java @@ -51,6 +51,8 @@ import java.net.URI; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.classfmt.AnnotationInfo; @@ -66,7 +68,6 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.impl.Constant; import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; import org.eclipse.jdt.internal.compiler.util.Util; /* @@ -104,7 +105,7 @@ public class BinaryTypeBinding extends ReferenceBinding { // For the link with the principle structure protected LookupEnvironment environment; - protected SimpleLookupTable storedAnnotations = null; // keys are this ReferenceBinding & its fields and methods, value is an AnnotationHolder + protected Map storedAnnotations = null; // keys are this ReferenceBinding & its fields and methods, value is an AnnotationHolder private ReferenceBinding containerAnnotationType; int defaultNullness = 0; @@ -1976,7 +1977,7 @@ public void tagAsHavingDefectiveContainerType() { } @Override -SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) { +Map storedAnnotations(boolean forceInitialize, boolean forceStore) { if (!isPrototype()) return this.prototype.storedAnnotations(forceInitialize, forceStore); @@ -1984,7 +1985,7 @@ SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) if (forceInitialize && this.storedAnnotations == null) { if (!this.environment.globalOptions.storeAnnotations && !forceStore) return null; // not supported during this compile - this.storedAnnotations = new SimpleLookupTable(3); + this.storedAnnotations = new HashMap<>(); } return this.storedAnnotations; } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java index 303fe765988..2cee6f160a7 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java @@ -71,15 +71,13 @@ import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; import org.eclipse.jdt.internal.compiler.util.HashtableOfModule; import org.eclipse.jdt.internal.compiler.util.HashtableOfPackage; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; -@SuppressWarnings({"rawtypes", "unchecked"}) public class LookupEnvironment implements ProblemReasons, TypeConstants { /** * Map from typeBinding -> accessRestriction rule */ - private Map accessRestrictions; + private Map accessRestrictions; ImportBinding[] defaultImports; // ROOT_ONLY /** * The root environment driving the current compilation. @@ -109,11 +107,11 @@ public class LookupEnvironment implements ProblemReasons, TypeConstants { private CompleteTypeBindingsSteps stepCompleted = CompleteTypeBindingsSteps.NONE; // ROOT_ONLY public ITypeRequestor typeRequestor; // SHARED - private SimpleLookupTable uniqueParameterizedGenericMethodBindings; + private Map uniqueParameterizedGenericMethodBindings; // key is a string with the method selector value is an array of method bindings - private SimpleLookupTable uniquePolymorphicMethodBindings; - private SimpleLookupTable uniqueGetClassMethodBinding; // https://bugs.eclipse.org/bugs/show_bug.cgi?id=300734 + private Map uniquePolymorphicMethodBindings; + private Map uniqueGetClassMethodBinding; // https://bugs.eclipse.org/bugs/show_bug.cgi?id=300734 boolean useModuleSystem; // true when compliance >= 9 and nameEnvironment is module aware // key is a string with the module name value is a module binding @@ -124,7 +122,7 @@ public class LookupEnvironment implements ProblemReasons, TypeConstants { private CompilationUnitDeclaration[] units = new CompilationUnitDeclaration[4]; // ROOT_ONLY private MethodVerifier verifier; - private ArrayList missingTypes; + private ArrayList missingTypes; Set typesBeingConnected; // SHARED public boolean isProcessingAnnotations = false; // ROOT_ONLY public boolean mayTolerateMissingType = false; @@ -214,11 +212,11 @@ public LookupEnvironment(ITypeRequestor typeRequestor, CompilerOptions globalOpt this.defaultPackage = new PlainPackageBinding(this); // assume the default package always exists this.defaultImports = null; this.nameEnvironment = nameEnvironment; - this.knownPackages = new HashtableOfPackage(); - this.uniqueParameterizedGenericMethodBindings = new SimpleLookupTable(3); - this.uniquePolymorphicMethodBindings = new SimpleLookupTable(3); + this.knownPackages = new HashtableOfPackage<>(); + this.uniqueParameterizedGenericMethodBindings = new HashMap<>(); + this.uniquePolymorphicMethodBindings = new HashMap<>(); this.missingTypes = null; - this.accessRestrictions = new HashMap(3); + this.accessRestrictions = new HashMap<>(3); this.classFilePool = ClassFilePool.newInstance(); this.typesBeingConnected = new LinkedHashSet<>(); this.deferredEnumMethods = new ArrayList<>(); @@ -239,11 +237,11 @@ public LookupEnvironment(ITypeRequestor typeRequestor, CompilerOptions globalOpt this.defaultPackage = new PlainPackageBinding(this); // assume the default package always exists this.defaultImports = null; this.nameEnvironment = rootEnv.nameEnvironment; - this.knownPackages = new HashtableOfPackage(); - this.uniqueParameterizedGenericMethodBindings = new SimpleLookupTable(3); - this.uniquePolymorphicMethodBindings = new SimpleLookupTable(3); + this.knownPackages = new HashtableOfPackage<>(); + this.uniqueParameterizedGenericMethodBindings = new HashMap<>(); + this.uniquePolymorphicMethodBindings = new HashMap<>(); this.missingTypes = null; - this.accessRestrictions = new HashMap(3); + this.accessRestrictions = new HashMap<>(); this.classFilePool = rootEnv.classFilePool; this.typesBeingConnected = rootEnv.typesBeingConnected; this.deferredEnumMethods = rootEnv.deferredEnumMethods; @@ -1116,7 +1114,7 @@ public MissingTypeBinding createMissingType(PackageBinding packageBinding, char[ } packageBinding.addType(missingType); if (this.missingTypes == null) - this.missingTypes = new ArrayList(3); + this.missingTypes = new ArrayList<>(); this.missingTypes.add(missingType); return missingType; } @@ -1194,7 +1192,7 @@ public PlainPackageBinding createPlainPackage(char[][] compoundName) { public ParameterizedGenericMethodBinding createParameterizedGenericMethod(MethodBinding genericMethod, RawTypeBinding rawType) { // cached info is array of already created parameterized types for this type - ParameterizedGenericMethodBinding[] cachedInfo = (ParameterizedGenericMethodBinding[])this.uniqueParameterizedGenericMethodBindings.get(genericMethod); + ParameterizedGenericMethodBinding[] cachedInfo = this.uniqueParameterizedGenericMethodBindings.get(genericMethod); boolean needToGrow = false; int index = 0; if (cachedInfo != null){ @@ -1234,7 +1232,7 @@ public ParameterizedGenericMethodBinding createParameterizedGenericMethod(Method boolean inferredWithUncheckedConversion, boolean hasReturnProblem, TypeBinding targetType) { // cached info is array of already created parameterized types for this type - ParameterizedGenericMethodBinding[] cachedInfo = (ParameterizedGenericMethodBinding[])this.uniqueParameterizedGenericMethodBindings.get(genericMethod); + ParameterizedGenericMethodBinding[] cachedInfo = this.uniqueParameterizedGenericMethodBindings.get(genericMethod); int argLength = typeArguments == null ? 0: typeArguments.length; boolean needToGrow = false; int index = 0; @@ -1282,7 +1280,7 @@ public ParameterizedGenericMethodBinding createParameterizedGenericMethod(Method public PolymorphicMethodBinding createPolymorphicMethod(MethodBinding originalPolymorphicMethod, TypeBinding[] parameters, Scope scope) { // cached info is array of already created polymorphic methods for this type String key = new String(originalPolymorphicMethod.selector); - PolymorphicMethodBinding[] cachedInfo = (PolymorphicMethodBinding[]) this.uniquePolymorphicMethodBindings.get(key); + PolymorphicMethodBinding[] cachedInfo = this.uniquePolymorphicMethodBindings.get(key); int parametersLength = parameters == null ? 0: parameters.length; TypeBinding[] parametersTypeBinding = new TypeBinding[parametersLength]; for (int i = 0; i < parametersLength; i++) { @@ -1343,7 +1341,7 @@ public boolean usesAnnotatedTypeSystem() { public MethodBinding updatePolymorphicMethodReturnType(PolymorphicMethodBinding binding, TypeBinding typeBinding) { // update the return type to be the given return type, but reuse existing binding if one can match String key = new String(binding.selector); - PolymorphicMethodBinding[] cachedInfo = (PolymorphicMethodBinding[]) this.uniquePolymorphicMethodBindings.get(key); + PolymorphicMethodBinding[] cachedInfo = this.uniquePolymorphicMethodBindings.get(key); boolean needToGrow = false; int index = 0; TypeBinding[] parameters = binding.parameters; @@ -1382,9 +1380,9 @@ public ParameterizedMethodBinding createGetClassMethod(TypeBinding receiverType, // see if we have already cached this method for the given receiver type. ParameterizedMethodBinding retVal = null; if (this.uniqueGetClassMethodBinding == null) { - this.uniqueGetClassMethodBinding = new SimpleLookupTable(3); + this.uniqueGetClassMethodBinding = new HashMap<>(); } else { - retVal = (ParameterizedMethodBinding)this.uniqueGetClassMethodBinding.get(receiverType); + retVal = this.uniqueGetClassMethodBinding.get(receiverType); } if (retVal == null) { retVal = ParameterizedMethodBinding.instantiateGetClass(receiverType, originalMethod, scope); @@ -1496,7 +1494,7 @@ public WildcardBinding createWildcard(ReferenceBinding genericType, int rank, Ty * Returns the access restriction associated to a given type, or null if none */ public AccessRestriction getAccessRestriction(TypeBinding type) { - return (AccessRestriction) this.accessRestrictions.get(type); + return this.accessRestrictions.get(type); } /** @@ -1892,7 +1890,7 @@ else if ((referenceBinding = packageBinding.getType0(compoundName[compoundName.l private TypeBinding[] getTypeArgumentsFromSignature(SignatureWrapper wrapper, TypeVariableBinding[] staticVariables, ReferenceBinding enclosingType, ReferenceBinding genericType, char[][][] missingTypeNames, ITypeAnnotationWalker walker) { - java.util.ArrayList args = new java.util.ArrayList(2); + ArrayList args = new ArrayList<>(); int rank = 0; do { args.add(getTypeFromVariantTypeSignature(wrapper, staticVariables, enclosingType, genericType, rank, missingTypeNames, @@ -2274,7 +2272,7 @@ TypeBinding getTypeFromVariantTypeSignature( boolean isMissingType(char[] typeName) { for (int i = this.missingTypes == null ? 0 : this.missingTypes.size(); --i >= 0;) { - MissingTypeBinding missingType = (MissingTypeBinding) this.missingTypes.get(i); + MissingTypeBinding missingType = this.missingTypes.get(i); if (CharOperation.equals(missingType.sourceName, typeName)) return true; } @@ -2316,17 +2314,17 @@ public void reset() { this.defaultPackage = new PlainPackageBinding(this); // assume the default package always exists this.defaultImports = null; - this.knownPackages = new HashtableOfPackage(); - this.accessRestrictions = new HashMap(3); + this.knownPackages = new HashtableOfPackage<>(); + this.accessRestrictions = new HashMap<>(); this.verifier = null; // NOTE: remember to fix #updateCaches(...) when adding unique binding caches - this.uniqueParameterizedGenericMethodBindings = new SimpleLookupTable(3); - this.uniquePolymorphicMethodBindings = new SimpleLookupTable(3); + this.uniqueParameterizedGenericMethodBindings = new HashMap<>(); + this.uniquePolymorphicMethodBindings = new HashMap<>(); this.uniqueGetClassMethodBinding = null; this.missingTypes = null; - this.typesBeingConnected = new LinkedHashSet(); + this.typesBeingConnected = new LinkedHashSet<>(); for (int i = this.units.length; --i >= 0;) this.units[i] = null; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier.java index ca6c3dee197..4d1ee5e6bd0 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier.java @@ -36,7 +36,6 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; import org.eclipse.jdt.internal.compiler.util.HashtableOfObject; -import org.eclipse.jdt.internal.compiler.util.SimpleSet; import org.eclipse.jdt.internal.compiler.util.Sorting; @SuppressWarnings({"rawtypes", "unchecked"}) @@ -295,16 +294,16 @@ void checkForMissingHashCodeMethod() { void checkForRedundantSuperinterfaces(ReferenceBinding superclass, ReferenceBinding[] superInterfaces) { if (superInterfaces == Binding.NO_SUPERINTERFACES) return; - SimpleSet interfacesToCheck = new SimpleSet(superInterfaces.length); - SimpleSet redundantInterfaces = null; // bark but once. + Set interfacesToCheck = new HashSet<>(superInterfaces.length); + Set redundantInterfaces = null; // bark but once. for (int i = 0, l = superInterfaces.length; i < l; i++) { ReferenceBinding toCheck = superInterfaces[i]; for (int j = 0; j < l; j++) { ReferenceBinding implementedInterface = superInterfaces[j]; if (i != j && toCheck.implementsInterface(implementedInterface, true)) { if (redundantInterfaces == null) { - redundantInterfaces = new SimpleSet(3); - } else if (redundantInterfaces.includes(implementedInterface)) { + redundantInterfaces = new HashSet<>(); + } else if (redundantInterfaces.contains(implementedInterface)) { continue; } redundantInterfaces.add(implementedInterface); @@ -321,16 +320,16 @@ void checkForRedundantSuperinterfaces(ReferenceBinding superclass, ReferenceBind } ReferenceBinding[] itsInterfaces = null; - SimpleSet inheritedInterfaces = new SimpleSet(5); + Set inheritedInterfaces = new HashSet<>(); ReferenceBinding superType = superclass; while (superType != null && superType.isValidBinding()) { if ((itsInterfaces = superType.superInterfaces()) != Binding.NO_SUPERINTERFACES) { for (ReferenceBinding inheritedInterface : itsInterfaces) { - if (!inheritedInterfaces.includes(inheritedInterface) && inheritedInterface.isValidBinding()) { - if (interfacesToCheck.includes(inheritedInterface)) { + if (!inheritedInterfaces.contains(inheritedInterface) && inheritedInterface.isValidBinding()) { + if (interfacesToCheck.contains(inheritedInterface)) { if (redundantInterfaces == null) { - redundantInterfaces = new SimpleSet(3); - } else if (redundantInterfaces.includes(inheritedInterface)) { + redundantInterfaces = new HashSet<>(); + } else if (redundantInterfaces.contains(inheritedInterface)) { continue; } redundantInterfaces.add(inheritedInterface); @@ -350,10 +349,9 @@ void checkForRedundantSuperinterfaces(ReferenceBinding superclass, ReferenceBind superType = superType.superclass(); } - int nextPosition = inheritedInterfaces.elementSize; + int nextPosition = inheritedInterfaces.size(); if (nextPosition == 0) return; - ReferenceBinding[] interfacesToVisit = new ReferenceBinding[nextPosition]; - inheritedInterfaces.asArray(interfacesToVisit); + ReferenceBinding[] interfacesToVisit = inheritedInterfaces.toArray(ReferenceBinding[]::new); for (int i = 0; i < nextPosition; i++) { superType = interfacesToVisit[i]; if ((itsInterfaces = superType.superInterfaces()) != Binding.NO_SUPERINTERFACES) { @@ -362,11 +360,11 @@ void checkForRedundantSuperinterfaces(ReferenceBinding superclass, ReferenceBind System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[nextPosition + itsLength + 5], 0, nextPosition); for (int a = 0; a < itsLength; a++) { ReferenceBinding inheritedInterface = itsInterfaces[a]; - if (!inheritedInterfaces.includes(inheritedInterface) && inheritedInterface.isValidBinding()) { - if (interfacesToCheck.includes(inheritedInterface)) { + if (!inheritedInterfaces.contains(inheritedInterface) && inheritedInterface.isValidBinding()) { + if (interfacesToCheck.contains(inheritedInterface)) { if (redundantInterfaces == null) { - redundantInterfaces = new SimpleSet(3); - } else if (redundantInterfaces.includes(inheritedInterface)) { + redundantInterfaces = new HashSet<>(); + } else if (redundantInterfaces.contains(inheritedInterface)) { continue; } redundantInterfaces.add(inheritedInterface); @@ -632,12 +630,12 @@ void computeInheritedMethods(ReferenceBinding superclass, ReferenceBinding[] sup superInterfaces = Sorting.sortTypes(superInterfaces); } - SimpleSet skip = findSuperinterfaceCollisions(superclass, superInterfaces); + Set skip = findSuperinterfaceCollisions(superclass, superInterfaces); int len = superInterfaces.length; for (int i = len-1; i >= 0; i--) { superType = superInterfaces[i]; if (superType.isValidBinding()) { - if (skip != null && skip.includes(superType)) continue; + if (skip != null && skip.contains(superType)) continue; MethodBinding[] methods = superType.unResolvedMethods(); nextMethod : for (int m = methods.length; --m >= 0;) { // Interface methods are all abstract public @@ -792,7 +790,7 @@ public boolean doesMethodOverride(MethodBinding method, MethodBinding inheritedM public static boolean doesMethodOverride(MethodBinding method, MethodBinding inheritedMethod, LookupEnvironment environment) { return couldMethodOverride(method, inheritedMethod) && areMethodsCompatible(method, inheritedMethod, environment); } -SimpleSet findSuperinterfaceCollisions(ReferenceBinding superclass, ReferenceBinding[] superInterfaces) { +Set findSuperinterfaceCollisions(ReferenceBinding superclass, ReferenceBinding[] superInterfaces) { return null; // noop in 1.4 } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier15.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier15.java index 6aeff70df64..fe18857731f 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier15.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/MethodVerifier15.java @@ -37,6 +37,8 @@ import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Argument; @@ -47,7 +49,6 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities; import org.eclipse.jdt.internal.compiler.util.HashtableOfObject; -import org.eclipse.jdt.internal.compiler.util.SimpleSet; import org.eclipse.jdt.internal.compiler.util.Sorting; class MethodVerifier15 extends MethodVerifier { @@ -864,7 +865,7 @@ boolean doTypeVariablesClash(MethodBinding one, MethodBinding substituteTwo) { return one.typeVariables != Binding.NO_TYPE_VARIABLES && !(substituteTwo instanceof ParameterizedGenericMethodBinding); } @Override -SimpleSet findSuperinterfaceCollisions(ReferenceBinding superclass, ReferenceBinding[] superInterfaces) { +Set findSuperinterfaceCollisions(ReferenceBinding superclass, ReferenceBinding[] superInterfaces) { ReferenceBinding[] interfacesToVisit = null; int nextPosition = 0; ReferenceBinding[] itsInterfaces = superInterfaces; @@ -915,7 +916,7 @@ SimpleSet findSuperinterfaceCollisions(ReferenceBinding superclass, ReferenceBin } if (!isInconsistent) return null; // hierarchy is consistent so no collisions are possible - SimpleSet copy = null; + Set copy = null; for (int i = 0; i < nextPosition; i++) { ReferenceBinding current = interfacesToVisit[i]; if (current.isValidBinding()) { @@ -924,7 +925,7 @@ SimpleSet findSuperinterfaceCollisions(ReferenceBinding superclass, ReferenceBin ReferenceBinding next = interfacesToVisit[j]; if (next.isValidBinding() && TypeBinding.equalsEquals(next.erasure(), erasure)) { if (copy == null) - copy = new SimpleSet(nextPosition); + copy = new HashSet<>(nextPosition); copy.add(interfacesToVisit[i]); copy.add(interfacesToVisit[j]); } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java index f50b8c38589..6675b6bfa73 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java @@ -31,7 +31,6 @@ import org.eclipse.jdt.internal.compiler.env.IModuleAwareNameEnvironment; import org.eclipse.jdt.internal.compiler.env.IUpdatableModule; import org.eclipse.jdt.internal.compiler.util.HashtableOfPackage; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; import org.eclipse.jdt.internal.compiler.util.SimpleSetOfCharArray; /** @@ -151,7 +150,7 @@ public String toString() { boolean isAuto = false; private final boolean[] isComplete = new boolean[UpdateKind.values().length]; private Set transitiveRequires; - SimpleLookupTable storedAnnotations = null; + Map storedAnnotations = null; /** * Packages declared in this module (indexed by qualified name). @@ -866,18 +865,18 @@ public int getDefaultNullness() { getAnnotationTagBits(); // ensure annotations are initialized return this.defaultNullness; } - SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) { + Map storedAnnotations(boolean forceInitialize, boolean forceStore) { if (forceInitialize && this.storedAnnotations == null) { if (!this.environment.globalOptions.storeAnnotations && !forceStore) return null; // not supported during this compile - this.storedAnnotations = new SimpleLookupTable(3); + this.storedAnnotations = new HashMap<>(); } return this.storedAnnotations; } public AnnotationHolder retrieveAnnotationHolder(Binding binding, boolean forceInitialization) { - SimpleLookupTable store = storedAnnotations(forceInitialization, false); - return store == null ? null : (AnnotationHolder) store.get(binding); + Map store = storedAnnotations(forceInitialization, false); + return store == null ? null : store.get(binding); } AnnotationBinding[] retrieveAnnotations(Binding binding) { @@ -891,11 +890,11 @@ public void setAnnotations(AnnotationBinding[] annotations, boolean forceStore) } void storeAnnotationHolder(Binding binding, AnnotationHolder holder) { if (holder == null) { - SimpleLookupTable store = storedAnnotations(false, false); + Map store = storedAnnotations(false, false); if (store != null) - store.removeKey(binding); + store.remove(binding); } else { - SimpleLookupTable store = storedAnnotations(true, false); + Map store = storedAnnotations(true, false); if (store != null) store.put(binding, holder); } @@ -904,14 +903,14 @@ void storeAnnotationHolder(Binding binding, AnnotationHolder holder) { void storeAnnotations(Binding binding, AnnotationBinding[] annotations, boolean forceStore) { AnnotationHolder holder = null; if (annotations == null || annotations.length == 0) { - SimpleLookupTable store = storedAnnotations(false, forceStore); + Map store = storedAnnotations(false, forceStore); if (store != null) - holder = (AnnotationHolder) store.get(binding); + holder = store.get(binding); if (holder == null) return; // nothing to delete } else { - SimpleLookupTable store = storedAnnotations(true, forceStore); + Map store = storedAnnotations(true, forceStore); if (store == null) return; // not supported - holder = (AnnotationHolder) store.get(binding); + holder = store.get(binding); if (holder == null) holder = new AnnotationHolder(); } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java index 33286162ac6..9047e85ffdc 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java @@ -55,8 +55,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ast.LambdaExpression; @@ -65,7 +67,6 @@ import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.impl.ReferenceContext; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; /* Not all fields defined by this type (& its subclasses) are initialized when it is created. @@ -88,7 +89,7 @@ abstract public class ReferenceBinding extends TypeBinding { char[] constantPoolName; char[] signature; - private SimpleLookupTable compatibleCache; + private Map compatibleCache; int typeBits; // additional bits characterizing this type protected MethodBinding [] singleAbstractMethod; @@ -1206,6 +1207,17 @@ public TypeVariableBinding getTypeVariable(char[] variableName) { @Override public int hashCode() { + if (!StackWalker. + getInstance(). + walk(stream -> stream.anyMatch(s -> "java.util.HashMap".equals(s.getClassName()) //$NON-NLS-1$ + || "java.util.concurrent.ConcurrentHashMap".equals(s.getClassName())))) //$NON-NLS-1$ + + { + RuntimeException e=new RuntimeException("called outside HashMap"); //$NON-NLS-1$ + e.printStackTrace(); + throw e; + } + // ensure ReferenceBindings hash to the same position as UnresolvedReferenceBindings so they can be replaced without rehashing // ALL ReferenceBindings are unique when created so equals() is the same as == return (this.compoundName == null || this.compoundName.length == 0) @@ -1450,7 +1462,7 @@ public boolean isCompatibleWith(TypeBinding otherType, /*@Nullable*/ Scope captu return true; Object result; if (this.compatibleCache == null) { - this.compatibleCache = new SimpleLookupTable(3); + this.compatibleCache = new HashMap<>(); result = null; } else { result = this.compatibleCache.get(otherType); // [dbg reset] this.compatibleCache.put(otherType,null) @@ -1901,8 +1913,8 @@ protected void appendNullAnnotation(StringBuilder nameBuffer, CompilerOptions op } public AnnotationHolder retrieveAnnotationHolder(Binding binding, boolean forceInitialization) { - SimpleLookupTable store = storedAnnotations(forceInitialization, false); - return store == null ? null : (AnnotationHolder) store.get(binding); + Map store = storedAnnotations(forceInitialization, false); + return store == null ? null : store.get(binding); } AnnotationBinding[] retrieveAnnotations(Binding binding) { @@ -2067,11 +2079,11 @@ public TypeBinding downwardsProjection(Scope scope, TypeBinding[] mentionedTypeV void storeAnnotationHolder(Binding binding, AnnotationHolder holder) { if (holder == null) { - SimpleLookupTable store = storedAnnotations(false, false); + Map store = storedAnnotations(false, false); if (store != null) - store.removeKey(binding); + store.remove(binding); } else { - SimpleLookupTable store = storedAnnotations(true, false); + Map store = storedAnnotations(true, false); if (store != null) store.put(binding, holder); } @@ -2080,21 +2092,21 @@ void storeAnnotationHolder(Binding binding, AnnotationHolder holder) { void storeAnnotations(Binding binding, AnnotationBinding[] annotations, boolean forceStore) { AnnotationHolder holder = null; if (annotations == null || annotations.length == 0) { - SimpleLookupTable store = storedAnnotations(false, forceStore); + Map store = storedAnnotations(false, forceStore); if (store != null) - holder = (AnnotationHolder) store.get(binding); + holder = store.get(binding); if (holder == null) return; // nothing to delete } else { - SimpleLookupTable store = storedAnnotations(true, forceStore); + Map store = storedAnnotations(true, forceStore); if (store == null) return; // not supported - holder = (AnnotationHolder) store.get(binding); + holder = store.get(binding); if (holder == null) holder = new AnnotationHolder(); } storeAnnotationHolder(binding, holder.setAnnotations(annotations)); } -SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) { +Map storedAnnotations(boolean forceInitialize, boolean forceStore) { return null; // overrride if interested in storing annotations for the receiver, its fields and methods } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java index fe5bbebe33b..ee797b6141d 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java @@ -84,8 +84,6 @@ import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; import org.eclipse.jdt.internal.compiler.util.HashtableOfObject; import org.eclipse.jdt.internal.compiler.util.ObjectVector; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; -import org.eclipse.jdt.internal.compiler.util.SimpleSet; @SuppressWarnings({ "rawtypes", "unchecked" }) public abstract class Scope { @@ -958,11 +956,11 @@ protected boolean connectTypeVariables(TypeParameter[] typeParameters, boolean c // https://bugs.eclipse.org/bugs/show_bug.cgi?id=335751 if (compilerOptions().complianceLevel > ClassFileConstants.JDK1_6) { if (typeVariable.rank >= varSuperType.rank && varSuperType.declaringElement == typeVariable.declaringElement) { - SimpleSet set = new SimpleSet(typeParameters.length); + Set set = new HashSet<>(typeParameters.length); set.add(typeVariable); ReferenceBinding superBinding = varSuperType; while (superBinding instanceof TypeVariableBinding) { - if (set.includes(superBinding)) { + if (set.contains(superBinding)) { problemReporter().hierarchyCircularity(typeVariable, varSuperType, typeRef); typeVariable.tagBits |= TagBits.HierarchyHasProblems; break firstBound; // do not keep first bound @@ -4993,7 +4991,7 @@ && compilerOptions().isAnnotationBasedNullAnalysisEnabled int mostSpecificLength = mostSpecificExceptions.length; ReferenceBinding[] nextExceptions = getFilteredExceptions(next); int nextLength = nextExceptions.length; - SimpleSet temp = new SimpleSet(mostSpecificLength); + Set temp = new HashSet<>(mostSpecificLength); boolean changed = false; nextException : for (int t = 0; t < mostSpecificLength; t++) { ReferenceBinding exception = mostSpecificExceptions[t]; @@ -5012,8 +5010,7 @@ && compilerOptions().isAnnotationBasedNullAnalysisEnabled } } if (changed) { - mostSpecificExceptions = temp.elementSize == 0 ? Binding.NO_EXCEPTIONS : new ReferenceBinding[temp.elementSize]; - temp.asArray(mostSpecificExceptions); + mostSpecificExceptions = temp.toArray(ReferenceBinding[]::new); } } } @@ -5395,7 +5392,7 @@ public MethodBinding getStaticFactory (ParameterizedTypeBinding allocationType, MethodBinding targetMethod = isInterface ? new MethodBinding(method.original(), genericType) : method.original(); MethodBinding staticFactory = new SyntheticFactoryMethodBinding(targetMethod, environment, originalEnclosingType); staticFactory.typeVariables = new TypeVariableBinding[factoryArity]; - final SimpleLookupTable map = new SimpleLookupTable(factoryArity); + Map map = new HashMap<>(factoryArity); // Rename each type variable T of the type to T' or T'' or T''' based on the enclosing level to avoid a clash. String prime = ""; //$NON-NLS-1$ @@ -5430,7 +5427,7 @@ public boolean isRawSubstitution() { } @Override public TypeBinding substitute(TypeVariableBinding typeVariable) { - TypeBinding retVal = (TypeBinding) map.get(typeVariable.unannotated()); + TypeBinding retVal = map.get(typeVariable.unannotated()); return retVal == null ? typeVariable : typeVariable.hasTypeAnnotations() ? environment().createAnnotatedType(retVal, typeVariable.getTypeAnnotations()) : retVal; } }; @@ -5438,7 +5435,7 @@ public TypeBinding substitute(TypeVariableBinding typeVariable) { // initialize new variable bounds for (int j = 0; j < factoryArity; j++) { TypeVariableBinding originalVariable = j < classTypeVariablesArity ? classTypeVariables[j] : methodTypeVariables[j - classTypeVariablesArity]; - TypeVariableBinding substitutedVariable = (TypeVariableBinding) map.get(originalVariable.unannotated()); + TypeVariableBinding substitutedVariable = map.get(originalVariable.unannotated()); TypeBinding substitutedSuperclass = Scope.substitute(substitution, originalVariable.superclass); ReferenceBinding[] substitutedInterfaces = Scope.substitute(substitution, originalVariable.superInterfaces); diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceModuleBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceModuleBinding.java index b6186780d46..dfe32ec4e13 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceModuleBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceModuleBinding.java @@ -15,12 +15,12 @@ import java.util.Collection; import java.util.LinkedHashMap; +import java.util.Map; import java.util.function.IntFunction; import java.util.stream.Stream; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.ModuleDeclaration; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; public class SourceModuleBinding extends ModuleBinding { @@ -154,9 +154,9 @@ public AnnotationBinding[] getAnnotations() { } @Override - SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) { + Map storedAnnotations(boolean forceInitialize, boolean forceStore) { if (this.scope != null) { // scope null when no annotation cached, and module got processed fully (159631) - SimpleLookupTable annotationTable = super.storedAnnotations(forceInitialize, forceStore); + Map annotationTable = super.storedAnnotations(forceInitialize, forceStore); if (annotationTable != null) this.scope.referenceCompilationUnit().compilationResult.hasAnnotations = true; return annotationTable; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java index 881161fe30e..dbee8e55626 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java @@ -59,6 +59,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Comparator; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -77,7 +78,6 @@ import org.eclipse.jdt.internal.compiler.impl.Constant; import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; import org.eclipse.jdt.internal.compiler.util.Util; @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -106,7 +106,7 @@ public class SourceTypeBinding extends ReferenceBinding { Map[] synthetics; char[] genericReferenceTypeSignature; - private SimpleLookupTable storedAnnotations = null; // keys are this ReferenceBinding & its fields and methods, value is an AnnotationHolder + private Map storedAnnotations = null; // keys are this ReferenceBinding & its fields and methods, value is an AnnotationHolder public int defaultNullness; boolean memberTypesSorted = false; @@ -3150,7 +3150,7 @@ public final int sourceStart() { return this.scope.referenceContext.sourceStart; } @Override -SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) { +Map storedAnnotations(boolean forceInitialize, boolean forceStore) { if (!isPrototype()) return this.prototype.storedAnnotations(forceInitialize, forceStore); @@ -3159,7 +3159,7 @@ SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) final CompilerOptions globalOptions = this.scope.environment().globalOptions; if (!globalOptions.storeAnnotations && !forceStore) return null; // not supported during this compile - this.storedAnnotations = new SimpleLookupTable(3); + this.storedAnnotations = new HashMap<>(); } return this.storedAnnotations; } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeBindingVisitor.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeBindingVisitor.java index f0ff19ee373..a7574c9aa4f 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeBindingVisitor.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeBindingVisitor.java @@ -13,13 +13,14 @@ *******************************************************************************/ package org.eclipse.jdt.internal.compiler.lookup; +import java.util.HashMap; +import java.util.Map; import org.eclipse.jdt.internal.compiler.ast.Wildcard; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; public class TypeBindingVisitor { - private SimpleLookupTable visitedCache; + private Map visitedCache; public void reset() { this.visitedCache = null; @@ -72,9 +73,9 @@ public static void visit(TypeBindingVisitor visitor, TypeBinding type) { if (type == null) return; - SimpleLookupTable visitedCache = visitor.visitedCache; + Map visitedCache = visitor.visitedCache; if (visitedCache == null) { - visitor.visitedCache = new SimpleLookupTable(3); + visitor.visitedCache = new HashMap<>(3); visitedCache = visitor.visitedCache; } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeSystem.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeSystem.java index c7de83d7eaa..6c0c9e18766 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeSystem.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeSystem.java @@ -23,7 +23,6 @@ import java.util.function.Consumer; import java.util.function.Supplier; import org.eclipse.jdt.internal.compiler.ast.ASTNode; -import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; import org.eclipse.jdt.internal.compiler.util.Util; /* TypeSystem: An abstraction responsible for keeping track of types that undergo "derivation" of some sort and the derived types produced thus. @@ -219,12 +218,12 @@ void put (ReferenceBinding genericType, TypeBinding[] typeArguments, ReferenceBi private int typeid = TypeIds.T_LastWellKnownTypeId; private TypeBinding [][] types; protected HashedParameterizedTypes parameterizedTypes; // auxiliary fast lookup table for parameterized types. - private SimpleLookupTable annotationTypes; // cannot store in types, since AnnotationBinding is not a TypeBinding and we don't want types to operate at Binding level. + private HashMap annotationTypes; // cannot store in types, since AnnotationBinding is not a TypeBinding and we don't want types to operate at Binding level. LookupEnvironment environment; public TypeSystem(LookupEnvironment environment) { this.environment = environment; - this.annotationTypes = new SimpleLookupTable(16); + this.annotationTypes = new HashMap<>(); this.typeid = TypeIds.T_LastWellKnownTypeId; this.types = new TypeBinding[TypeIds.T_LastWellKnownTypeId * 2][]; this.parameterizedTypes = new HashedParameterizedTypes(); @@ -560,7 +559,7 @@ alternate code paths. Unless care is exercised, we will end up with duplicate ob We may return a resolved annotation when requested for unresolved one, but not vice versa. */ public final AnnotationBinding getAnnotationType(ReferenceBinding annotationType, boolean requiredResolved) { - AnnotationBinding annotation = (AnnotationBinding) this.annotationTypes.get(annotationType); + AnnotationBinding annotation = this.annotationTypes.get(annotationType); if (annotation == null) { if (requiredResolved) annotation = new AnnotationBinding(annotationType, Binding.NO_ELEMENT_VALUE_PAIRS); @@ -589,7 +588,7 @@ public void cleanUp(int typeId) { } public void reset() { - this.annotationTypes = new SimpleLookupTable(16); + this.annotationTypes = new HashMap<>(); this.typeid = TypeIds.T_LastWellKnownTypeId; this.types = new TypeBinding[TypeIds.T_LastWellKnownTypeId * 2][]; this.parameterizedTypes = new HashedParameterizedTypes(); @@ -613,14 +612,10 @@ public void updateCaches(UnresolvedReferenceBinding unresolvedType, ReferenceBin } } } - if (this.annotationTypes.get(unresolvedType) != null) { // update the key - Object[] keys = this.annotationTypes.keyTable; - for (int i = 0, l = keys.length; i < l; i++) { - if (keys[i] == unresolvedType) { - keys[i] = resolvedType; // hashCode is based on compoundName so this works. - break; - } - } + AnnotationBinding removed = this.annotationTypes.remove(unresolvedType); + if (removed != null) { + // update the key + this.annotationTypes.put(resolvedType, removed); } } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/ThrownExceptionFinder.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/ThrownExceptionFinder.java index 91c086bc398..14543f3445a 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/ThrownExceptionFinder.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/ThrownExceptionFinder.java @@ -13,6 +13,8 @@ *******************************************************************************/ package org.eclipse.jdt.internal.codeassist; +import java.util.HashSet; +import java.util.Set; import java.util.Stack; import org.eclipse.jdt.internal.compiler.ASTVisitor; import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; @@ -30,15 +32,13 @@ import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; -import org.eclipse.jdt.internal.compiler.util.SimpleSet; -@SuppressWarnings({"rawtypes", "unchecked"}) public class ThrownExceptionFinder extends ASTVisitor { - private SimpleSet thrownExceptions; - private Stack exceptionsStack; - private SimpleSet caughtExceptions; - private SimpleSet discouragedExceptions; + private Set thrownExceptions; + private Stack> exceptionsStack; + private Set caughtExceptions; + private Set discouragedExceptions; /** * Finds the thrown exceptions minus the ones that are already caught in previous catch blocks. @@ -47,10 +47,10 @@ public class ThrownExceptionFinder extends ASTVisitor { * by the method and whose super type has been caught already. */ public void processThrownExceptions(TryStatement tryStatement, BlockScope scope) { - this.thrownExceptions = new SimpleSet(); - this.exceptionsStack = new Stack(); - this.caughtExceptions = new SimpleSet(); - this.discouragedExceptions = new SimpleSet(); + this.thrownExceptions = new HashSet<>(); + this.exceptionsStack = new Stack<>(); + this.caughtExceptions = new HashSet<>(); + this.discouragedExceptions = new HashSet<>(); tryStatement.traverse(this, scope); removeCaughtExceptions(tryStatement, true /*remove unchecked exceptions this time*/); } @@ -101,9 +101,7 @@ private void endVisitMethodInvocation(MethodBinding methodBinding) { * {@link ThrownExceptionFinder#getDiscouragedExceptions()}. */ public ReferenceBinding[] getAlreadyCaughtExceptions() { - ReferenceBinding[] allCaughtExceptions = new ReferenceBinding[this.caughtExceptions.elementSize]; - this.caughtExceptions.asArray(allCaughtExceptions); - return allCaughtExceptions; + return this.caughtExceptions.toArray(ReferenceBinding[]::new); } /** @@ -113,9 +111,7 @@ public ReferenceBinding[] getAlreadyCaughtExceptions() { * @return Returns an array of thrown exceptions that are still not caught in any catch block. */ public ReferenceBinding[] getThrownUncaughtExceptions() { - ReferenceBinding[] result = new ReferenceBinding[this.thrownExceptions.elementSize]; - this.thrownExceptions.asArray(result); - return result; + return this.thrownExceptions.toArray(ReferenceBinding[]::new); } /** @@ -124,9 +120,7 @@ public ReferenceBinding[] getThrownUncaughtExceptions() { * @return all discouraged exceptions */ public ReferenceBinding[] getDiscouragedExceptions() { - ReferenceBinding[] allDiscouragedExceptions = new ReferenceBinding[this.discouragedExceptions.elementSize]; - this.discouragedExceptions.asArray(allDiscouragedExceptions); - return allDiscouragedExceptions; + return this.discouragedExceptions.toArray(ReferenceBinding[]::new); } @Override public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) { @@ -150,19 +144,16 @@ private boolean visitType(TypeDeclaration typeDeclaration) { @Override public boolean visit(TryStatement tryStatement, BlockScope scope) { this.exceptionsStack.push(this.thrownExceptions); - SimpleSet exceptionSet = new SimpleSet(); + Set exceptionSet = new HashSet<>(); this.thrownExceptions = exceptionSet; tryStatement.tryBlock.traverse(this, scope); removeCaughtExceptions(tryStatement, false); - this.thrownExceptions = (SimpleSet)this.exceptionsStack.pop(); + this.thrownExceptions = this.exceptionsStack.pop(); - Object[] values = exceptionSet.values; - for (Object value : values) { - if (value != null) { - this.thrownExceptions.add(value); - } + for (ReferenceBinding value : exceptionSet) { + this.thrownExceptions.add(value); } Block[] catchBlocks = tryStatement.catchBlocks; @@ -182,32 +173,32 @@ private void removeCaughtExceptions(TryStatement tryStatement, boolean recordUnc TypeBinding caughtException; for (TypeReference ref : unionTypeReference.typeReferences) { caughtException = ref.resolvedType; - if ((caughtException instanceof ReferenceBinding) && caughtException.isValidBinding()) { // might be null when its the completion node + if ((caughtException instanceof ReferenceBinding rb) && caughtException.isValidBinding()) { // might be null when its the completion node if (recordUncheckedCaughtExceptions) { // is in outermost try-catch. Remove all caught exceptions, unchecked or checked - removeCaughtException((ReferenceBinding)caughtException); - this.caughtExceptions.add(caughtException); + removeCaughtException(rb); + this.caughtExceptions.add(rb); } else { // is in some inner try-catch. Discourage already caught checked exceptions // from being proposed in an outer catch. if (!caughtException.isUncheckedException(true)) { - this.discouragedExceptions.add(caughtException); + this.discouragedExceptions.add(rb); } } } } } else { TypeBinding exception = catchArguments[i].type.resolvedType; - if ((exception instanceof ReferenceBinding) && exception.isValidBinding()) { + if ((exception instanceof ReferenceBinding rb) && exception.isValidBinding()) { if (recordUncheckedCaughtExceptions) { // is in outermost try-catch. Remove all caught exceptions, unchecked or checked - removeCaughtException((ReferenceBinding)exception); - this.caughtExceptions.add(exception); + removeCaughtException(rb); + this.caughtExceptions.add(rb); } else { // is in some inner try-catch. Discourage already caught checked exceptions // from being proposed in an outer catch if (!exception.isUncheckedException(true)) { - this.discouragedExceptions.add(exception); + this.discouragedExceptions.add(rb); } } } @@ -216,9 +207,7 @@ private void removeCaughtExceptions(TryStatement tryStatement, boolean recordUnc } private void removeCaughtException(ReferenceBinding caughtException) { - Object[] exceptions = this.thrownExceptions.values; - for (Object e : exceptions) { - ReferenceBinding exception = (ReferenceBinding)e; + for (ReferenceBinding exception : this.thrownExceptions) { if (exception != null) { if (TypeBinding.equalsEquals(exception, caughtException)) { this.thrownExceptions.remove(exception); diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/State.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/State.java index acf760cac5d..bcb48dcafa1 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/State.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/State.java @@ -23,16 +23,8 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; import org.eclipse.core.resources.IContainer; @@ -594,14 +586,14 @@ void write(DataOutputStream output) throws IOException { * String[] Interned type locators */ out.writeInt(length = this.references.size()); - SimpleLookupTable internedTypeLocators = new SimpleLookupTable(length); + Map internedTypeLocators = new HashMap<>(length); if (length > 0) { Set keys = this.references.keySet(); for (String key : keys) { if (key != null) { length--; out.writeStringUsingLast(key); - internedTypeLocators.put(key, Integer.valueOf(internedTypeLocators.elementSize)); + internedTypeLocators.put(key, Integer.valueOf(internedTypeLocators.size())); } } if (JavaBuilder.DEBUG && length != 0) { @@ -623,8 +615,8 @@ void write(DataOutputStream output) throws IOException { if (key != null) { length--; out.writeStringUsingLast(key); - Integer index = (Integer) internedTypeLocators.get(value); - out.writeIntInRange(index.intValue(), internedTypeLocators.elementSize); + Integer index = internedTypeLocators.get(value); + out.writeIntInRange(index.intValue(), internedTypeLocators.size()); } } if (JavaBuilder.DEBUG && length != 0) { @@ -714,7 +706,7 @@ void write(DataOutputStream output) throws IOException { for (Entry entry : this.references.entrySet()) { String key = entry.getKey(); length--; - Integer index = (Integer) internedTypeLocators.get(key); + Integer index = internedTypeLocators.get(key); out.writeInt(index.intValue()); ReferenceCollection collection = entry.getValue(); if (collection instanceof AdditionalTypeCollection) {