Corrected class name extraction for inner classes, when checking ImportHelper.isAlrea... idea/142.4582
authorYaroslav Lepenkin <yaroslav.lepenkin@jetbrains.com>
Fri, 4 Sep 2015 09:43:04 +0000 (12:43 +0300)
committerYaroslav Lepenkin <yaroslav.lepenkin@jetbrains.com>
Fri, 4 Sep 2015 09:52:54 +0000 (12:52 +0300)
java/java-impl/src/com/intellij/psi/impl/source/codeStyle/ImportHelper.java
java/java-tests/testSrc/com/intellij/codeInsight/completion/JavadocCompletionTest.groovy

index e5cb7d55ed6119e74a0a82ef2f0ee2f3a7b8bc81..7d7e392f55bda8e9aab0667397daed586bec4b23 100644 (file)
@@ -577,7 +577,8 @@ public class ImportHelper{
   }
   
   public static boolean isAlreadyImported(@NotNull PsiJavaFile file, @NotNull String fullyQualifiedName) {
   }
   
   public static boolean isAlreadyImported(@NotNull PsiJavaFile file, @NotNull String fullyQualifiedName) {
-    String className = ClassUtil.extractClassName(fullyQualifiedName);
+    String className = extractClassName(file, fullyQualifiedName);
+
     Project project = file.getProject();
     PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(project);
 
     Project project = file.getProject();
     PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(project);
 
@@ -585,6 +586,18 @@ public class ImportHelper{
     return psiClass != null && fullyQualifiedName.equals(psiClass.getQualifiedName());
   }
 
     return psiClass != null && fullyQualifiedName.equals(psiClass.getQualifiedName());
   }
 
+  @NotNull
+  private static String extractClassName(@NotNull PsiJavaFile file, @NotNull String fullyQualifiedName) {
+    for (PsiClass aClass : file.getClasses()) {
+      String outerClassName = aClass.getQualifiedName();
+      if (outerClassName != null && fullyQualifiedName.startsWith(outerClassName)) {
+        return fullyQualifiedName.substring(outerClassName.lastIndexOf('.') + 1);
+      }
+    }
+
+    return ClassUtil.extractClassName(fullyQualifiedName);
+  }
+
   public ASTNode getDefaultAnchor(@NotNull PsiImportList list, @NotNull PsiImportStatementBase statement){
     PsiJavaCodeReferenceElement ref = statement.getImportReference();
     if (ref == null) return null;
   public ASTNode getDefaultAnchor(@NotNull PsiImportList list, @NotNull PsiImportStatementBase statement){
     PsiJavaCodeReferenceElement ref = statement.getImportReference();
     if (ref == null) return null;
index 317a3dd61cc96d7466c792d500971b3aeafdbfef..c33e6083de2a9b4a2689897f6f7c321c6467bfbb 100644 (file)
@@ -513,6 +513,41 @@ class Test {
 '''
   }
 
 '''
   }
 
+  public void testShortNameIfInnerClass() {
+    javaSettings.CLASS_NAMES_IN_JAVADOC = JavaCodeStyleSettings.FULLY_QUALIFY_NAMES_IF_NOT_IMPORTED
+    def text = '''
+package pkg;
+
+class Foo {
+
+    /**
+     * @throws FooE<caret>
+     */
+    void foo() {
+    }
+
+    static class FooException extends RuntimeException {}
+}
+'''
+    myFixture.configureByText "a.java", text
+    myFixture.completeBasic()
+    myFixture.type('\t')
+    myFixture.checkResult '''
+package pkg;
+
+class Foo {
+
+    /**
+     * @throws FooException 
+     */
+    void foo() {
+    }
+
+    static class FooException extends RuntimeException {}
+}
+'''
+  }
+
   public void testCustomReferenceProvider() throws Exception {
     PsiReferenceRegistrarImpl registrar =
       (PsiReferenceRegistrarImpl) ReferenceProvidersRegistry.getInstance().getRegistrar(StdLanguages.JAVA);
   public void testCustomReferenceProvider() throws Exception {
     PsiReferenceRegistrarImpl registrar =
       (PsiReferenceRegistrarImpl) ReferenceProvidersRegistry.getInstance().getRegistrar(StdLanguages.JAVA);