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 9d4840cf53fd..3baa842b23a8 100644 --- a/java/java-impl/src/com/intellij/codeInspection/magicConstant/MagicConstantInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/magicConstant/MagicConstantInspection.java @@ -26,6 +26,7 @@ import com.intellij.ide.util.treeView.AbstractTreeNode; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; @@ -56,6 +57,8 @@ import org.jetbrains.annotations.NotNull; import java.util.*; public class MagicConstantInspection extends LocalInspectionTool { + private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.magicConstant.MagicConstantInspection"); + @Nls @NotNull @Override @@ -185,7 +188,7 @@ public class MagicConstantInspection extends LocalInspectionTool { jdk = ((JdkOrderEntry)orderEntry).getJdk(); } } - if (jdk == null) return; + LOG.assertTrue(jdk != null, "JDK not configured for module "+module); LocalFileSystem lfs = LocalFileSystem.getInstance(); VirtualFile root = null; if (root == null) { // community idea under idea @@ -194,10 +197,11 @@ public class MagicConstantInspection extends LocalInspectionTool { if (root == null) { // idea under idea root = lfs.findFileByPath(FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/community/java/jdkAnnotations"); } - if (root == null) { + if (root == null) { // build root = VirtualFileManager.getInstance().findFileByUrl("jar://"+ FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/lib/jdkAnnotations.jar!/"); } if (root == null) { + LOG.error("jdk annotations not found in: "+ FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/lib/jdkAnnotations.jar!/"); return; } @@ -278,8 +282,8 @@ public class MagicConstantInspection extends LocalInspectionTool { } } - private static AllowedValues getAllowedValuesFromMagic(@NotNull PsiModifierListOwner element, @NotNull PsiType type) { - PsiAnnotation magic = AnnotationUtil.findAnnotationInHierarchy(element, Collections.singleton(MagicConstant.class.getName())); + private static AllowedValues getAllowedValuesFromMagic(@NotNull PsiModifierListOwner element, + @NotNull PsiType type, PsiAnnotation magic) { if (magic == null) return null; PsiAnnotationMemberValue[] allowedValues; final boolean canBeOred; @@ -342,14 +346,15 @@ public class MagicConstantInspection extends LocalInspectionTool { } static AllowedValues getAllowedValues(@NotNull PsiModifierListOwner element, PsiType type, Set visited) { - AllowedValues values; - if (type != null) { - values = getAllowedValuesFromMagic(element, type); - if (values != null) return values; - } - PsiAnnotation[] annotations = AnnotationUtil.getAllAnnotations(element, true, null); for (PsiAnnotation annotation : annotations) { + AllowedValues values; + if (type != null && MagicConstant.class.getName().equals(annotation.getQualifiedName())) { + //PsiAnnotation magic = AnnotationUtil.findAnnotationInHierarchy(element, Collections.singleton(MagicConstant.class.getName())); + values = getAllowedValuesFromMagic(element, type, annotation); + if (values != null) return values; + } + PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement(); PsiElement resolved = ref == null ? null : ref.resolve(); if (!(resolved instanceof PsiClass) || !((PsiClass)resolved).isAnnotationType()) continue; 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 5f9216771bc2..b7cee434f219 100644 --- a/java/java-tests/testData/inspection/magic/simple/src/X.java +++ b/java/java-tests/testData/inspection/magic/simple/src/X.java @@ -239,4 +239,9 @@ public class X { new javax.swing.JLabel("text", 3); } } + static class OverrideX extends X { + void f(int x) { + super.f(x); + } + } } diff --git a/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java b/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java index c5c008eaee88..7f5ff36dd58d 100644 --- a/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java +++ b/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java @@ -152,6 +152,44 @@ public class AnnotationUtil { if (listOwner instanceof PsiClass) { return findAnnotationInHierarchy((PsiClass)listOwner, annotationNames, null); } + if (listOwner instanceof PsiParameter) { + PsiParameter parameter = (PsiParameter)listOwner; + return doFindAnnotationInHierarchy(parameter, annotationNames, null); + } + return null; + } + + private static PsiAnnotation doFindAnnotationInHierarchy(PsiParameter parameter, + Set annotationNames, + Set visited) { + PsiAnnotation annotation = findAnnotation(parameter, annotationNames); + if (annotation != null) return annotation; + PsiElement scope = parameter.getDeclarationScope(); + if (!(scope instanceof PsiMethod)) { + return null; + } + PsiMethod method = (PsiMethod)scope; + PsiClass aClass = method.getContainingClass(); + PsiElement parent = parameter.getParent(); + if (aClass == null || !(parent instanceof PsiParameterList)) { + return null; + } + int index = ((PsiParameterList)parent).getParameterIndex(parameter); + HierarchicalMethodSignature methodSignature = method.getHierarchicalMethodSignature(); + + final List superSignatures = methodSignature.getSuperSignatures(); + PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(aClass.getProject()); + for (final HierarchicalMethodSignature superSignature : superSignatures) { + final PsiMethod superMethod = superSignature.getMethod(); + if (visited == null) visited = new THashSet(); + if (!visited.add(superMethod)) continue; + if (!resolveHelper.isAccessible(superMethod, parameter, null)) continue; + PsiParameter[] superParameters = superMethod.getParameterList().getParameters(); + if (index < superParameters.length) { + PsiAnnotation insuper = doFindAnnotationInHierarchy(superParameters[index], annotationNames, visited); + if (insuper != null) return insuper; + } + } return null; }