1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.HashMap;
15 import net.sourceforge.phpdt.core.IBuffer;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.ILocalVariable;
18 import net.sourceforge.phpdt.core.IOpenable;
19 import net.sourceforge.phpdt.core.ISourceRange;
20 import net.sourceforge.phpdt.core.ISourceReference;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.core.Signature;
23 import net.sourceforge.phpdt.core.WorkingCopyOwner;
24 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
25 import net.sourceforge.phpdt.internal.core.util.Util;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IProgressMonitor;
32 public class LocalVariable extends JavaElement implements ILocalVariable {
34 public int declarationSourceStart, declarationSourceEnd;
35 public int nameStart, nameEnd;
41 int declarationSourceStart,
42 int declarationSourceEnd,
45 String typeSignature) {
48 this.declarationSourceStart = declarationSourceStart;
49 this.declarationSourceEnd = declarationSourceEnd;
50 this.nameStart = nameStart;
51 this.nameEnd = nameEnd;
52 this.typeSignature = typeSignature;
55 protected void closing(Object info) {
56 // a local variable has no info
59 protected Object createElementInfo() {
60 // a local variable has no info
64 public boolean equals(Object o) {
65 if (!(o instanceof LocalVariable)) return false;
66 LocalVariable other = (LocalVariable)o;
68 this.declarationSourceStart == other.declarationSourceStart
69 && this.declarationSourceEnd == other.declarationSourceEnd
70 && this.nameStart == other.nameStart
71 && this.nameEnd == other.nameEnd
75 public boolean exists() {
76 return this.parent.exists(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=46192
79 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) {
80 // a local variable has no info
83 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) {
84 switch (token.charAt(0)) {
86 return getHandleUpdatingCountFromMemento(memento, owner);
92 * @see JavaElement#getHandleMemento()
94 public String getHandleMemento(){
95 StringBuffer buff= new StringBuffer(((JavaElement)getParent()).getHandleMemento());
96 buff.append(getHandleMementoDelimiter());
97 buff.append(this.name);
98 buff.append(JEM_COUNT);
99 buff.append(this.declarationSourceStart);
100 buff.append(JEM_COUNT);
101 buff.append(this.declarationSourceEnd);
102 buff.append(JEM_COUNT);
103 buff.append(this.nameStart);
104 buff.append(JEM_COUNT);
105 buff.append(this.nameEnd);
106 buff.append(JEM_COUNT);
107 buff.append(this.typeSignature);
108 if (this.occurrenceCount > 1) {
109 buff.append(JEM_COUNT);
110 buff.append(this.occurrenceCount);
112 return buff.toString();
115 protected char getHandleMementoDelimiter() {
116 return JavaElement.JEM_LOCALVARIABLE;
119 public IResource getCorrespondingResource() {
123 public int getElementType() {
124 return LOCAL_VARIABLE;
127 public ISourceRange getNameRange() {
128 return new SourceRange(this.nameStart, this.nameEnd-this.nameStart+1);
131 public IPath getPath() {
132 return this.parent.getPath();
135 public IResource getResource() {
136 return this.parent.getResource();
140 * @see ISourceReference
142 public String getSource() throws JavaModelException {
143 IOpenable openable = this.parent.getOpenableParent();
144 IBuffer buffer = openable.getBuffer();
145 if (buffer == null) {
148 ISourceRange range = getSourceRange();
149 int offset = range.getOffset();
150 int length = range.getLength();
151 if (offset == -1 || length == 0 ) {
155 return buffer.getText(offset, length);
156 } catch(RuntimeException e) {
162 * @see ISourceReference
164 public ISourceRange getSourceRange() {
165 return new SourceRange(this.declarationSourceStart, this.declarationSourceEnd-this.declarationSourceStart+1);
168 public String getTypeSignature() {
169 return this.typeSignature;
172 public IResource getUnderlyingResource() throws JavaModelException {
173 return this.parent.getUnderlyingResource();
176 public int hashCode() {
177 return Util.combineHashCodes(this.parent.hashCode(), this.nameStart);
180 public boolean isStructureKnown() throws JavaModelException {
184 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
185 buffer.append(this.tabString(tab));
186 if (info != NO_INFO) {
187 buffer.append(Signature.toString(this.getTypeSignature()));
188 buffer.append(" "); //$NON-NLS-1$
190 toStringName(buffer);