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.IJavaElement; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.core.WorkingCopyOwner; 22 import org.eclipse.jdt.internal.core.util.MementoTokenizer; 23 import org.eclipse.jdt.internal.core.util.Util; 24 25 26 public class LocalVariable extends SourceRefElement implements ILocalVariable { 27 28 String name; 29 public int declarationSourceStart, declarationSourceEnd; 30 public int nameStart, nameEnd; 31 String typeSignature; 32 33 public LocalVariable( 34 JavaElement parent, 35 String name, 36 int declarationSourceStart, 37 int declarationSourceEnd, 38 int nameStart, 39 int nameEnd, 40 String typeSignature) { 41 42 super(parent); 43 this.name = name; 44 this.declarationSourceStart = declarationSourceStart; 45 this.declarationSourceEnd = declarationSourceEnd; 46 this.nameStart = nameStart; 47 this.nameEnd = nameEnd; 48 this.typeSignature = typeSignature; 49 } 50 51 protected void closing(Object info) { 52 } 54 55 protected Object createElementInfo() { 56 return null; 58 } 59 60 public boolean equals(Object o) { 61 if (!(o instanceof LocalVariable)) return false; 62 LocalVariable other = (LocalVariable)o; 63 return 64 this.declarationSourceStart == other.declarationSourceStart 65 && this.declarationSourceEnd == other.declarationSourceEnd 66 && this.nameStart == other.nameStart 67 && this.nameEnd == other.nameEnd 68 && super.equals(o); 69 } 70 71 public boolean exists() { 72 return this.parent.exists(); } 74 75 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) { 76 } 78 79 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) { 80 switch (token.charAt(0)) { 81 case JEM_COUNT: 82 return getHandleUpdatingCountFromMemento(memento, owner); 83 } 84 return this; 85 } 86 87 90 protected void getHandleMemento(StringBuffer buff) { 91 ((JavaElement)getParent()).getHandleMemento(buff); 92 buff.append(getHandleMementoDelimiter()); 93 buff.append(this.name); 94 buff.append(JEM_COUNT); 95 buff.append(this.declarationSourceStart); 96 buff.append(JEM_COUNT); 97 buff.append(this.declarationSourceEnd); 98 buff.append(JEM_COUNT); 99 buff.append(this.nameStart); 100 buff.append(JEM_COUNT); 101 buff.append(this.nameEnd); 102 buff.append(JEM_COUNT); 103 buff.append(this.typeSignature); 104 if (this.occurrenceCount > 1) { 105 buff.append(JEM_COUNT); 106 buff.append(this.occurrenceCount); 107 } 108 } 109 110 protected char getHandleMementoDelimiter() { 111 return JavaElement.JEM_LOCALVARIABLE; 112 } 113 114 public IResource getCorrespondingResource() { 115 return null; 116 } 117 118 public String getElementName() { 119 return this.name; 120 } 121 122 public int getElementType() { 123 return LOCAL_VARIABLE; 124 } 125 126 public ISourceRange getNameRange() { 127 return new SourceRange(this.nameStart, this.nameEnd-this.nameStart+1); 128 } 129 130 public IPath getPath() { 131 return this.parent.getPath(); 132 } 133 134 public IResource getResource() { 135 return this.parent.getResource(); 136 } 137 138 141 public String getSource() throws JavaModelException { 142 IOpenable openable = this.parent.getOpenableParent(); 143 IBuffer buffer = openable.getBuffer(); 144 if (buffer == null) { 145 return null; 146 } 147 ISourceRange range = getSourceRange(); 148 int offset = range.getOffset(); 149 int length = range.getLength(); 150 if (offset == -1 || length == 0 ) { 151 return null; 152 } 153 try { 154 return buffer.getText(offset, length); 155 } catch(RuntimeException e) { 156 return null; 157 } 158 } 159 160 163 public ISourceRange getSourceRange() { 164 return new SourceRange(this.declarationSourceStart, this.declarationSourceEnd-this.declarationSourceStart+1); 165 } 166 167 public String getTypeSignature() { 168 return this.typeSignature; 169 } 170 171 public IResource getUnderlyingResource() throws JavaModelException { 172 return this.parent.getUnderlyingResource(); 173 } 174 175 public int hashCode() { 176 return Util.combineHashCodes(this.parent.hashCode(), this.nameStart); 177 } 178 179 public boolean isStructureKnown() throws JavaModelException { 180 return true; 181 } 182 183 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) { 184 buffer.append(this.tabString(tab)); 185 if (info != NO_INFO) { 186 buffer.append(Signature.toString(this.getTypeSignature())); 187 buffer.append(" "); } 189 toStringName(buffer); 190 } 191 } 192 | Popular Tags |