clear ClsParameter.name cache on roots change (IDEA-136727)
authorpeter <peter@jetbrains.com>
Wed, 25 Feb 2015 18:09:34 +0000 (19:09 +0100)
committerpeter <peter@jetbrains.com>
Wed, 25 Feb 2015 18:16:23 +0000 (19:16 +0100)
java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClsParameterImpl.java

index b4b3385239471f82356322f8c2e63a665be7d973..4cec970017411a8566736f6ec6e5896ca3dd5926 100644 (file)
@@ -16,6 +16,7 @@
 package com.intellij.psi.impl.compiled;
 
 import com.intellij.openapi.project.DumbService;
+import com.intellij.openapi.roots.FileIndexFacade;
 import com.intellij.openapi.util.AtomicNotNullLazyValue;
 import com.intellij.openapi.util.NotNullLazyValue;
 import com.intellij.psi.*;
@@ -30,10 +31,13 @@ import com.intellij.psi.impl.source.tree.TreeElement;
 import com.intellij.psi.search.LocalSearchScope;
 import com.intellij.psi.search.SearchScope;
 import com.intellij.psi.stubs.StubElement;
+import com.intellij.psi.util.CachedValueProvider;
+import com.intellij.psi.util.CachedValuesManager;
 import com.intellij.ui.RowIcon;
 import com.intellij.util.IncorrectOperationException;
 import com.intellij.util.PlatformIcons;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 
@@ -63,34 +67,37 @@ public class ClsParameterImpl extends ClsRepositoryPsiElement<PsiParameterStub>
 
   @Override
   public String getName() {
-    String name = myName;
-
-    if (name == null) {
-      PsiParameterStubImpl parameterStub = (PsiParameterStubImpl)getStub();
-      if (!parameterStub.isAutoGeneratedName()) {
-        name = parameterStub.getName();
-      }
-
-      if (name == null) {
-        if (DumbService.getInstance(getProject()).isDumb()) {
-          return null;
-        }
-
-        ClsMethodImpl method = (ClsMethodImpl)getDeclarationScope();
-        PsiMethod sourceMethod = method.getSourceMirrorMethod();
-        if (sourceMethod != null) {
-          assert sourceMethod != method : method;
-          name = sourceMethod.getParameterList().getParameters()[getIndex()].getName();
-        }
-        else {
-          name = getMirrorName();
-        }
+    return CachedValuesManager.getCachedValue(this, new CachedValueProvider<String>() {
+      @Nullable
+      @Override
+      public Result<String> compute() {
+        return Result.create(calcName(), 
+                             getContainingFile(),
+                             getContainingFile().getNavigationElement(),
+                             FileIndexFacade.getInstance(getProject()).getRootModificationTracker());
       }
+    });
+  }
 
-      myName = name;
+  @Nullable 
+  private String calcName() {
+    PsiParameterStubImpl parameterStub = (PsiParameterStubImpl)getStub();
+    if (!parameterStub.isAutoGeneratedName()) {
+      return parameterStub.getName();
+    }
+    
+    if (DumbService.getInstance(getProject()).isDumb()) {
+      return null;
     }
 
-    return name;
+    ClsMethodImpl method = (ClsMethodImpl)getDeclarationScope();
+    PsiMethod sourceMethod = method.getSourceMirrorMethod();
+    if (sourceMethod != null) {
+      assert sourceMethod != method : method;
+      return sourceMethod.getParameterList().getParameters()[getIndex()].getName();
+    }
+    
+    return getMirrorName();
   }
 
   public boolean isAutoGeneratedName() {