KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > ClassFileWorkingCopy


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
30  * A working copy on an <code>IClassFile</code>.
31  */

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/*no info available*/), 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 /**
78  * @see Openable#openBuffer(IProgressMonitor, Object)
79  */

80 protected IBuffer openBuffer(IProgressMonitor pm, Object JavaDoc info) throws JavaModelException {
81
82     // create buffer
83
IBuffer buffer = this.owner.createBuffer(this);
84     if (buffer == null) return null;
85     
86     // set the buffer source
87
if (buffer.getCharacters() == null) {
88         IBuffer classFileBuffer = this.classFile.getBuffer();
89         if (classFileBuffer != null) {
90             buffer.setContents(classFileBuffer.getCharacters());
91         } else {
92             // Disassemble
93
IClassFileReader reader = ToolFactory.createDefaultClassFileReader(this.classFile, IClassFileReader.ALL);
94             Disassembler disassembler = new Disassembler();
95             String JavaDoc contents = disassembler.disassemble(reader, Util.getLineSeparator("", getJavaProject()), ClassFileBytesDisassembler.WORKING_COPY); //$NON-NLS-1$
96
buffer.setContents(contents);
97         }
98     }
99
100     // add buffer to buffer cache
101
BufferManager bufManager = getBufferManager();
102     bufManager.addBuffer(buffer);
103             
104     // listen to buffer changes
105
buffer.addBufferChangedListener(this);
106     
107     return buffer;
108 }
109
110 protected void toStringName(StringBuffer JavaDoc buffer) {
111     buffer.append(this.classFile.getElementName());
112 }
113
114 }
115
Popular Tags