|
| 1 | +/* |
| 2 | + * Copyright 2000-2014 JetBrains s.r.o. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.intellij.execution.filters; |
| 17 | + |
| 18 | +import java.util.concurrent.ConcurrentMap; |
| 19 | + |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import com.intellij.openapi.project.DumbService; |
| 22 | +import com.intellij.openapi.project.Project; |
| 23 | +import com.intellij.openapi.util.Pair; |
| 24 | +import com.intellij.psi.JavaPsiFacade; |
| 25 | +import com.intellij.psi.PsiClass; |
| 26 | +import com.intellij.psi.PsiFile; |
| 27 | +import com.intellij.psi.search.GlobalSearchScope; |
| 28 | +import com.intellij.util.ObjectUtil; |
| 29 | +import com.intellij.util.containers.ContainerUtil; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author peter |
| 33 | + */ |
| 34 | +public class ExceptionInfoCache |
| 35 | +{ |
| 36 | + private final ConcurrentMap<String, Pair<PsiClass[], PsiFile[]>> myCache = ContainerUtil.createConcurrentSoftValueMap(); |
| 37 | + private final Project myProject; |
| 38 | + private final GlobalSearchScope mySearchScope; |
| 39 | + |
| 40 | + public ExceptionInfoCache(GlobalSearchScope searchScope) |
| 41 | + { |
| 42 | + myProject = ObjectUtil.assertNotNull(searchScope.getProject()); |
| 43 | + mySearchScope = searchScope; |
| 44 | + } |
| 45 | + |
| 46 | + @NotNull |
| 47 | + public Project getProject() |
| 48 | + { |
| 49 | + return myProject; |
| 50 | + } |
| 51 | + |
| 52 | + @NotNull |
| 53 | + private PsiClass[] findClassesPreferringMyScope(String className) |
| 54 | + { |
| 55 | + JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(myProject); |
| 56 | + PsiClass[] result = psiFacade.findClasses(className, mySearchScope); |
| 57 | + return result.length != 0 ? result : psiFacade.findClasses(className, GlobalSearchScope.allScope(myProject)); |
| 58 | + } |
| 59 | + |
| 60 | + Pair<PsiClass[], PsiFile[]> resolveClass(String className) |
| 61 | + { |
| 62 | + Pair<PsiClass[], PsiFile[]> cached = myCache.get(className); |
| 63 | + if(cached != null) |
| 64 | + { |
| 65 | + return cached; |
| 66 | + } |
| 67 | + |
| 68 | + if(DumbService.isDumb(myProject)) |
| 69 | + { |
| 70 | + return Pair.create(PsiClass.EMPTY_ARRAY, PsiFile.EMPTY_ARRAY); |
| 71 | + } |
| 72 | + |
| 73 | + PsiClass[] classes = findClassesPreferringMyScope(className); |
| 74 | + if(classes.length == 0) |
| 75 | + { |
| 76 | + final int dollarIndex = className.indexOf('$'); |
| 77 | + if(dollarIndex >= 0) |
| 78 | + { |
| 79 | + classes = findClassesPreferringMyScope(className.substring(0, dollarIndex)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + PsiFile[] files = new PsiFile[classes.length]; |
| 84 | + for(int i = 0; i < classes.length; i++) |
| 85 | + { |
| 86 | + files[i] = (PsiFile) classes[i].getContainingFile().getNavigationElement(); |
| 87 | + } |
| 88 | + |
| 89 | + Pair<PsiClass[], PsiFile[]> result = Pair.create(classes, files); |
| 90 | + myCache.put(className, result); |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments