IDEA-90475 ("Map replaceable with EnumMap" inspection suggests replacement when TreeMap constructor takes a Comparator)

This commit is contained in:
Bas Leijdekkers
2012-08-23 16:56:26 +02:00
parent 29be01b141
commit 3b1e52c933
3 changed files with 31 additions and 23 deletions
@@ -1040,8 +1040,8 @@ instantiating.object.to.get.class.object.problem.descriptor=Instantiating object
field.may.be.static.problem.descriptor=Field <code>#ref</code> may be 'static' #loc
method.may.be.static.problem.descriptor=Method <code>#ref()</code> may be 'static' #loc
class.initializer.may.be.static.problem.descriptor=Class initializer may be 'static' #loc
map.replaceable.by.enum.map.problem.descriptor=<code>#ref</code> replaceable with EnumMap #loc
set.replaceable.by.enum.set.problem.descriptor=<code>#ref</code> replaceable with EnumSet #loc
map.replaceable.by.enum.map.problem.descriptor=<code>#ref</code> replaceable with 'EnumMap' #loc
set.replaceable.by.enum.set.problem.descriptor=<code>#ref</code> replaceable with 'EnumSet' #loc
inner.class.may.be.static.problem.descriptor=Inner class <code>#ref</code> may be 'static' #loc
string.buffer.must.have.initial.capacity.problem.descriptor=<code>#ref</code> without initial capacity #loc
string.buffer.replaceable.by.string.builder.problem.descriptor=<code>StringBuffer #ref</code> may be declared as 'StringBuilder' #loc
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ExpectedTypeUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
@@ -28,15 +29,13 @@ public class MapReplaceableByEnumMapInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"map.replaceable.by.enum.map.display.name");
return InspectionGadgetsBundle.message("map.replaceable.by.enum.map.display.name");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"map.replaceable.by.enum.map.problem.descriptor");
return InspectionGadgetsBundle.message("map.replaceable.by.enum.map.problem.descriptor");
}
@Override
@@ -48,16 +47,19 @@ public class MapReplaceableByEnumMapInspection extends BaseInspection {
extends BaseInspectionVisitor {
@Override
public void visitNewExpression(
@NotNull PsiNewExpression expression) {
public void visitNewExpression(@NotNull PsiNewExpression expression) {
super.visitNewExpression(expression);
final PsiType type = expression.getType();
if (!(type instanceof PsiClassType)) {
return;
}
final PsiClassType classType = (PsiClassType)type;
PsiClassType classType = (PsiClassType)type;
if (!classType.hasParameters()) {
return;
final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false);
if (!(expectedType instanceof PsiClassType)) {
return;
}
classType = (PsiClassType)expectedType;
}
final PsiType[] typeArguments = classType.getParameters();
if (typeArguments.length != 2) {
@@ -67,12 +69,10 @@ public class MapReplaceableByEnumMapInspection extends BaseInspection {
if (!(argumentType instanceof PsiClassType)) {
return;
}
if (!TypeUtils.expressionHasTypeOrSubtype(expression,
CommonClassNames.JAVA_UTIL_MAP)) {
if (!TypeUtils.expressionHasTypeOrSubtype(expression, CommonClassNames.JAVA_UTIL_MAP)) {
return;
}
if (null != TypeUtils.expressionHasTypeOrSubtype(expression,
"java.util.EnumMap", "java.util.concurrent.ConcurrentMap")) {
if (null != TypeUtils.expressionHasTypeOrSubtype(expression, "java.util.EnumMap", "java.util.concurrent.ConcurrentMap")) {
return;
}
final PsiClassType argumentClassType = (PsiClassType)argumentType;
@@ -80,13 +80,17 @@ public class MapReplaceableByEnumMapInspection extends BaseInspection {
if (argumentClass == null || !argumentClass.isEnum()) {
return;
}
final PsiClass aClass =
PsiTreeUtil.getParentOfType(expression, PsiClass.class);
final PsiClass aClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class);
if (argumentClass.equals(aClass)) {
final PsiMember member =
PsiTreeUtil.getParentOfType(expression, PsiMember.class);
if (member != null &&
!member.hasModifierProperty(PsiModifier.STATIC)) {
final PsiMember member = PsiTreeUtil.getParentOfType(expression, PsiMember.class);
if (member != null && !member.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
}
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList != null) {
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length > 0 && TypeUtils.expressionHasTypeOrSubtype(arguments[0], "java.util.Comparator")) {
return;
}
}
@@ -1,7 +1,7 @@
package com.siyeh.igtest.performance.map_replaceable_by_enum_map;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections; import java.util.HashMap;
import java.util.Map; import java.util.TreeMap;
public class MapReplaceableByEnumMap {
@@ -14,4 +14,8 @@ public class MapReplaceableByEnumMap {
Map<MyEnum, Object> enums = new HashMap();
// enum map here throws exception at runtime -> don't suggest it
}
void foo() {
final Map<MyEnum, Object> map = new TreeMap(Collections.reverseOrder());
}
}