001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core;
011:
012: import java.util.HashMap;
013:
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jdt.core.*;
018: import org.eclipse.jdt.core.IJavaElement;
019: import org.eclipse.jdt.core.JavaModelException;
020: import org.eclipse.jdt.core.WorkingCopyOwner;
021: import org.eclipse.jdt.internal.core.util.MementoTokenizer;
022: import org.eclipse.jdt.internal.core.util.Util;
023:
024: public class LocalVariable extends SourceRefElement implements
025: ILocalVariable {
026:
027: String name;
028: public int declarationSourceStart, declarationSourceEnd;
029: public int nameStart, nameEnd;
030: String typeSignature;
031:
032: public LocalVariable(JavaElement parent, String name,
033: int declarationSourceStart, int declarationSourceEnd,
034: int nameStart, int nameEnd, String typeSignature) {
035:
036: super (parent);
037: this .name = name;
038: this .declarationSourceStart = declarationSourceStart;
039: this .declarationSourceEnd = declarationSourceEnd;
040: this .nameStart = nameStart;
041: this .nameEnd = nameEnd;
042: this .typeSignature = typeSignature;
043: }
044:
045: protected void closing(Object info) {
046: // a local variable has no info
047: }
048:
049: protected Object createElementInfo() {
050: // a local variable has no info
051: return null;
052: }
053:
054: public boolean equals(Object o) {
055: if (!(o instanceof LocalVariable))
056: return false;
057: LocalVariable other = (LocalVariable) o;
058: return this .declarationSourceStart == other.declarationSourceStart
059: && this .declarationSourceEnd == other.declarationSourceEnd
060: && this .nameStart == other.nameStart
061: && this .nameEnd == other.nameEnd && super .equals(o);
062: }
063:
064: public boolean exists() {
065: return this .parent.exists(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=46192
066: }
067:
068: protected void generateInfos(Object info, HashMap newElements,
069: IProgressMonitor pm) {
070: // a local variable has no info
071: }
072:
073: public IJavaElement getHandleFromMemento(String token,
074: MementoTokenizer memento, WorkingCopyOwner owner) {
075: switch (token.charAt(0)) {
076: case JEM_COUNT:
077: return getHandleUpdatingCountFromMemento(memento, owner);
078: }
079: return this ;
080: }
081:
082: /*
083: * @see JavaElement#getHandleMemento(StringBuffer)
084: */
085: protected void getHandleMemento(StringBuffer buff) {
086: ((JavaElement) getParent()).getHandleMemento(buff);
087: buff.append(getHandleMementoDelimiter());
088: buff.append(this .name);
089: buff.append(JEM_COUNT);
090: buff.append(this .declarationSourceStart);
091: buff.append(JEM_COUNT);
092: buff.append(this .declarationSourceEnd);
093: buff.append(JEM_COUNT);
094: buff.append(this .nameStart);
095: buff.append(JEM_COUNT);
096: buff.append(this .nameEnd);
097: buff.append(JEM_COUNT);
098: buff.append(this .typeSignature);
099: if (this .occurrenceCount > 1) {
100: buff.append(JEM_COUNT);
101: buff.append(this .occurrenceCount);
102: }
103: }
104:
105: protected char getHandleMementoDelimiter() {
106: return JavaElement.JEM_LOCALVARIABLE;
107: }
108:
109: public IResource getCorrespondingResource() {
110: return null;
111: }
112:
113: public String getElementName() {
114: return this .name;
115: }
116:
117: public int getElementType() {
118: return LOCAL_VARIABLE;
119: }
120:
121: public ISourceRange getNameRange() {
122: return new SourceRange(this .nameStart, this .nameEnd
123: - this .nameStart + 1);
124: }
125:
126: public IPath getPath() {
127: return this .parent.getPath();
128: }
129:
130: public IResource getResource() {
131: return this .parent.getResource();
132: }
133:
134: /**
135: * @see ISourceReference
136: */
137: public String getSource() throws JavaModelException {
138: IOpenable openable = this .parent.getOpenableParent();
139: IBuffer buffer = openable.getBuffer();
140: if (buffer == null) {
141: return null;
142: }
143: ISourceRange range = getSourceRange();
144: int offset = range.getOffset();
145: int length = range.getLength();
146: if (offset == -1 || length == 0) {
147: return null;
148: }
149: try {
150: return buffer.getText(offset, length);
151: } catch (RuntimeException e) {
152: return null;
153: }
154: }
155:
156: /**
157: * @see ISourceReference
158: */
159: public ISourceRange getSourceRange() {
160: return new SourceRange(this .declarationSourceStart,
161: this .declarationSourceEnd - this .declarationSourceStart
162: + 1);
163: }
164:
165: public String getTypeSignature() {
166: return this .typeSignature;
167: }
168:
169: public IResource getUnderlyingResource() throws JavaModelException {
170: return this .parent.getUnderlyingResource();
171: }
172:
173: public int hashCode() {
174: return Util.combineHashCodes(this .parent.hashCode(),
175: this .nameStart);
176: }
177:
178: public boolean isStructureKnown() throws JavaModelException {
179: return true;
180: }
181:
182: protected void toStringInfo(int tab, StringBuffer buffer,
183: 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(" "); //$NON-NLS-1$
188: }
189: toStringName(buffer);
190: }
191: }
|