diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/ChangeContextUtil.java b/java/java-psi-impl/src/com/intellij/codeInsight/ChangeContextUtil.java index a9002429d91f..da997e971ab0 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/ChangeContextUtil.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/ChangeContextUtil.java @@ -15,6 +15,7 @@ */ package com.intellij.codeInsight; +import com.intellij.lang.ASTNode; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Key; import com.intellij.psi.*; @@ -29,12 +30,13 @@ import org.jetbrains.annotations.Nullable; public class ChangeContextUtil { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.ChangeContextUtil"); - public static final Key ENCODED_KEY = Key.create("ENCODED_KEY"); - public static final Key THIS_QUALIFIER_CLASS_KEY = Key.create("THIS_QUALIFIER_CLASS_KEY"); + private static final Key HARD_REF_TO_AST = Key.create("HARD_REF_TO_AST"); + private static final Key ENCODED_KEY = Key.create("ENCODED_KEY"); + private static final Key THIS_QUALIFIER_CLASS_KEY = Key.create("THIS_QUALIFIER_CLASS_KEY"); public static final Key REF_MEMBER_KEY = Key.create("REF_MEMBER_KEY"); public static final Key CAN_REMOVE_QUALIFIER_KEY = Key.create("CAN_REMOVE_QUALIFIER_KEY"); public static final Key REF_CLASS_KEY = Key.create("REF_CLASS_KEY"); - public static final Key REF_MEMBER_THIS_CLASS_KEY = Key.create("REF_MEMBER_THIS_CLASS_KEY"); + private static final Key REF_MEMBER_THIS_CLASS_KEY = Key.create("REF_MEMBER_THIS_CLASS_KEY"); private ChangeContextUtil() {} @@ -50,6 +52,11 @@ public class ChangeContextUtil { PsiElement topLevelScope, boolean includeRefClasses, boolean canChangeQualifier) { + if (scope instanceof StubBasedPsiElement) { + // as long as "scope" is reachable, don't let GC collect AST together with all the copyable user data + scope.putUserData(HARD_REF_TO_AST, scope.getNode()); + } + if (scope instanceof PsiThisExpression){ scope.putCopyableUserData(ENCODED_KEY, ""); @@ -116,6 +123,9 @@ public class ChangeContextUtil { public static PsiElement decodeContextInfo(@NotNull PsiElement scope, @Nullable PsiClass thisClass, @Nullable PsiExpression thisAccessExpr) throws IncorrectOperationException { + if (scope instanceof StubBasedPsiElement) { + scope.putUserData(HARD_REF_TO_AST, null); + } if (scope.getCopyableUserData(ENCODED_KEY) != null) { scope.putCopyableUserData(ENCODED_KEY, null); @@ -345,6 +355,9 @@ public class ChangeContextUtil { } public static void clearContextInfo(PsiElement scope) { + if (scope instanceof StubBasedPsiElement) { + scope.putUserData(HARD_REF_TO_AST, null); + } scope.putCopyableUserData(ENCODED_KEY, null); scope.putCopyableUserData(THIS_QUALIFIER_CLASS_KEY, null); scope.putCopyableUserData(REF_MEMBER_KEY, null);