2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com
.intellij
.psi
.impl
.source
;
18 import com
.intellij
.psi
.PsiClass
;
19 import com
.intellij
.psi
.PsiField
;
20 import com
.intellij
.psi
.PsiMethod
;
21 import com
.intellij
.psi
.impl
.PsiClassImplUtil
;
22 import com
.intellij
.psi
.impl
.PsiImplUtil
;
23 import gnu
.trove
.THashMap
;
24 import org
.jetbrains
.annotations
.NotNull
;
26 import java
.util
.ArrayList
;
27 import java
.util
.Collections
;
28 import java
.util
.List
;
31 public class ClassInnerStuffCache
{
32 private volatile Map
<String
, PsiField
> myCachedFieldsMap
= null;
33 private volatile Map
<String
, PsiMethod
[]> myCachedMethodsMap
= null;
34 private volatile Map
<String
, PsiClass
> myCachedInnersMap
= null;
36 private volatile PsiMethod
[] myCachedConstructors
= null;
37 private final PsiClass myClass
;
39 public ClassInnerStuffCache(final PsiClass aClass
) {
43 public void dropCaches() {
44 myCachedConstructors
= null;
46 myCachedFieldsMap
= null;
47 myCachedMethodsMap
= null;
48 myCachedInnersMap
= null;
52 public PsiMethod
[] getConstructors() {
53 PsiMethod
[] constructors
= myCachedConstructors
;
54 if (constructors
== null) {
55 myCachedConstructors
= constructors
= PsiImplUtil
.getConstructors(myClass
);
60 public PsiField
findFieldByName(String name
, boolean checkBases
) {
62 Map
<String
, PsiField
> cachedFields
= myCachedFieldsMap
;
63 if (cachedFields
== null) {
64 final PsiField
[] fields
= myClass
.getFields();
65 if (fields
.length
> 0) {
66 cachedFields
= new THashMap
<String
, PsiField
>();
67 for (final PsiField field
: fields
) {
68 cachedFields
.put(field
.getName(), field
);
70 myCachedFieldsMap
= cachedFields
;
73 myCachedFieldsMap
= Collections
.emptyMap();
77 return cachedFields
.get(name
);
79 return PsiClassImplUtil
.findFieldByName(myClass
, name
, checkBases
);
84 public PsiMethod
[] findMethodsByName(String name
, boolean checkBases
) {
86 Map
<String
, PsiMethod
[]> cachedMethods
= myCachedMethodsMap
;
87 if(cachedMethods
== null){
88 cachedMethods
= new THashMap
<String
,PsiMethod
[]>();
90 Map
<String
, List
<PsiMethod
>> cachedMethodsMap
= new THashMap
<String
,List
<PsiMethod
>>();
91 final PsiMethod
[] methods
= myClass
.getMethods();
92 for (final PsiMethod method
: methods
) {
93 List
<PsiMethod
> list
= cachedMethodsMap
.get(method
.getName());
95 list
= new ArrayList
<PsiMethod
>(1);
96 cachedMethodsMap
.put(method
.getName(), list
);
100 for (Map
.Entry
<String
, List
<PsiMethod
>> entry
: cachedMethodsMap
.entrySet()) {
101 List
<PsiMethod
> cached
= entry
.getValue();
102 String methodName
= entry
.getKey();
103 cachedMethods
.put(methodName
, cached
.toArray(new PsiMethod
[cached
.size()]));
105 myCachedMethodsMap
= cachedMethods
;
108 final PsiMethod
[] psiMethods
= cachedMethods
.get(name
);
109 return psiMethods
!= null ? psiMethods
: PsiMethod
.EMPTY_ARRAY
;
112 return PsiClassImplUtil
.findMethodsByName(myClass
, name
, checkBases
);
115 public PsiClass
findInnerClassByName(String name
, boolean checkBases
) {
117 Map
<String
, PsiClass
> inners
= myCachedInnersMap
;
118 if (inners
== null) {
119 final PsiClass
[] classes
= myClass
.getInnerClasses();
120 if (classes
.length
> 0) {
121 inners
= new THashMap
<String
, PsiClass
>();
122 for (final PsiClass psiClass
: classes
) {
123 inners
.put(psiClass
.getName(), psiClass
);
125 myCachedInnersMap
= inners
;
128 myCachedInnersMap
= Collections
.emptyMap();
132 return inners
.get(name
);
134 return PsiClassImplUtil
.findInnerByName(myClass
, name
, checkBases
);