Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary Map.containsKey calls. #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,7 @@ private static ContextTokenImpl nextToken() {

private static ContextFormatter getFormatter(ContextStackEntry entry) {
Integer key = entry.phaseId;
if (formatterMap.containsKey(key)) {
return formatterMap.get(key);
} else {
return defaultFormatter;
}
return formatterMap.getOrDefault(key, defaultFormatter);
}

private static class ContextTokenImpl implements ContextToken {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ private FieldBinding getAccessibleField(FieldBinding binding, TypeBinding receiv
alreadyResolvedMembers.put(binding, m);
}

if (inAspect.accessForInline.containsKey(m)) {
return (FieldBinding) inAspect.accessForInline.get(m);
FieldBinding ret = (FieldBinding) inAspect.accessForInline.get(m);
if (ret != null) {
return ret;
}
FieldBinding ret = new InlineAccessFieldBinding(inAspect, binding, m);
ret = new InlineAccessFieldBinding(inAspect, binding, m);
inAspect.accessForInline.put(m, ret);
return ret;
}
Expand Down Expand Up @@ -216,9 +217,11 @@ private MethodBinding getAccessibleMethod(MethodBinding binding, TypeBinding rec
// runtime this will be satisfied by the super).
m = world.makeResolvedMember(binding, receiverType);
}
if (inAspect.accessForInline.containsKey(m))
return (MethodBinding) inAspect.accessForInline.get(m);
MethodBinding ret = world.makeMethodBinding(AjcMemberMaker.inlineAccessMethodForMethod(inAspect.typeX, m));
MethodBinding ret = (MethodBinding) inAspect.accessForInline.get(m);
if (ret != null) {
return ret;
}
ret = world.makeMethodBinding(AjcMemberMaker.inlineAccessMethodForMethod(inAspect.typeX, m));
inAspect.accessForInline.put(m, ret);
return ret;
}
Expand All @@ -236,8 +239,9 @@ public SuperAccessMethodPair(ResolvedMember originalMethod, MethodBinding access
private MethodBinding getSuperAccessMethod(MethodBinding binding) {
ResolvedMember m = world.makeResolvedMember(binding);
ResolvedMember superAccessMember = AjcMemberMaker.superAccessMethod(inAspect.typeX, m);
if (inAspect.superAccessForInline.containsKey(superAccessMember)) {
return inAspect.superAccessForInline.get(superAccessMember).accessMethod;
SuperAccessMethodPair pair = inAspect.superAccessForInline.get(superAccessMember);
if (pair != null) {
return pair.accessMethod;
}
MethodBinding ret = world.makeMethodBinding(superAccessMember);
inAspect.superAccessForInline.put(superAccessMember, new SuperAccessMethodPair(m, ret));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,10 @@ private boolean doDeclareParents(DeclareParents declareParents, SourceTypeBindin
List<ResolvedType> newParents = declareParents.findMatchingNewParents(resolvedSourceType, false);
if (!newParents.isEmpty()) {
for (ResolvedType parent : newParents) {
if (dangerousInterfaces.containsKey(parent)) {
String dangerous = dangerousInterfaces.get(parent);
if (dangerous != null) {
ResolvedType onType = factory.fromEclipse(sourceType);
factory.showMessage(IMessage.ERROR, onType + ": " + dangerousInterfaces.get(parent),
factory.showMessage(IMessage.ERROR, onType + ": " + dangerous,
onType.getSourceLocation(), null);
}
if (Modifier.isFinal(parent.getModifiers())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,9 @@ private boolean hasAnyArguments(ParameterizedTypeBinding ptb) {
*/
private UnresolvedType fromTypeVariableBinding(TypeVariableBinding aTypeVariableBinding) {
// first, check for recursive call to this method for the same tvBinding
if (typeVariableBindingsInProgress.containsKey(aTypeVariableBinding)) {
return typeVariableBindingsInProgress.get(aTypeVariableBinding);
UnresolvedType alreadyInProgress = typeVariableBindingsInProgress.get(aTypeVariableBinding);
if (alreadyInProgress != null) {
return alreadyInProgress;
}

// Check if its a type variable binding that we need to recover to an alias...
Expand All @@ -382,8 +383,9 @@ private UnresolvedType fromTypeVariableBinding(TypeVariableBinding aTypeVariable
}
}

if (typeVariablesForThisMember.containsKey(new String(aTypeVariableBinding.sourceName))) {
return typeVariablesForThisMember.get(new String(aTypeVariableBinding.sourceName));
UnresolvedType typeVarForThis = typeVariablesForThisMember.get(new String(aTypeVariableBinding.sourceName));
if (typeVarForThis != null) {
return typeVarForThis;
}

// Create the UnresolvedTypeVariableReferenceType for the type variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public FieldBinding getPrivilegedAccessField(FieldBinding baseField, ASTNode loc
baseField = ((ParameterizedFieldBinding) baseField).originalField;
}
ResolvedMember key = inAspect.factory.makeResolvedMember(baseField);
if (accessors.containsKey(key))
return (FieldBinding) accessors.get(key);
FieldBinding fieldBinding = (FieldBinding) accessors.get(key);
if (fieldBinding != null) {
return fieldBinding;
}
FieldBinding ret = new PrivilegedFieldBinding(inAspect, baseField);
checkWeaveAccess(key.getDeclaringType(), location);
if (!baseField.alwaysNeedsAccessMethod(true))
Expand Down Expand Up @@ -89,8 +91,10 @@ public MethodBinding getPrivilegedAccessMethod(MethodBinding baseMethod, ASTNode
} else {
key = inAspect.factory.makeResolvedMember(baseMethod);
}
if (accessors.containsKey(key))
return (MethodBinding) accessors.get(key);
MethodBinding methodBinding = (MethodBinding) accessors.get(key);
if (methodBinding != null) {
return methodBinding;
}

MethodBinding ret;
if (baseMethod.isConstructor()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ private ByteArrayOutputStream getOutxmlContents(List<String> aspectNames) {

/**
* Returns a map where the keys are File objects corresponding to all the output directories and the values are a list of
* aspects which are sent to that ouptut directory
* aspects which are sent to that output directory
*/
private Map<File, List<String>> findOutputDirsForAspects() {
Map<File, List<String>> outputDirsToAspects = new HashMap<>();
Expand Down Expand Up @@ -729,10 +729,8 @@ private Map<File, List<String>> findOutputDirsForAspects() {
char[] fileName = entry.getValue();
File outputDir = buildConfig.getCompilationResultDestinationManager().getOutputLocationForClass(
new File(new String(fileName)));
if (!outputDirsToAspects.containsKey(outputDir)) {
outputDirsToAspects.put(outputDir, new ArrayList<>());
}
outputDirsToAspects.get(outputDir).add(aspectName);
outputDirsToAspects.computeIfAbsent(outputDir, f -> new ArrayList<>())
.add(aspectName);
}
}
}
Expand Down Expand Up @@ -1243,9 +1241,7 @@ private void addAspectName(String name, char[] fileContainingAspect) {
if (state.getAspectNamesToFileNameMap() == null) {
state.initializeAspectNamesToFileNameMap();
}
if (!state.getAspectNamesToFileNameMap().containsKey(name)) {
state.getAspectNamesToFileNameMap().put(name, fileContainingAspect);
}
state.getAspectNamesToFileNameMap().putIfAbsent(name, fileContainingAspect);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ private NameEnvironmentAnswer findType(String name) {
if (seenOnPreviousBuild != null) {
return new NameEnvironmentAnswer(seenOnPreviousBuild, null);
}
if (this.inflatedClassFilesCache.containsKey(name)) {
return this.inflatedClassFilesCache.get(name);
NameEnvironmentAnswer cached = this.inflatedClassFilesCache.get(name);
if (cached != null) {
return cached;
} else {
File fileOnDisk = classesFromName.get(name);
// System.err.println("find: " + name + " found: " + cf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ private boolean addOrReplaceDescendantsOf(ResolvedType aspectType, boolean inWea
}

public void addAdviceLikeDeclares(ResolvedType aspectType) {
if (!members.containsKey(aspectType)) {
CrosscuttingMembers xcut = members.get(aspectType);
if (xcut == null) {
return;
}
CrosscuttingMembers xcut = members.get(aspectType);
xcut.addDeclares(aspectType.collectDeclares(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,11 +881,8 @@ protected UnresolvedType parameterize(UnresolvedType aType, Map<String, Unresolv
boolean inParameterizedType, World w) {
if (aType instanceof TypeVariableReference) {
String variableName = ((TypeVariableReference) aType).getTypeVariable().getName();
if (!typeVariableMap.containsKey(variableName)) {
return aType; // if the type variable comes from the method (and
// not the type) thats OK
}
return typeVariableMap.get(variableName);
// if the type variable comes from the method (and not the type) that's OK
return typeVariableMap.getOrDefault(variableName, aType);
} else if (aType.isParameterizedType()) {
if (inParameterizedType) {
if (w != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,9 @@ public List<ShadowMunger> getDeclaredAdvice() {
for (int j = 0; j < ptypes.length; j++) {
if (ptypes[j] instanceof TypeVariableReferenceType) {
TypeVariableReferenceType tvrt = (TypeVariableReferenceType) ptypes[j];
if (typeVariableMap.containsKey(tvrt.getTypeVariable().getName())) {
newPTypes[j] = typeVariableMap.get(tvrt.getTypeVariable().getName());
UnresolvedType newPType = typeVariableMap.get(tvrt.getTypeVariable().getName());
if (newPType != null) {
newPTypes[j] = newPType;
} else {
newPTypes[j] = ptypes[j];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1427,8 +1427,9 @@ public AspectPrecedenceCalculator(World forSomeWorld) {
*/
public int compareByPrecedence(ResolvedType firstAspect, ResolvedType secondAspect) {
PrecedenceCacheKey key = new PrecedenceCacheKey(firstAspect, secondAspect);
if (cachedResults.containsKey(key)) {
return cachedResults.get(key);
Integer cachedOrder = cachedResults.get(key);
if (cachedOrder != null) {
return cachedOrder;
} else {
int order = 0;
DeclarePrecedence orderer = null; // Records the declare
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap,
if (type.isTypeVariableReference()) {
TypeVariableReference t = (TypeVariableReference) type;
String key = t.getTypeVariable().getName();
if (typeVariableMap.containsKey(key)) {
newType = typeVariableMap.get(key);
}
newType = typeVariableMap.getOrDefault(key, type);
} else if (type.isParameterizedType()) {
newType = w.resolve(type).parameterize(typeVariableMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,9 @@ public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap,
if (newNamePatterns.length == 1) {
String simpleName = newNamePatterns[0].maybeGetSimpleName();
if (simpleName != null) {
if (typeVariableMap.containsKey(simpleName)) {
String newName = ((ReferenceType) typeVariableMap.get(simpleName)).getName().replace('$', '.');
UnresolvedType unresolvedType = typeVariableMap.get(simpleName);
if (unresolvedType != null) {
String newName = ((ReferenceType) unresolvedType).getName().replace('$', '.');
StringTokenizer strTok = new StringTokenizer(newName, ".");
newNamePatterns = new NamePattern[strTok.countTokens()];
int index = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ private DocumentParser() {
}

public static Definition parse(final URL url) throws Exception {
if (CACHE && parsedFiles.containsKey(url.toString())) {
return parsedFiles.get(url.toString());
if (CACHE) {
Definition cached = parsedFiles.get(url.toString());
if (cached != null) {
return cached;
}
}
Definition def = null;

Expand Down