performance, bit operations

This commit is contained in:
Alexey Kudravtsev
2012-01-24 12:10:39 +04:00
parent 9adadf6569
commit 8f4258d45d
3 changed files with 97 additions and 60 deletions
@@ -35,6 +35,7 @@ import com.intellij.openapi.roots.JdkOrderEntry;
import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
@@ -50,6 +51,7 @@ import com.intellij.psi.util.*;
import com.intellij.slicer.*;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import com.intellij.util.containers.ConcurrentSoftValueHashMap;
import gnu.trove.THashSet;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.Nls;
@@ -284,6 +286,20 @@ public class MagicConstantInspection extends LocalInspectionTool {
result = 31 * result + (canBeOred ? 1 : 0);
return result;
}
public boolean isSubsetOf(@NotNull AllowedValues other, @NotNull PsiManager manager) {
for (PsiAnnotationMemberValue value : values) {
boolean found = false;
for (PsiAnnotationMemberValue otherValue : other.values) {
if (same(value, otherValue, manager)) {
found = true;
break;
}
}
if (!found) return false;
}
return true;
}
}
private static AllowedValues getAllowedValuesFromMagic(@NotNull PsiModifierListOwner element,
@@ -480,7 +496,7 @@ public class MagicConstantInspection extends LocalInspectionTool {
return value.getText();
}
}, ", ");
holder.registerProblem(argument, "Must be one of the: "+ values);
holder.registerProblem(argument, "Must be one of: "+ values);
}
private static boolean isAllowed(@NotNull final PsiElement scope,
@@ -499,9 +515,9 @@ public class MagicConstantInspection extends LocalInspectionTool {
}
private static boolean isGoodExpression(PsiExpression expression,
AllowedValues allowedValues,
PsiElement scope,
PsiManager manager) {
@NotNull AllowedValues allowedValues,
@NotNull PsiElement scope,
@NotNull PsiManager manager) {
expression = PsiUtil.deparenthesizeExpression(expression);
if (expression == null) return true;
if (expression instanceof PsiConditionalExpression) {
@@ -515,18 +531,24 @@ public class MagicConstantInspection extends LocalInspectionTool {
if (isOneOf(expression, allowedValues, manager)) return true;
if (allowedValues.canBeOred) {
if (expression instanceof PsiPolyadicExpression &&
JavaTokenType.OR.equals(((PsiPolyadicExpression)expression).getOperationTokenType())) {
for (PsiExpression operand : ((PsiPolyadicExpression)expression).getOperands()) {
if (!isAllowed(scope, operand, allowedValues, manager)) return false;
}
return true;
}
//todo & ~(CONST | CONST)
PsiExpression zero = JavaPsiFacade.getElementFactory(manager.getProject()).createExpressionFromText("0", expression);
PsiExpression zero = getLiteralExpression(expression, manager, "0");
if (same(expression, zero, manager)) return true;
PsiExpression mOne = JavaPsiFacade.getElementFactory(manager.getProject()).createExpressionFromText("-1", expression);
PsiExpression mOne = getLiteralExpression(expression, manager, "-1");
if (same(expression, mOne, manager)) return true;
if (expression instanceof PsiPolyadicExpression) {
IElementType tokenType = ((PsiPolyadicExpression)expression).getOperationTokenType();
if (JavaTokenType.OR.equals(tokenType) || JavaTokenType.AND.equals(tokenType)) {
for (PsiExpression operand : ((PsiPolyadicExpression)expression).getOperands()) {
if (!isAllowed(scope, operand, allowedValues, manager)) return false;
}
return true;
}
}
if (expression instanceof PsiPrefixExpression &&
JavaTokenType.TILDE.equals(((PsiPrefixExpression)expression).getOperationTokenType())) {
PsiExpression operand = ((PsiPrefixExpression)expression).getOperand();
return operand == null || isAllowed(scope, operand, allowedValues, manager);
}
}
PsiElement resolved = null;
@@ -540,11 +562,26 @@ public class MagicConstantInspection extends LocalInspectionTool {
AllowedValues allowedForRef;
if (resolved instanceof PsiModifierListOwner &&
(allowedForRef = getAllowedValues((PsiModifierListOwner)resolved, getType((PsiModifierListOwner)resolved), null)) != null &&
Comparing.equal(allowedValues, allowedForRef)) return true;
allowedForRef.isSubsetOf(allowedValues, manager)) return true;
return PsiType.NULL.equals(expression.getType());
}
private static Key<Map<String, PsiExpression>> LITERAL_EXPRESSION_CACHE = Key.create("LITERAL_EXPRESSION_CACHE");
private static PsiExpression getLiteralExpression(PsiExpression context, PsiManager manager, @NotNull String text) {
Map<String, PsiExpression> cache = LITERAL_EXPRESSION_CACHE.get(manager);
if (cache == null) {
cache = new ConcurrentSoftValueHashMap<String, PsiExpression>();
cache = manager.putUserDataIfAbsent(LITERAL_EXPRESSION_CACHE, cache);
}
PsiExpression expression = cache.get(text);
if (expression == null) {
expression = JavaPsiFacade.getElementFactory(manager.getProject()).createExpressionFromText(text, context);
cache.put(text, expression);
}
return expression;
}
private static boolean isOneOf(@NotNull PsiExpression expression, @NotNull AllowedValues allowedValues, @NotNull PsiManager manager) {
for (PsiAnnotationMemberValue allowedValue : allowedValues.values) {
if (same(allowedValue, expression, manager)) return true;
@@ -14,7 +14,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -189,7 +189,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -201,7 +201,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -225,7 +225,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -237,7 +237,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -249,7 +249,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>119</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -276,7 +276,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -288,7 +288,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -300,7 +300,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -312,7 +312,7 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: 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>Must be one of the: Const.X, Const.Y, Const.Z</description>
<description>Must be one of: Const.X, Const.Y, Const.Z</description>
</problem>
@@ -383,7 +383,7 @@
<file>X.java</file>
<line>193</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
<description>Must be one of: Const.X, Const.Y</description>
</problem>
@@ -391,14 +391,14 @@
<file>X.java</file>
<line>195</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
<description>Must be one of: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>227</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
<description>Must be one of: Const.X, Const.Y</description>
</problem>
@@ -406,7 +406,7 @@
<file>X.java</file>
<line>228</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
<description>Must be one of: Const.X, Const.Y</description>
</problem>
@@ -414,7 +414,7 @@
<file>X.java</file>
<line>229</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
<description>Must be one of: Const.X, Const.Y</description>
</problem>
@@ -422,28 +422,28 @@
<file>X.java</file>
<line>230</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
<description>Must be one of: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>231</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
<description>Must be one of: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>238</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Magic Constant</problem_class>
<description>Must be one of the: Calendar.JANUARY, Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL, Calendar.MAY, Calendar.JUNE, Calendar.JULY, Calendar.AUGUST, Calendar.SEPTEMBER, Calendar.OCTOBER, Calendar.NOVEMBER, Calendar.DECEMBER</description>
<description>Must be one of: Calendar.JANUARY, Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL, Calendar.MAY, Calendar.JUNE, Calendar.JULY, Calendar.AUGUST, Calendar.SEPTEMBER, Calendar.OCTOBER, Calendar.NOVEMBER, Calendar.DECEMBER</description>
</problem>
<problem>
<file>X.java</file>
<line>239</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Magic Constant</problem_class>
<description>Must be one of the: SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.LEADING, SwingConstants.TRAILING</description>
<description>Must be one of: SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.LEADING, SwingConstants.TRAILING</description>
</problem>
</problems>
@@ -103,10 +103,10 @@ public class X {
if (x == Const.X) {
x = Const.Y;
assert x != Const.Z;
f |= Const.Y;
f |= Const.Y; f &= Const.X & ~(Const.Z | Const.X);
}
else {
f |= Const.X;
f |= Const.X; f = f & ~(Const.X | Const.X);
}
f3(f);