1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.jdt.core.*; 19 import org.eclipse.jdt.core.dom.ASTNode; 20 import org.eclipse.jdt.core.dom.CompilationUnit; 21 import org.eclipse.jdt.internal.core.util.DOMFinder; 22 import org.eclipse.jdt.internal.core.util.MementoTokenizer; 23 import org.eclipse.jdt.internal.core.util.Messages; 24 25 28 public abstract class SourceRefElement extends JavaElement implements ISourceReference { 29 37 public int occurrenceCount = 1; 38 39 protected SourceRefElement(JavaElement parent) { 40 super(parent); 41 } 42 45 protected void closing(Object info) throws JavaModelException { 46 } 48 51 protected Object createElementInfo() { 52 return null; } 54 57 public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException { 58 if (container == null) { 59 throw new IllegalArgumentException (Messages.operation_nullContainer); 60 } 61 IJavaElement[] elements= new IJavaElement[] {this}; 62 IJavaElement[] containers= new IJavaElement[] {container}; 63 IJavaElement[] siblings= null; 64 if (sibling != null) { 65 siblings= new IJavaElement[] {sibling}; 66 } 67 String [] renamings= null; 68 if (rename != null) { 69 renamings= new String [] {rename}; 70 } 71 getJavaModel().copy(elements, containers, siblings, renamings, force, monitor); 72 } 73 76 public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException { 77 IJavaElement[] elements = new IJavaElement[] {this}; 78 getJavaModel().delete(elements, force, monitor); 79 } 80 public boolean equals(Object o) { 81 if (!(o instanceof SourceRefElement)) return false; 82 return this.occurrenceCount == ((SourceRefElement)o).occurrenceCount && 83 super.equals(o); 84 } 85 89 public ASTNode findNode(CompilationUnit ast) { 90 DOMFinder finder = new DOMFinder(ast, this, false); 91 try { 92 return finder.search(); 93 } catch (JavaModelException e) { 94 return null; 96 } 97 } 98 101 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) throws JavaModelException { 102 Openable openableParent = (Openable)getOpenableParent(); 103 if (openableParent == null) return; 104 105 JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo(openableParent); 106 if (openableParentInfo == null) { 107 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm); 108 } 109 } 110 113 public ICompilationUnit getCompilationUnit() { 114 return (ICompilationUnit) getAncestor(COMPILATION_UNIT); 115 } 116 122 public IResource getCorrespondingResource() throws JavaModelException { 123 if (!exists()) throw newNotPresentException(); 124 return null; 125 } 126 129 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) { 130 switch (token.charAt(0)) { 131 case JEM_COUNT: 132 return getHandleUpdatingCountFromMemento(memento, workingCopyOwner); 133 } 134 return this; 135 } 136 protected void getHandleMemento(StringBuffer buff) { 137 super.getHandleMemento(buff); 138 if (this.occurrenceCount > 1) { 139 buff.append(JEM_COUNT); 140 buff.append(this.occurrenceCount); 141 } 142 } 143 147 public IJavaElement getHandleUpdatingCountFromMemento(MementoTokenizer memento, WorkingCopyOwner owner) { 148 if (!memento.hasMoreTokens()) return this; 149 this.occurrenceCount = Integer.parseInt(memento.nextToken()); 150 if (!memento.hasMoreTokens()) return this; 151 String token = memento.nextToken(); 152 return getHandleFromMemento(token, memento, owner); 153 } 154 157 public int getOccurrenceCount() { 158 return this.occurrenceCount; 159 } 160 164 public IOpenable getOpenableParent() { 165 IJavaElement current = getParent(); 166 while (current != null){ 167 if (current instanceof IOpenable){ 168 return (IOpenable) current; 169 } 170 current = current.getParent(); 171 } 172 return null; 173 } 174 177 public IPath getPath() { 178 return this.getParent().getPath(); 179 } 180 183 public IResource getResource() { 184 return this.getParent().getResource(); 185 } 186 189 public String getSource() throws JavaModelException { 190 IOpenable openable = getOpenableParent(); 191 IBuffer buffer = openable.getBuffer(); 192 if (buffer == null) { 193 return null; 194 } 195 ISourceRange range = getSourceRange(); 196 int offset = range.getOffset(); 197 int length = range.getLength(); 198 if (offset == -1 || length == 0 ) { 199 return null; 200 } 201 try { 202 return buffer.getText(offset, length); 203 } catch(RuntimeException e) { 204 return null; 205 } 206 } 207 210 public ISourceRange getSourceRange() throws JavaModelException { 211 SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo(); 212 return info.getSourceRange(); 213 } 214 217 public IResource getUnderlyingResource() throws JavaModelException { 218 if (!exists()) throw newNotPresentException(); 219 return getParent().getUnderlyingResource(); 220 } 221 224 public boolean hasChildren() throws JavaModelException { 225 return getChildren().length > 0; 226 } 227 230 public boolean isStructureKnown() throws JavaModelException { 231 return true; 233 } 234 237 public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException { 238 if (container == null) { 239 throw new IllegalArgumentException (Messages.operation_nullContainer); 240 } 241 IJavaElement[] elements= new IJavaElement[] {this}; 242 IJavaElement[] containers= new IJavaElement[] {container}; 243 IJavaElement[] siblings= null; 244 if (sibling != null) { 245 siblings= new IJavaElement[] {sibling}; 246 } 247 String [] renamings= null; 248 if (rename != null) { 249 renamings= new String [] {rename}; 250 } 251 getJavaModel().move(elements, containers, siblings, renamings, force, monitor); 252 } 253 256 public void rename(String newName, boolean force, IProgressMonitor monitor) throws JavaModelException { 257 if (newName == null) { 258 throw new IllegalArgumentException (Messages.element_nullName); 259 } 260 IJavaElement[] elements= new IJavaElement[] {this}; 261 IJavaElement[] dests= new IJavaElement[] {this.getParent()}; 262 String [] renamings= new String [] {newName}; 263 getJavaModel().rename(elements, dests, renamings, force, monitor); 264 } 265 protected void toStringName(StringBuffer buffer) { 266 super.toStringName(buffer); 267 if (this.occurrenceCount > 1) { 268 buffer.append("#"); buffer.append(this.occurrenceCount); 270 } 271 } 272 } 273 | Popular Tags |