Skip to content

Commit

Permalink
When is ReferenceBinding.hashCode() used outside HashMap?
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T committed Dec 9, 2024
1 parent 11bf464 commit 99d961b
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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/*<PMB,IC18>*/ inferenceContexts;
private Map<ParameterizedGenericMethodBinding, InferenceContext18> inferenceContexts;
public HashMap<TypeBinding, MethodBinding> solutionsPerTargetType;
private InferenceContext18 outerInferenceContext; // resolving within the context of an outer (lambda) inference?
public boolean argsContainCast;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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/*<PGMB,InferenceContext18>*/ inferenceContexts;
private Map<ParameterizedGenericMethodBinding, InferenceContext18> inferenceContexts;
private HashMap<TypeBinding, MethodBinding> solutionsPerTargetType;
private InferenceContext18 outerInferenceContext; // resolving within the context of an outer (lambda) inference?

Expand Down Expand Up @@ -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);
}

Expand All @@ -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$
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@

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;
import org.eclipse.jdt.internal.compiler.ast.StatementWithFinallyBlock;
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 @@ -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<ReferenceBinding, Integer> indexes = new HashMap<>();
boolean isMethodContext;

public UnconditionalFlowInfo initsOnReturn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/*
Expand Down Expand Up @@ -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<Binding, AnnotationHolder> storedAnnotations = null; // keys are this ReferenceBinding & its fields and methods, value is an AnnotationHolder

private ReferenceBinding containerAnnotationType;
int defaultNullness = 0;
Expand Down Expand Up @@ -1976,15 +1977,15 @@ public void tagAsHavingDefectiveContainerType() {
}

@Override
SimpleLookupTable storedAnnotations(boolean forceInitialize, boolean forceStore) {
Map<Binding, AnnotationHolder> storedAnnotations(boolean forceInitialize, boolean forceStore) {

if (!isPrototype())
return this.prototype.storedAnnotations(forceInitialize, 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ public TypeBinding clone(TypeBinding outerType) {

@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;
}
return this.enclosingType.hashCode();
}
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeBinding, AccessRestriction> accessRestrictions;
ImportBinding[] defaultImports; // ROOT_ONLY
/**
* The root environment driving the current compilation.
Expand Down Expand Up @@ -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<MethodBinding, ParameterizedGenericMethodBinding[]> 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<String, PolymorphicMethodBinding[]> uniquePolymorphicMethodBindings;
private Map<TypeBinding, ParameterizedMethodBinding> 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
Expand All @@ -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<MissingTypeBinding> missingTypes;
Set<SourceTypeBinding> typesBeingConnected; // SHARED
public boolean isProcessingAnnotations = false; // ROOT_ONLY
public boolean mayTolerateMissingType = false;
Expand Down Expand Up @@ -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<>();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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<TypeBinding> args = new ArrayList<>();
int rank = 0;
do {
args.add(getTypeFromVariantTypeSignature(wrapper, staticVariables, enclosingType, genericType, rank, missingTypeNames,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 99d961b

Please sign in to comment.