KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > Scope


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javacore.parser;
21
22 import java.lang.ref.SoftReference JavaDoc;
23 import java.util.*;
24 import org.netbeans.api.java.classpath.ClassPath;
25 import org.netbeans.jmi.javamodel.*;
26 import org.netbeans.mdr.handlers.BaseObjectHandler;
27 import org.netbeans.mdr.persistence.MOFID;
28 import org.netbeans.modules.javacore.ExclusiveMutex;
29 import org.netbeans.modules.javacore.JMManager;
30 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
31 import org.netbeans.modules.javacore.internalapi.JavaModelUtil;
32 import org.netbeans.modules.javacore.jmiimpl.javamodel.JavaClassClassImpl;
33 import org.netbeans.modules.javacore.jmiimpl.javamodel.JavaClassImpl;
34
35 /**
36  *
37  * @author Tomas Hurka
38  */

39 public class Scope implements ScopeMember {
40     
41     private Scope parentScope;
42     private Map positiveCache;
43     private Set negativeCache;
44     private ArrayList members;
45     
46     /** Creates a new instance of Scope */
47     Scope(Scope parent) {
48         parentScope=parent;
49         positiveCache=new HashMap();
50         negativeCache=new HashSet();
51         members=new ArrayList();
52     }
53     
54     public Object JavaDoc lookup(final Object JavaDoc key) {
55         Object JavaDoc val=positiveCache.get(key);
56         if (val!=null)
57             return val;
58         if (!negativeCache.contains(key))
59             val=lookupMembers(key);
60         if (val==null && parentScope!=null)
61             val=parentScope.lookup(key);
62         return val;
63     }
64     
65     void addMember(final ScopeMember member) {
66         positiveCache.clear();
67         negativeCache.clear();
68         members.add(member);
69     }
70     
71     public boolean equals(Object JavaDoc obj) {
72         if (obj!=null && obj instanceof Scope) {
73             Scope sc=(Scope)obj;
74             if (!members.equals(sc.members))
75                 return false;
76             if (parentScope==null)
77                 return parentScope==sc.parentScope;
78             return parentScope.equals(sc.parentScope);
79         }
80         return false;
81     }
82     
83     protected Object JavaDoc clone() {
84         Scope cloned=new Scope(parentScope);
85         
86         cloned.members=(ArrayList)members.clone();
87         return cloned;
88     }
89     
90     private Object JavaDoc lookupMembers(final Object JavaDoc key) {
91         Iterator it=members.iterator();
92         Object JavaDoc value=null;
93         
94         while(it.hasNext()) {
95             ScopeMember m=(ScopeMember)it.next();
96             Object JavaDoc val=m.lookup(key);
97             
98             if (val==null)
99                 continue;
100             if (value==null) {
101                 value=val;
102                 continue;
103             }
104             if (!value.equals(val))
105                 // ambiguos reference
106
JMManager.getLog().log("Ambiguos reference "+key+":"+value+":"+val); // NOI18N
107
}
108         if (value!=null)
109             positiveCache.put(key,value);
110         else
111             negativeCache.add(key);
112         return value;
113     }
114     
115     static Scope createMemberTypeScope(ClassDefinition jcls,MDRParser sc) {
116         if (jcls instanceof ParameterizedType)
117             jcls=((ParameterizedType)jcls).getDefinition();
118         if (jcls instanceof UnresolvedClass)
119             return new Scope(null);
120         MOFID id=((BaseObjectHandler) jcls)._getMofId();
121         Map cache=getCacheFor(Scope.class.getName()+"createMemberTypeScope"); // NOI18N
122
Scope scope=(Scope)cache.get(id);
123
124         if (scope==null) {
125             cache.put(id,new Scope(null));
126             scope=constructMemberTypeScope(jcls, sc);
127             cache.put(id,scope);
128         }
129         return scope;
130     }
131     
132     static Scope constructMemberTypeScope(ClassDefinition jcls,MDRParser sc) {
133         List superScopes=new ArrayList(2);
134         Scope superScope=null;
135         Scope memberScope;
136         JavaClass superJavaClass=getSuperClass(jcls,sc);
137         Collection interfaces=getInterfaces(jcls,sc);
138         Iterator ifaceIt=interfaces.iterator();
139         
140         if (superJavaClass!=null && !(superJavaClass instanceof UnresolvedClass))
141             superScopes.add(createMemberTypeScope(superJavaClass,sc));
142         while (ifaceIt.hasNext()) {
143             JavaClass ifaceClass=(JavaClass)ifaceIt.next();
144             
145             if (ifaceClass!=null && !(ifaceClass instanceof UnresolvedClass))
146                 superScopes.add(createMemberTypeScope(ifaceClass,sc));
147         }
148         if (!superScopes.isEmpty()) {
149             Iterator scopeIt=superScopes.iterator();
150
151             superScope=new Scope(null);
152             while(scopeIt.hasNext())
153                 superScope.addMember((ScopeMember)scopeIt.next());
154         }
155         memberScope=new Scope(superScope);
156         if (jcls instanceof JavaClass)
157             memberScope.addMember(new MemberClassScope((JavaClass)jcls));
158         return memberScope;
159     }
160     
161     static Scope createTypeScope(String JavaDoc jpck, ClassPath classPath, ElementInfo[] imports) {
162         Scope packageImp=new Scope(null);
163         Scope currentPackage=new Scope(packageImp);
164         Scope singleImp=new Scope(currentPackage);
165         Scope memberTypes=new Scope(singleImp);
166         Iterator it;
167         Resource rsc;
168         
169         packageImp.addMember(new PackageImpScope("java.lang",classPath)); // NOI18N
170
currentPackage.addMember(new PackageImpScope(jpck,classPath));
171         if (imports != null) {
172             for (int i = 0; i < imports.length; i++) {
173                 ElementInfo importInfo=imports[i];
174                 String JavaDoc name=importInfo.name;
175                 
176                 if (importInfo.infoType==ElementInfo.IMPORT_ON_DEMAND_TYPE)
177                     packageImp.addMember(new PackageImpScope(name,classPath));
178                 else
179                     singleImp.addMember(new SingleImpScope(name));
180             }
181         }
182         return singleImp;
183     }
184
185     static Scope createStaticImpScope(Resource rsc) {
186         Scope packageImp=new Scope(null);
187         Scope singleImp=new Scope(packageImp);
188         Scope memberTypes=new Scope(singleImp);
189         Object JavaDoc[] imports=rsc.getImports().toArray();
190         JavaClassClassImpl jclsClass=(JavaClassClassImpl)((JavaModelPackage)rsc.refImmediatePackage()).getJavaClass();
191         
192         for(int i=0;i<imports.length;i++) {
193             Import imp=(Import)imports[i];
194
195             if (imp.isStatic()) {
196                 String JavaDoc name=imp.getName();
197                 
198                 if (imp.isOnDemand()) {
199                     JavaClass javaClass =(JavaClass)jclsClass.resolveClass(name, true);
200                     
201                     if (javaClass!=null)
202                         packageImp.addMember(createStaticImportScope(javaClass,null));
203                 } else {
204                     int lastDot=name.lastIndexOf('.');
205                     
206                     if (lastDot!=-1) {
207                         String JavaDoc className=name.substring(0,lastDot);
208                         JavaClass javaClass =(JavaClass)jclsClass.resolveClass(className, true);
209                         
210                         if (javaClass!=null)
211                             singleImp.addMember(createStaticImportScope(javaClass,name.substring(lastDot+1)));
212                     }
213                 }
214             }
215         }
216         return singleImp;
217     }
218     
219     static MethodScope createMethodScope(ClassDefinition jcls) {
220         if (jcls instanceof UnresolvedClass)
221             return new MethodScope(null);
222         MOFID id=((BaseObjectHandler) jcls)._getMofId();
223         Map cache=getCacheFor(Scope.class.getName()+"createMethodScope"); // NOI18N
224
MethodScope scope=(MethodScope)cache.get(id);
225
226         if (scope==null) {
227             scope=createMethodScopeImpl(jcls);
228             cache.put(id,scope);
229         }
230         return scope;
231     }
232
233     private static MethodScope createMethodScopeImpl(ClassDefinition jcls) {
234         JavaClass superJavaClass=jcls.getSuperClass();
235         Iterator ifaceIt=jcls.getInterfaces().iterator();
236         List superScopes=new ArrayList(2);
237         Iterator scopeIt;
238         MethodScope superScope;
239         MethodScope memberScope;
240         
241         if (superJavaClass!=null)
242             superScopes.add(createMethodScope(superJavaClass));
243         while (ifaceIt.hasNext()) {
244             JavaClass ifaceClass=(JavaClass)ifaceIt.next();
245             
246             if (ifaceClass!=null)
247                 superScopes.add(createMethodScope(ifaceClass));
248         }
249         superScope=new MethodScope(null);
250         scopeIt=superScopes.iterator();
251         while(scopeIt.hasNext())
252             superScope.addMember((ScopeMember)scopeIt.next());
253         memberScope=new MethodScope(superScope);
254         memberScope.addMember(new MethodScopeMember(jcls));
255         return memberScope;
256     }
257
258     static Scope createStaticImportScope(JavaClass jcls,String JavaDoc name) {
259         if (jcls instanceof UnresolvedClass)
260             return new Scope(null);
261         return createStaticImportScopeImpl(jcls,name);
262     }
263
264     private static Scope createStaticImportScopeImpl(JavaClass jcls,String JavaDoc name) {
265         JavaClass superJavaClass=jcls.getSuperClass();
266         Iterator ifaceIt=jcls.getInterfaces().iterator();
267         List superScopes=new ArrayList(2);
268         Iterator scopeIt;
269         Scope superScope;
270         Scope memberScope;
271         
272         if (superJavaClass!=null)
273             superScopes.add(createStaticImportScope(superJavaClass,name));
274         while (ifaceIt.hasNext()) {
275             JavaClass ifaceClass=(JavaClass)ifaceIt.next();
276             
277             if (ifaceClass!=null)
278                 superScopes.add(createStaticImportScope(ifaceClass,name));
279         }
280         superScope=new Scope(null);
281         scopeIt=superScopes.iterator();
282         while(scopeIt.hasNext())
283             superScope.addMember((ScopeMember)scopeIt.next());
284         memberScope=new Scope(superScope);
285         memberScope.addMember(new StaticImportScope(jcls,name));
286         return memberScope;
287     }
288     
289     static Scope createFieldScope(ClassDefinition jcls) {
290         if (jcls instanceof UnresolvedClass)
291             return new Scope(null);
292         MOFID id=((BaseObjectHandler) jcls)._getMofId();
293         Map cache=getCacheFor(Scope.class.getName()+"createFieldScope"); // NOI18N
294
Scope scope=(Scope)cache.get(id);
295
296         if (scope==null) {
297             scope=createFieldScopeImpl(jcls);
298             cache.put(id,scope);
299         }
300         return scope;
301     }
302     
303     private static Scope createFieldScopeImpl(ClassDefinition jcls) {
304         JavaClass superJavaClass=jcls.getSuperClass();
305         Iterator ifaceIt=jcls.getInterfaces().iterator();
306         List superScopes=new ArrayList(2);
307         Iterator scopeIt;
308         Scope superScope;
309         Scope memberScope;
310         
311         if (superJavaClass!=null)
312             superScopes.add(createFieldScope(superJavaClass));
313         while (ifaceIt.hasNext()) {
314             JavaClass ifaceClass=(JavaClass)ifaceIt.next();
315             
316             if (ifaceClass!=null)
317                 superScopes.add(createFieldScope(ifaceClass));
318         }
319         superScope=new Scope(null);
320         scopeIt=superScopes.iterator();
321         while(scopeIt.hasNext())
322             superScope.addMember((ScopeMember)scopeIt.next());
323         memberScope=new Scope(superScope);
324         memberScope.addMember(new MemberFieldScope(jcls));
325         return memberScope;
326     }
327
328     private static Map getCacheFor(String JavaDoc cacheId) {
329         ExclusiveMutex mutex=JMManager.getTransactionMutex();
330         Map parserCache=mutex.getParserCache();
331         
332         SoftReference JavaDoc ref = (SoftReference JavaDoc) parserCache.get(cacheId);
333         Map cache = ref == null ? null : (Map) ref.get();
334
335         
336         if (cache==null) {
337             cache=new HashMap();
338             parserCache.put(cacheId,new SoftReference JavaDoc(cache));
339         }
340         return cache;
341     }
342     
343     private static JavaClass getSuperClass(ClassDefinition jcls,MDRParser sc) {
344         if (sc!=null)
345             return sc.getSuperClass(jcls);
346         return jcls.getSuperClass();
347     }
348     
349     private static Collection getInterfaces(ClassDefinition jcls,MDRParser sc) {
350         if (sc!=null)
351             return sc.getInterfaces(jcls);
352         return jcls.getInterfaces();
353     }
354
355     public static Scope createTypeScope(Resource rsc,ClassPath classPath) {
356         String JavaDoc jpck=rsc.getPackageName();
357         Collection importCol=rsc.getImports();
358         Iterator it=importCol.iterator();
359         List imports=new ArrayList();
360         
361         while(it.hasNext()) {
362             Import imp=(Import)it.next();
363             if (!imp.isStatic()) {
364                 int infoType=imp.isOnDemand()?ElementInfo.IMPORT_ON_DEMAND_TYPE:ElementInfo.SINGLE_IMPORT_TYPE;
365
366                 imports.add(new ElementInfo(null, infoType, imp.getName()));
367             }
368         }
369         return createTypeScope(jpck,classPath,(ElementInfo[])imports.toArray(new ElementInfo[imports.size()]));
370     }
371     
372     
373     public static Scope computeTypeScope(Element element) {
374         Scope classScope;
375         
376         if (element instanceof JavaClass) {
377             JavaClass javaClass=((JavaClass)element);
378             ClassDefinition decl=javaClass.getDeclaringClass();
379             Iterator typeParIt;
380             Scope parent;
381             
382             if (element instanceof JavaClassImpl && ((JavaClassImpl)element).isTransient() && decl==null) { // local class
383
parent=computeTypeScope(JavaModelUtil.getDeclaringFeature((Element)element.refImmediateComposite()));
384             } else if (decl==null) { // jcls is top-level class
385
ClassPath cp=JavaMetamodel.getManager().getClassPath();
386                 
387                 parent=Scope.createTypeScope(element.getResource(),cp);
388             } else {
389                 parent=computeTypeScope(decl);
390             }
391             classScope=new Scope(parent);
392             classScope.addMember(Scope.createMemberTypeScope(javaClass,null));
393             typeParIt=javaClass.getTypeParameters().iterator();
394             while(typeParIt.hasNext()) {
395                 TypeParameter tp=(TypeParameter)typeParIt.next();
396                 
397                 classScope.addMember(new TypeParamScope(tp));
398             }
399         } else if (element instanceof ClassMember) {
400             ClassMember cm=(ClassMember)element;
401             
402             classScope=computeTypeScope(cm.getDeclaringClass());
403             if (cm instanceof CallableFeature) {
404                 CallableFeature cf=(CallableFeature)cm;
405                 Iterator typeParIt;
406                 
407                 classScope=new Scope(classScope);
408                 typeParIt=cf.getTypeParameters().iterator();
409                 while(typeParIt.hasNext()) {
410                     TypeParameter tp=(TypeParameter)typeParIt.next();
411
412                     classScope.addMember(new TypeParamScope(tp));
413                 }
414             }
415         } else if (element instanceof ClassDefinition) { // anonymous class
416
Scope parent=computeTypeScope(JavaModelUtil.getDeclaringFeature(element));
417             
418             classScope=new Scope(parent);
419             classScope.addMember(Scope.createMemberTypeScope((ClassDefinition)element,null));
420         } else { // inside block
421
Feature feature=JavaModelUtil.getDeclaringFeature(element);
422                 
423             assert feature!=null:"Invalid element "+element;
424             classScope=computeTypeScope(feature);
425         }
426         return classScope;
427     }
428 }
429
Popular Tags