Organized imports
[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 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;
26
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IProgressMonitor;
30
31
32 public class LocalVariable extends JavaElement implements ILocalVariable {
33
34         public int declarationSourceStart, declarationSourceEnd;
35         public int nameStart, nameEnd;
36         String typeSignature;
37         
38         public LocalVariable(
39                         JavaElement parent, 
40                         String name, 
41                         int declarationSourceStart, 
42                         int declarationSourceEnd,
43                         int nameStart, 
44                         int nameEnd,
45                         String typeSignature) {
46                 
47                 super(parent, name);
48                 this.declarationSourceStart = declarationSourceStart;
49                 this.declarationSourceEnd = declarationSourceEnd;
50                 this.nameStart = nameStart;
51                 this.nameEnd = nameEnd;
52                 this.typeSignature = typeSignature;
53         }
54
55         protected void closing(Object info) {
56                 // a local variable has no info
57         }
58
59         protected Object createElementInfo() {
60                 // a local variable has no info
61                 return null;
62         }
63
64         public boolean equals(Object o) {
65                 if (!(o instanceof LocalVariable)) return false;
66                 LocalVariable other = (LocalVariable)o;
67                 return 
68                         this.declarationSourceStart == other.declarationSourceStart 
69                         && this.declarationSourceEnd == other.declarationSourceEnd
70                         && this.nameStart == other.nameStart
71                         && this.nameEnd == other.nameEnd
72                         && super.equals(o);
73         }
74         
75         public boolean exists() {
76                 return this.parent.exists(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=46192
77         }
78
79         protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) {
80                 // a local variable has no info
81         }
82
83         public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) {
84                 switch (token.charAt(0)) {
85                         case JEM_COUNT:
86                                 return getHandleUpdatingCountFromMemento(memento, owner);
87                 }
88                 return this;
89         }
90
91         /*
92          * @see JavaElement#getHandleMemento()
93          */
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);
111                 }
112                 return buff.toString();
113         }
114
115         protected char getHandleMementoDelimiter() {
116                 return JavaElement.JEM_LOCALVARIABLE;
117         }
118
119         public IResource getCorrespondingResource() {
120                 return null;
121         }
122
123         public int getElementType() {
124                 return LOCAL_VARIABLE;
125         }
126
127         public ISourceRange getNameRange() {
128                 return new SourceRange(this.nameStart, this.nameEnd-this.nameStart+1);
129         }
130         
131         public IPath getPath() {
132                 return this.parent.getPath();
133         }
134
135         public IResource getResource() {
136                 return this.parent.getResource();
137         }
138
139         /**
140          * @see ISourceReference
141          */
142         public String getSource() throws JavaModelException {
143                 IOpenable openable = this.parent.getOpenableParent();
144                 IBuffer buffer = openable.getBuffer();
145                 if (buffer == null) {
146                         return null;
147                 }
148                 ISourceRange range = getSourceRange();
149                 int offset = range.getOffset();
150                 int length = range.getLength();
151                 if (offset == -1 || length == 0 ) {
152                         return null;
153                 }
154                 try {
155                         return buffer.getText(offset, length);
156                 } catch(RuntimeException e) {
157                         return null;
158                 }
159         }
160         
161         /**
162          * @see ISourceReference
163          */
164         public ISourceRange getSourceRange() {
165                 return new SourceRange(this.declarationSourceStart, this.declarationSourceEnd-this.declarationSourceStart+1);
166         }
167         
168         public String getTypeSignature() {
169                 return this.typeSignature;
170         }
171
172         public IResource getUnderlyingResource() throws JavaModelException {
173                 return this.parent.getUnderlyingResource();
174         }
175
176         public int hashCode() {
177                 return Util.combineHashCodes(this.parent.hashCode(), this.nameStart);
178         }
179         
180         public boolean isStructureKnown() throws JavaModelException {
181         return true;
182     }
183         
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$
189                 }
190                 toStringName(buffer);
191         }
192 }