1 19 20 package org.netbeans.modules.javacore.parser; 21 22 import java.lang.ref.SoftReference ; 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 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 47 Scope(Scope parent) { 48 parentScope=parent; 49 positiveCache=new HashMap(); 50 negativeCache=new HashSet(); 51 members=new ArrayList(); 52 } 53 54 public Object lookup(final Object key) { 55 Object 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 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 clone() { 84 Scope cloned=new Scope(parentScope); 85 86 cloned.members=(ArrayList)members.clone(); 87 return cloned; 88 } 89 90 private Object lookupMembers(final Object key) { 91 Iterator it=members.iterator(); 92 Object value=null; 93 94 while(it.hasNext()) { 95 ScopeMember m=(ScopeMember)it.next(); 96 Object 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 JMManager.getLog().log("Ambiguos reference "+key+":"+value+":"+val); } 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"); 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 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)); 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 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 [] 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 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 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"); 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 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 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"); 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 cacheId) { 329 ExclusiveMutex mutex=JMManager.getTransactionMutex(); 330 Map parserCache=mutex.getParserCache(); 331 332 SoftReference ref = (SoftReference ) 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 (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 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) { parent=computeTypeScope(JavaModelUtil.getDeclaringFeature((Element)element.refImmediateComposite())); 384 } else if (decl==null) { 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) { Scope parent=computeTypeScope(JavaModelUtil.getDeclaringFeature(element)); 417 418 classScope=new Scope(parent); 419 classScope.addMember(Scope.createMemberTypeScope((ClassDefinition)element,null)); 420 } else { 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 |