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