From c9fa78083843d9d35928e3fc7d650d581c118929 Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Mon, 23 Jan 2012 11:58:46 +0100 Subject: [PATCH] cache for anonymous names --- .../ide/util/JavaAnonymousClassesHelper.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 java/java-impl/src/com/intellij/ide/util/JavaAnonymousClassesHelper.java diff --git a/java/java-impl/src/com/intellij/ide/util/JavaAnonymousClassesHelper.java b/java/java-impl/src/com/intellij/ide/util/JavaAnonymousClassesHelper.java new file mode 100644 index 000000000000..f3902470507c --- /dev/null +++ b/java/java-impl/src/com/intellij/ide/util/JavaAnonymousClassesHelper.java @@ -0,0 +1,76 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.ide.util; + +import com.intellij.openapi.util.Key; +import com.intellij.psi.JavaRecursiveElementWalkingVisitor; +import com.intellij.psi.PsiAnonymousClass; +import com.intellij.psi.PsiClass; +import com.intellij.psi.util.CachedValue; +import com.intellij.psi.util.CachedValueProvider; +import com.intellij.psi.util.CachedValuesManager; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Konstantin Bulenkov + */ +public class JavaAnonymousClassesHelper { + private static final Key>> ANONYMOUS_CLASS_NAME = Key.create("ANONYMOUS_CLASS_NAME"); + + @Nullable + public static String getName(@NotNull PsiAnonymousClass cls) { + final PsiClass upper = PsiTreeUtil.getParentOfType(cls, PsiClass.class); + if (upper != null) { + final CachedValue> value = upper.getUserData(ANONYMOUS_CLASS_NAME); + if (value != null && value.hasUpToDateValue()) { + return value.getValue().get(cls); + } + final HashMap map = new HashMap(); + upper.accept(new JavaRecursiveElementWalkingVisitor() { + int index = 0; + + @Override + public void visitAnonymousClass(PsiAnonymousClass aClass) { + index++; + map.put(aClass, "$" + String.valueOf(index)); + } + + @Override + public void visitClass(PsiClass aClass) { + if (aClass == upper) { + super.visitClass(aClass); + } + } + }); + + final CachedValue> cachedValue = + CachedValuesManager.getManager(cls.getProject()).createCachedValue(new CachedValueProvider>() { + @Override + public Result> compute() { + return new Result>(map, upper); + } + }); + upper.putUserData(ANONYMOUS_CLASS_NAME, cachedValue); + return map.get(cls); + } + return null; + } +}