From faf7fc72894fd2f739a652a273c1c63d9f534698 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 4 Jun 2012 13:03:07 +0400 Subject: [PATCH] IDEA-86896 Magic constant doesn't understand bit flag constants --- .../MagicConstantInspection.java | 31 +++++++++---------- .../inspection/magic/simple/src/X.java | 12 +++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/magicConstant/MagicConstantInspection.java b/java/java-impl/src/com/intellij/codeInspection/magicConstant/MagicConstantInspection.java index 8fb41e8a9a47..0d5f66a5964a 100644 --- a/java/java-impl/src/com/intellij/codeInspection/magicConstant/MagicConstantInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/magicConstant/MagicConstantInspection.java @@ -196,10 +196,9 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { private static void attachJdkAnnotations(Sdk jdk) { LocalFileSystem lfs = LocalFileSystem.getInstance(); - VirtualFile root = null; - if (root == null) { // community idea under idea - root = lfs.findFileByPath(FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/java/jdkAnnotations"); - } + // community idea under idea + VirtualFile root = lfs.findFileByPath(FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/java/jdkAnnotations"); + if (root == null) { // idea under idea root = lfs.findFileByPath(FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/community/java/jdkAnnotations"); } @@ -252,7 +251,7 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { final PsiAnnotationMemberValue[] values; final boolean canBeOred; - private AllowedValues(PsiAnnotationMemberValue[] values, boolean canBeOred) { + private AllowedValues(@NotNull PsiAnnotationMemberValue[] values, boolean canBeOred) { this.values = values; this.canBeOred = canBeOred; } @@ -304,8 +303,8 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { } private static AllowedValues getAllowedValuesFromMagic(@NotNull PsiModifierListOwner element, - @NotNull PsiType type, PsiAnnotation magic) { - if (magic == null) return null; + @NotNull PsiType type, + @NotNull PsiAnnotation magic) { PsiAnnotationMemberValue[] allowedValues; final boolean canBeOred; if (TypeConversionUtil.getTypeRank(type) <= TypeConversionUtil.LONG_RANK) { @@ -345,7 +344,7 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { return new AllowedValues(values, ored); } - private static PsiAnnotationMemberValue[] readFromClass(@NonNls String attributeName, @NotNull PsiAnnotation magic, PsiType type) { + private static PsiAnnotationMemberValue[] readFromClass(@NonNls @NotNull String attributeName, @NotNull PsiAnnotation magic, @NotNull PsiType type) { PsiAnnotationMemberValue fromClassAttr = magic.findAttributeValue(attributeName); PsiType fromClassType = fromClassAttr instanceof PsiClassObjectAccessExpression ? ((PsiClassObjectAccessExpression)fromClassAttr).getOperand().getType() : null; PsiClass fromClass = fromClassType instanceof PsiClassType ? ((PsiClassType)fromClassType).resolve() : null; @@ -386,7 +385,6 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { if (values != null) return values; } - return parseBeanInfo(element); } @@ -467,12 +465,12 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { return new AllowedValues(array, false); } - private static PsiType getType(PsiModifierListOwner element) { + private static PsiType getType(@NotNull PsiModifierListOwner element) { return element instanceof PsiVariable ? ((PsiVariable)element).getType() : element instanceof PsiMethod ? ((PsiMethod)element).getReturnType() : null; } private static void checkMagicParameterArgument(@NotNull PsiParameter parameter, - PsiExpression argument, + @NotNull PsiExpression argument, @NotNull AllowedValues allowedValues, @NotNull ProblemsHolder holder) { final PsiManager manager = PsiManager.getInstance(holder.getProject()); @@ -482,7 +480,7 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { } } - private static void registerProblem(PsiExpression argument, AllowedValues allowedValues, ProblemsHolder holder) { + private static void registerProblem(@NotNull PsiExpression argument, @NotNull AllowedValues allowedValues, @NotNull ProblemsHolder holder) { String values = StringUtil.join(allowedValues.values, new Function() { @Override @@ -509,7 +507,6 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { return processValuesFlownTo(argument, scope, new Processor() { @Override public boolean process(PsiExpression expression) { - if (false & !PsiTreeUtil.isAncestor(scope, expression, false)) return true; return isGoodExpression(expression, allowedValues, scope, manager); } }); @@ -538,7 +535,7 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { 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)) { + if (JavaTokenType.OR.equals(tokenType) || JavaTokenType.AND.equals(tokenType) || JavaTokenType.PLUS.equals(tokenType)) { for (PsiExpression operand : ((PsiPolyadicExpression)expression).getOperands()) { if (!isAllowed(scope, operand, allowedValues, manager)) return false; } @@ -568,8 +565,8 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { return PsiType.NULL.equals(expression.getType()); } - private static Key> LITERAL_EXPRESSION_CACHE = Key.create("LITERAL_EXPRESSION_CACHE"); - private static PsiExpression getLiteralExpression(PsiExpression context, PsiManager manager, @NotNull String text) { + private static final Key> LITERAL_EXPRESSION_CACHE = Key.create("LITERAL_EXPRESSION_CACHE"); + private static PsiExpression getLiteralExpression(@NotNull PsiExpression context, @NotNull PsiManager manager, @NotNull String text) { Map cache = LITERAL_EXPRESSION_CACHE.get(manager); if (cache == null) { cache = new ConcurrentSoftValueHashMap(); @@ -605,7 +602,7 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool { } private static boolean processValuesFlownTo(@NotNull final PsiExpression argument, - PsiElement scope, + @NotNull PsiElement scope, @NotNull final Processor processor) { SliceAnalysisParams params = new SliceAnalysisParams(); params.dataFlowToThis = true; diff --git a/java/java-tests/testData/inspection/magic/simple/src/X.java b/java/java-tests/testData/inspection/magic/simple/src/X.java index 84c3ea63651f..9fcf81269ee7 100644 --- a/java/java-tests/testData/inspection/magic/simple/src/X.java +++ b/java/java-tests/testData/inspection/magic/simple/src/X.java @@ -244,4 +244,16 @@ public class X { super.f(x); } } + + void plusSupportedInFlags(@MagicConstant(flags ={Const.X, Const.Y, Const.Z}) int x) { + ////////////// GOOD + plusSupportedInFlags(Const.X + Const.Y); + plusSupportedInFlags(Const.Z + Const.X + Const.Y); + plusSupportedInFlags(Const.Z + (Const.X + Const.Y)); + + int ix = Const.X + Const.Y; + plusSupportedInFlags(ix); + plusSupportedInFlags(0); + plusSupportedInFlags(-1); + } }