1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.Enumeration ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.core.resources.*; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 import org.eclipse.core.runtime.PerformanceStats; 21 import org.eclipse.jdt.core.*; 22 import org.eclipse.jdt.internal.codeassist.CompletionEngine; 23 import org.eclipse.jdt.internal.codeassist.SelectionEngine; 24 import org.eclipse.jdt.internal.core.util.Util; 25 26 27 33 public abstract class Openable extends JavaElement implements IOpenable, IBufferChangedListener { 34 35 protected Openable(JavaElement parent) { 36 super(parent); 37 } 38 46 public void bufferChanged(BufferChangedEvent event) { 47 if (event.getBuffer().isClosed()) { 48 JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().remove(this); 49 getBufferManager().removeBuffer(event.getBuffer()); 50 } else { 51 JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().add(this); 52 } 53 } 54 64 protected abstract boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException; 65 68 public boolean canBeRemovedFromCache() { 69 try { 70 return !hasUnsavedChanges(); 71 } catch (JavaModelException e) { 72 return false; 73 } 74 } 75 78 public boolean canBufferBeRemovedFromCache(IBuffer buffer) { 79 return !buffer.hasUnsavedChanges(); 80 } 81 84 protected void closeBuffer() { 85 if (!hasBuffer()) return; IBuffer buffer = getBufferManager().getBuffer(this); 87 if (buffer != null) { 88 buffer.close(); 89 buffer.removeBufferChangedListener(this); 90 } 91 } 92 95 protected void closing(Object info) { 96 closeBuffer(); 97 } 98 protected void codeComplete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit cu, org.eclipse.jdt.internal.compiler.env.ICompilationUnit unitToSkip, int position, CompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException { 99 if (requestor == null) { 100 throw new IllegalArgumentException ("Completion requestor cannot be null"); } 102 PerformanceStats performanceStats = CompletionEngine.PERF 103 ? PerformanceStats.getStats(JavaModelManager.COMPLETION_PERF, this) 104 : null; 105 if(performanceStats != null) { 106 performanceStats.startRun(new String (cu.getFileName()) + " at " + position); } 108 IBuffer buffer = getBuffer(); 109 if (buffer == null) { 110 return; 111 } 112 if (position < -1 || position > buffer.getLength()) { 113 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS)); 114 } 115 JavaProject project = (JavaProject) getJavaProject(); 116 SearchableEnvironment environment = project.newSearchableNameEnvironment(owner); 117 118 environment.unitToSkip = unitToSkip; 120 121 CompletionEngine engine = new CompletionEngine(environment, requestor, project.getOptions(true), project); 123 engine.complete(cu, position, 0); 124 if(performanceStats != null) { 125 performanceStats.endRun(); 126 } 127 if (NameLookup.VERBOSE) { 128 System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); } 131 } 132 protected IJavaElement[] codeSelect(org.eclipse.jdt.internal.compiler.env.ICompilationUnit cu, int offset, int length, WorkingCopyOwner owner) throws JavaModelException { 133 PerformanceStats performanceStats = SelectionEngine.PERF 134 ? PerformanceStats.getStats(JavaModelManager.SELECTION_PERF, this) 135 : null; 136 if(performanceStats != null) { 137 performanceStats.startRun(new String (cu.getFileName()) + " at [" + offset + "," + length + "]"); } 139 140 JavaProject project = (JavaProject)getJavaProject(); 141 SearchableEnvironment environment = project.newSearchableNameEnvironment(owner); 142 143 SelectionRequestor requestor= new SelectionRequestor(environment.nameLookup, this); 144 IBuffer buffer = getBuffer(); 145 if (buffer == null) { 146 return requestor.getElements(); 147 } 148 int end= buffer.getLength(); 149 if (offset < 0 || length < 0 || offset + length > end ) { 150 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS)); 151 } 152 153 SelectionEngine engine = new SelectionEngine(environment, requestor, project.getOptions(true)); 155 engine.select(cu, offset, offset + length - 1); 156 157 if(performanceStats != null) { 158 performanceStats.endRun(); 159 } 160 if (NameLookup.VERBOSE) { 161 System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); } 164 return requestor.getElements(); 165 } 166 169 protected Object createElementInfo() { 170 return new OpenableElementInfo(); 171 } 172 175 public boolean exists() { 176 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 177 if (manager.getInfo(this) != null) return true; 178 if (!parentExists()) return false; 179 PackageFragmentRoot root = getPackageFragmentRoot(); 180 if (root != null 181 && (root == this || !root.isArchive())) { 182 return resourceExists(); 183 } 184 return super.exists(); 185 } 186 public String findRecommendedLineSeparator() throws JavaModelException { 187 IBuffer buffer = getBuffer(); 188 String source = buffer == null ? null : buffer.getContents(); 189 return Util.getLineSeparator(source, getJavaProject()); 190 } 191 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor monitor) throws JavaModelException { 192 193 if (JavaModelCache.VERBOSE){ 194 String element; 195 switch (getElementType()) { 196 case JAVA_PROJECT: 197 element = "project"; break; 199 case PACKAGE_FRAGMENT_ROOT: 200 element = "root"; break; 202 case PACKAGE_FRAGMENT: 203 element = "package"; break; 205 case CLASS_FILE: 206 element = "class file"; break; 208 case COMPILATION_UNIT: 209 element = "compilation unit"; break; 211 default: 212 element = "element"; } 214 System.out.println(Thread.currentThread() +" OPENING " + element + " " + this.toStringWithAncestors()); } 216 217 openParent(info, newElements, monitor); 219 if (monitor != null && monitor.isCanceled()) 220 throw new OperationCanceledException(); 221 222 newElements.put(this, info); 225 226 try { 228 OpenableElementInfo openableElementInfo = (OpenableElementInfo)info; 229 boolean isStructureKnown = buildStructure(openableElementInfo, monitor, newElements, getResource()); 230 openableElementInfo.setIsStructureKnown(isStructureKnown); 231 } catch (JavaModelException e) { 232 newElements.remove(this); 233 throw e; 234 } 235 236 JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().remove(this); 238 239 if (JavaModelCache.VERBOSE) { 240 System.out.println(JavaModelManager.getJavaModelManager().cacheToString("-> ")); } 242 } 243 252 public IBuffer getBuffer() throws JavaModelException { 253 if (hasBuffer()) { 254 Object info = getElementInfo(); 256 IBuffer buffer = getBufferManager().getBuffer(this); 257 if (buffer == null) { 258 buffer = openBuffer(null, info); 260 } 261 if (buffer instanceof NullBuffer) { 262 return null; 263 } 264 return buffer; 265 } else { 266 return null; 267 } 268 } 269 273 public IBufferFactory getBufferFactory(){ 274 return getBufferManager().getDefaultBufferFactory(); 275 } 276 277 280 protected BufferManager getBufferManager() { 281 return BufferManager.getDefaultBufferManager(); 282 } 283 289 public IResource getCorrespondingResource() throws JavaModelException { 290 return getUnderlyingResource(); 291 } 292 295 public IOpenable getOpenable() { 296 return this; 297 } 298 299 300 301 304 public IResource getUnderlyingResource() throws JavaModelException { 305 IResource parentResource = this.parent.getUnderlyingResource(); 306 if (parentResource == null) { 307 return null; 308 } 309 int type = parentResource.getType(); 310 if (type == IResource.FOLDER || type == IResource.PROJECT) { 311 IContainer folder = (IContainer) parentResource; 312 IResource resource = folder.findMember(getElementName()); 313 if (resource == null) { 314 throw newNotPresentException(); 315 } else { 316 return resource; 317 } 318 } else { 319 return parentResource; 320 } 321 } 322 323 327 protected boolean hasBuffer() { 328 return false; 329 } 330 333 public boolean hasUnsavedChanges() throws JavaModelException{ 334 335 if (isReadOnly() || !isOpen()) { 336 return false; 337 } 338 IBuffer buf = this.getBuffer(); 339 if (buf != null && buf.hasUnsavedChanges()) { 340 return true; 341 } 342 int elementType = getElementType(); 345 if (elementType == PACKAGE_FRAGMENT || 346 elementType == PACKAGE_FRAGMENT_ROOT || 347 elementType == JAVA_PROJECT || 348 elementType == JAVA_MODEL) { Enumeration openBuffers= getBufferManager().getOpenBuffers(); 350 while (openBuffers.hasMoreElements()) { 351 IBuffer buffer= (IBuffer)openBuffers.nextElement(); 352 if (buffer.hasUnsavedChanges()) { 353 IJavaElement owner= (IJavaElement)buffer.getOwner(); 354 if (isAncestorOf(owner)) { 355 return true; 356 } 357 } 358 } 359 } 360 361 return false; 362 } 363 368 public boolean isConsistent() { 369 return true; 370 } 371 375 public boolean isOpen() { 376 return JavaModelManager.getJavaModelManager().getInfo(this) != null; 377 } 378 383 protected boolean isSourceElement() { 384 return false; 385 } 386 389 public boolean isStructureKnown() throws JavaModelException { 390 return ((OpenableElementInfo)getElementInfo()).isStructureKnown(); 391 } 392 395 public void makeConsistent(IProgressMonitor monitor) throws JavaModelException { 396 } 399 402 public void open(IProgressMonitor pm) throws JavaModelException { 403 getElementInfo(pm); 404 } 405 406 412 protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException { 413 return null; 414 } 415 416 419 protected void openParent(Object childInfo, HashMap newElements, IProgressMonitor pm) throws JavaModelException { 420 421 Openable openableParent = (Openable)getOpenableParent(); 422 if (openableParent != null && !openableParent.isOpen()){ 423 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm); 424 } 425 } 426 427 431 protected boolean parentExists(){ 432 433 IJavaElement parentElement = getParent(); 434 if (parentElement == null) return true; 435 return parentElement.exists(); 436 } 437 438 441 protected boolean resourceExists() { 442 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 443 if (workspace == null) return false; return 445 JavaModel.getTarget( 446 workspace.getRoot(), 447 this.getPath().makeRelative(), true) != null; 449 } 450 451 454 public void save(IProgressMonitor pm, boolean force) throws JavaModelException { 455 if (isReadOnly()) { 456 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this)); 457 } 458 IBuffer buf = getBuffer(); 459 if (buf != null) { buf.save(pm, force); 461 this.makeConsistent(pm); } 463 } 464 465 468 public PackageFragmentRoot getPackageFragmentRoot() { 469 return (PackageFragmentRoot) getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 470 } 471 472 } 473 | Popular Tags |