update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / source / ClassInnerStuffCache.java
blob3697d0e8bd8a320252564639e1762869c32cdfc9
1 /*
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;
29 import java.util.Map;
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) {
40 myClass = aClass;
43 public void dropCaches() {
44 myCachedConstructors = null;
46 myCachedFieldsMap = null;
47 myCachedMethodsMap = null;
48 myCachedInnersMap = null;
51 @NotNull
52 public PsiMethod[] getConstructors() {
53 PsiMethod[] constructors = myCachedConstructors;
54 if (constructors == null) {
55 myCachedConstructors = constructors = PsiImplUtil.getConstructors(myClass);
57 return constructors;
60 public PsiField findFieldByName(String name, boolean checkBases) {
61 if (!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;
72 else {
73 myCachedFieldsMap = Collections.emptyMap();
74 return null;
77 return cachedFields.get(name);
79 return PsiClassImplUtil.findFieldByName(myClass, name, checkBases);
83 @NotNull
84 public PsiMethod[] findMethodsByName(String name, boolean checkBases) {
85 if(!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());
94 if (list == null) {
95 list = new ArrayList<PsiMethod>(1);
96 cachedMethodsMap.put(method.getName(), list);
98 list.add(method);
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) {
116 if (!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;
127 else {
128 myCachedInnersMap = Collections.emptyMap();
129 return null;
132 return inners.get(name);
134 return PsiClassImplUtil.findInnerByName(myClass, name, checkBases);