default file template inspection to hold its internal PSI on a soft reference: prevents memory snapshot pollution with JavaFileElement's

This commit is contained in:
peter
2012-03-22 10:54:04 +01:00
parent 14c7d61811
commit 812951529f
2 changed files with 17 additions and 4 deletions
@@ -27,12 +27,13 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.UserDataHolderEx;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.reference.SoftReference;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.PatchedSoftReference;
import com.intellij.util.containers.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -75,12 +76,13 @@ public class MethodBodyChecker {
}
}
private static final Key<Map<String, PsiMethod>> CACHE_KEY = new Key<Map<String, PsiMethod>>("MethodBodyChecker templates cache");
private static final Key<SoftReference<Map<String, PsiMethod>>> CACHE_KEY = Key.create("MethodBodyChecker templates cache");
private static Map<String, PsiMethod> getTemplatesCache(PsiClass aClass) {
Map<String, PsiMethod> cache = aClass.getUserData(CACHE_KEY);
SoftReference<Map<String, PsiMethod>> ref = aClass.getUserData(CACHE_KEY);
Map<String, PsiMethod> cache = ref == null ? null : ref.get();
if (cache == null) {
cache = ((UserDataHolderEx)aClass).putUserDataIfAbsent(CACHE_KEY, new ConcurrentHashMap<String, PsiMethod>());
aClass.putUserData(CACHE_KEY, new PatchedSoftReference<Map<String, PsiMethod>>(cache = new ConcurrentHashMap<String, PsiMethod>()));
}
return cache;
}