Improve MagicConstantInspection in case of two constants sources

This commit is contained in:
Vladislav Rassokhin
2017-02-14 18:38:48 +03:00
parent 8badcc8f56
commit 07b27a76a9
3 changed files with 77 additions and 64 deletions
@@ -45,6 +45,7 @@ import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.*;
import com.intellij.slicer.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.FileBasedIndex;
@@ -325,42 +326,51 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool {
private static AllowedValues getAllowedValuesFromMagic(@NotNull PsiType type,
@NotNull PsiAnnotation magic,
@NotNull PsiManager manager) {
PsiAnnotationMemberValue[] allowedValues;
final boolean canBeOred;
PsiAnnotationMemberValue[] allowedValues = PsiAnnotationMemberValue.EMPTY_ARRAY;
boolean values = false, flags = false;
if (TypeConversionUtil.getTypeRank(type) <= TypeConversionUtil.LONG_RANK) {
PsiAnnotationMemberValue intValues = magic.findAttributeValue("intValues");
allowedValues = intValues instanceof PsiArrayInitializerMemberValue ? ((PsiArrayInitializerMemberValue)intValues).getInitializers() : PsiAnnotationMemberValue.EMPTY_ARRAY;
if (allowedValues.length == 0) {
PsiAnnotationMemberValue orValue = magic.findAttributeValue("flags");
allowedValues = orValue instanceof PsiArrayInitializerMemberValue ? ((PsiArrayInitializerMemberValue)orValue).getInitializers() : PsiAnnotationMemberValue.EMPTY_ARRAY;
canBeOred = true;
if (intValues instanceof PsiArrayInitializerMemberValue) {
allowedValues = ((PsiArrayInitializerMemberValue)intValues).getInitializers();
values = true;
}
else {
canBeOred = false;
PsiAnnotationMemberValue orValue = magic.findAttributeValue("flags");
if (orValue instanceof PsiArrayInitializerMemberValue) {
allowedValues = ((PsiArrayInitializerMemberValue)orValue).getInitializers();
flags = true;
}
}
}
else if (type.equals(PsiType.getJavaLangString(manager, GlobalSearchScope.allScope(manager.getProject())))) {
PsiAnnotationMemberValue strValuesAttr = magic.findAttributeValue("stringValues");
allowedValues = strValuesAttr instanceof PsiArrayInitializerMemberValue ? ((PsiArrayInitializerMemberValue)strValuesAttr).getInitializers() : PsiAnnotationMemberValue.EMPTY_ARRAY;
canBeOred = false;
if (strValuesAttr instanceof PsiArrayInitializerMemberValue) {
allowedValues = ((PsiArrayInitializerMemberValue)strValuesAttr).getInitializers();
values = true;
}
}
else {
return null; //other types not supported
}
if (allowedValues.length != 0) {
return new AllowedValues(allowedValues, canBeOred);
// Also there're could be valuesFromClass of flagsFromClass
PsiAnnotationMemberValue[] valuesFromClass = readFromClass("valuesFromClass", magic, type, manager);
if (valuesFromClass != null) {
allowedValues = ArrayUtil.mergeArrays(allowedValues, valuesFromClass, PsiAnnotationMemberValue.ARRAY_FACTORY);
values = true;
}
// last resort: try valuesFromClass
PsiAnnotationMemberValue[] values = readFromClass("valuesFromClass", magic, type, manager);
boolean ored = false;
if (values == null) {
values = readFromClass("flagsFromClass", magic, type, manager);
ored = true;
PsiAnnotationMemberValue[] flagsFromClass = readFromClass("flagsFromClass", magic, type, manager);
if (flagsFromClass != null) {
allowedValues = ArrayUtil.mergeArrays(allowedValues, flagsFromClass, PsiAnnotationMemberValue.ARRAY_FACTORY);
flags = true;
}
if (values == null) return null;
return new AllowedValues(values, ored);
if (allowedValues.length == 0) {
return null;
}
if (values && flags) {
// Combination of 'flags' and 'values', that's weird TODO: Log?
}
return new AllowedValues(allowedValues, flags);
}
private static PsiAnnotationMemberValue[] readFromClass(@NonNls @NotNull String attributeName,
@@ -14,7 +14,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -26,7 +26,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -38,7 +38,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -50,7 +50,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -62,7 +62,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -74,7 +74,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -86,7 +86,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -98,7 +98,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -110,7 +110,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -122,7 +122,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -134,7 +134,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -146,7 +146,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -158,7 +158,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -170,7 +170,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -178,7 +178,7 @@
<file>X.java</file>
<line>81</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -189,7 +189,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -201,7 +201,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -213,7 +213,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -225,7 +225,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -237,7 +237,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -249,7 +249,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -257,14 +257,14 @@
<file>X.java</file>
<line>118</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
<problem>
<file>X.java</file>
<line>119</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -276,7 +276,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -288,7 +288,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -300,7 +300,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -312,7 +312,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z or their combination</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z or their combination</description>
</problem>
@@ -322,7 +322,7 @@
<file>X.java</file>
<line>173</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -331,7 +331,7 @@
<file>X.java</file>
<line>174</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -340,7 +340,7 @@
<file>X.java</file>
<line>175</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -349,7 +349,7 @@
<file>X.java</file>
<line>177</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -357,7 +357,7 @@
<file>X.java</file>
<line>178</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -366,7 +366,7 @@
<file>X.java</file>
<line>179</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -375,7 +375,7 @@
<file>X.java</file>
<line>180</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Should be one of: Const.X, Const.Y, Const.Z</description>
<description>Should be one of: Const2.I, Const.X, Const.Y, Const.Z</description>
</problem>
@@ -24,7 +24,7 @@ class Const {
}
public class X {
void f(@MagicConstant(intValues={Const.X, Const.Y, Const.Z}) int x) {
void f(@MagicConstant(intValues={Const.X, Const.Y, Const.Z, Const2.I}) int x) {
/////////// BAD
f(0);
f(1);
@@ -50,7 +50,7 @@ public class X {
f2(x);
}
void f2(@MagicConstant(valuesFromClass =Const.class) int x) {
void f2(@MagicConstant(valuesFromClass =Const.class, intValues={Const2.I}) int x) {
/////////// BAD
f2(0);
f2(1);
@@ -61,11 +61,11 @@ public class X {
x = 2;
assert x != 1;
}
////////////// GOOD
f2(Const.X);
f2(Const.Y);
f2(Const.Z);
f2(Const.Z);
f2(Const2.I);
int i2 = this == null ? Const.X : Const.Y;
f2(i2);
if (x == Const.X) {
@@ -76,7 +76,7 @@ public class X {
f(x);
}
void f3(@MagicConstant(flags ={Const.X, Const.Y, Const.Z}) int x) {
void f3(@MagicConstant(flags ={Const.X, Const.Y, Const.Z, Const2.I}) int x) {
/////////// BAD
f3(2);
f3(1);
@@ -113,7 +113,7 @@ public class X {
f4(x);
}
void f4(@MagicConstant(flagsFromClass =Const.class) int x) {
void f4(@MagicConstant(flagsFromClass =Const.class, flags={Const2.I}) int x) {
/////////// BAD
f4(-3);
f4(1);
@@ -128,8 +128,8 @@ public class X {
////////////// GOOD
f4(Const.X);
f4(Const.Y);
f4(Const.Z);
f4(Const.Z);
f4(Const2.I);
int i2 = this == null ? Const.X : Const.Y;
f4(i2);
int ix = Const.X | Const.Y;
@@ -152,7 +152,7 @@ public class X {
class Alias {
@MagicConstant(intValues={Const.X, Const.Y, Const.Z})
@MagicConstant(intValues={Const.X, Const.Y, Const.Z, Const2.I})
@interface IntEnum{}
void f(@IntEnum int x) {
@@ -268,3 +268,6 @@ public class X {
font(0);
}
}
class Const2 {
public static final int I = 0x10;
}