Refactory: remove unused classes, imports, fields and methods.
[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 public class LocalVariable extends JavaElement implements ILocalVariable {
32
33         public int declarationSourceStart, declarationSourceEnd;
34
35         public int nameStart, nameEnd;
36
37         String typeSignature;
38
39         public LocalVariable(JavaElement parent, String name,
40                         int declarationSourceStart, int declarationSourceEnd,
41                         int nameStart, int nameEnd, String typeSignature) {
42
43                 super(parent, 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                 // a local variable has no info
53         }
54
55         protected Object createElementInfo() {
56                 // a local variable has no info
57                 return null;
58         }
59
60         public boolean equals(Object o) {
61                 if (!(o instanceof LocalVariable))
62                         return false;
63                 LocalVariable other = (LocalVariable) o;
64                 return this.declarationSourceStart == other.declarationSourceStart
65                                 && this.declarationSourceEnd == other.declarationSourceEnd
66                                 && this.nameStart == other.nameStart
67                                 && this.nameEnd == other.nameEnd && super.equals(o);
68         }
69
70         public boolean exists() {
71                 return this.parent.exists(); // see
72                                                                                 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=46192
73         }
74
75         protected void generateInfos(Object info, HashMap newElements,
76                         IProgressMonitor pm) {
77                 // a local variable has no info
78         }
79
80         public IJavaElement getHandleFromMemento(String token,
81                         MementoTokenizer memento, WorkingCopyOwner owner) {
82                 switch (token.charAt(0)) {
83                 case JEM_COUNT:
84                         return getHandleUpdatingCountFromMemento(memento, owner);
85                 }
86                 return this;
87         }
88
89         /*
90          * @see JavaElement#getHandleMemento()
91          */
92         public String getHandleMemento() {
93                 StringBuffer buff = new StringBuffer(((JavaElement) getParent())
94                                 .getHandleMemento());
95                 buff.append(getHandleMementoDelimiter());
96                 buff.append(this.name);
97                 buff.append(JEM_COUNT);
98                 buff.append(this.declarationSourceStart);
99                 buff.append(JEM_COUNT);
100                 buff.append(this.declarationSourceEnd);
101                 buff.append(JEM_COUNT);
102                 buff.append(this.nameStart);
103                 buff.append(JEM_COUNT);
104                 buff.append(this.nameEnd);
105                 buff.append(JEM_COUNT);
106                 buff.append(this.typeSignature);
107                 if (this.occurrenceCount > 1) {
108                         buff.append(JEM_COUNT);
109                         buff.append(this.occurrenceCount);
110                 }
111                 return buff.toString();
112         }
113
114         protected char getHandleMementoDelimiter() {
115                 return JavaElement.JEM_LOCALVARIABLE;
116         }
117
118         public IResource getCorrespondingResource() {
119                 return null;
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
128                                 + 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,
166                                 this.declarationSourceEnd - this.declarationSourceStart + 1);
167         }
168
169         public String getTypeSignature() {
170                 return this.typeSignature;
171         }
172
173         public IResource getUnderlyingResource() throws JavaModelException {
174                 return this.parent.getUnderlyingResource();
175         }
176
177         public int hashCode() {
178                 return Util.combineHashCodes(this.parent.hashCode(), this.nameStart);
179         }
180
181         public boolean isStructureKnown() throws JavaModelException {
182                 return true;
183         }
184
185         protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
186                 buffer.append(this.tabString(tab));
187                 if (info != NO_INFO) {
188                         buffer.append(Signature.toString(this.getTypeSignature()));
189                         buffer.append(" "); //$NON-NLS-1$
190                 }
191                 toStringName(buffer);
192         }
193 }