1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.runtime.IPath; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.jdt.core.IBuffer; 17 import org.eclipse.jdt.core.IClassFile; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IJavaModelStatusConstants; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.core.ToolFactory; 22 import org.eclipse.jdt.core.WorkingCopyOwner; 23 import org.eclipse.jdt.core.compiler.CharOperation; 24 import org.eclipse.jdt.core.util.ClassFileBytesDisassembler; 25 import org.eclipse.jdt.core.util.IClassFileReader; 26 import org.eclipse.jdt.internal.core.util.Disassembler; 27 import org.eclipse.jdt.internal.core.util.Util; 28 29 32 public class ClassFileWorkingCopy extends CompilationUnit { 33 34 public IClassFile classFile; 35 36 public ClassFileWorkingCopy(IClassFile classFile, WorkingCopyOwner owner) { 37 super((PackageFragment) classFile.getParent(), ((BinaryType) ((ClassFile) classFile).getType()).getSourceFileName(null), owner); 38 this.classFile = classFile; 39 } 40 41 public void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws JavaModelException { 42 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this)); 43 } 44 45 public IBuffer getBuffer() throws JavaModelException { 46 if (isWorkingCopy()) 47 return super.getBuffer(); 48 else 49 return this.classFile.getBuffer(); 50 } 51 52 public char[] getContents() { 53 try { 54 IBuffer buffer = getBuffer(); 55 if (buffer == null) return CharOperation.NO_CHAR; 56 char[] characters = buffer.getCharacters(); 57 if (characters == null) return CharOperation.NO_CHAR; 58 return characters; 59 } catch (JavaModelException e) { 60 return CharOperation.NO_CHAR; 61 } 62 } 63 64 public IPath getPath() { 65 return this.classFile.getPath(); 66 } 67 68 public IJavaElement getPrimaryElement(boolean checkOwner) { 69 if (checkOwner && isPrimary()) return this; 70 return new ClassFileWorkingCopy(this.classFile, DefaultWorkingCopyOwner.PRIMARY); 71 } 72 73 public IResource getResource() { 74 return this.classFile.getResource(); 75 } 76 77 80 protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException { 81 82 IBuffer buffer = this.owner.createBuffer(this); 84 if (buffer == null) return null; 85 86 if (buffer.getCharacters() == null) { 88 IBuffer classFileBuffer = this.classFile.getBuffer(); 89 if (classFileBuffer != null) { 90 buffer.setContents(classFileBuffer.getCharacters()); 91 } else { 92 IClassFileReader reader = ToolFactory.createDefaultClassFileReader(this.classFile, IClassFileReader.ALL); 94 Disassembler disassembler = new Disassembler(); 95 String contents = disassembler.disassemble(reader, Util.getLineSeparator("", getJavaProject()), ClassFileBytesDisassembler.WORKING_COPY); buffer.setContents(contents); 97 } 98 } 99 100 BufferManager bufManager = getBufferManager(); 102 bufManager.addBuffer(buffer); 103 104 buffer.addBufferChangedListener(this); 106 107 return buffer; 108 } 109 110 protected void toStringName(StringBuffer buffer) { 111 buffer.append(this.classFile.getElementName()); 112 } 113 114 } 115 | Popular Tags |