KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > LocalVariable


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jdt.core.*;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.core.WorkingCopyOwner;
22 import org.eclipse.jdt.internal.core.util.MementoTokenizer;
23 import org.eclipse.jdt.internal.core.util.Util;
24
25
26 public class LocalVariable extends SourceRefElement implements ILocalVariable {
27
28     String JavaDoc name;
29     public int declarationSourceStart, declarationSourceEnd;
30     public int nameStart, nameEnd;
31     String JavaDoc typeSignature;
32     
33     public LocalVariable(
34             JavaElement parent,
35             String JavaDoc name,
36             int declarationSourceStart,
37             int declarationSourceEnd,
38             int nameStart,
39             int nameEnd,
40             String JavaDoc typeSignature) {
41         
42         super(parent);
43         this.name = 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 JavaDoc info) {
52         // a local variable has no info
53
}
54
55     protected Object JavaDoc createElementInfo() {
56         // a local variable has no info
57
return null;
58     }
59
60     public boolean equals(Object JavaDoc o) {
61         if (!(o instanceof LocalVariable)) return false;
62         LocalVariable other = (LocalVariable)o;
63         return
64             this.declarationSourceStart == other.declarationSourceStart
65             && this.declarationSourceEnd == other.declarationSourceEnd
66             && this.nameStart == other.nameStart
67             && this.nameEnd == other.nameEnd
68             && super.equals(o);
69     }
70     
71     public boolean exists() {
72         return this.parent.exists(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=46192
73
}
74
75     protected void generateInfos(Object JavaDoc info, HashMap JavaDoc newElements, IProgressMonitor pm) {
76         // a local variable has no info
77
}
78
79     public IJavaElement getHandleFromMemento(String JavaDoc token, MementoTokenizer memento, WorkingCopyOwner owner) {
80         switch (token.charAt(0)) {
81             case JEM_COUNT:
82                 return getHandleUpdatingCountFromMemento(memento, owner);
83         }
84         return this;
85     }
86
87     /*
88      * @see JavaElement#getHandleMemento(StringBuffer)
89      */

90     protected void getHandleMemento(StringBuffer JavaDoc buff) {
91         ((JavaElement)getParent()).getHandleMemento(buff);
92         buff.append(getHandleMementoDelimiter());
93         buff.append(this.name);
94         buff.append(JEM_COUNT);
95         buff.append(this.declarationSourceStart);
96         buff.append(JEM_COUNT);
97         buff.append(this.declarationSourceEnd);
98         buff.append(JEM_COUNT);
99         buff.append(this.nameStart);
100         buff.append(JEM_COUNT);
101         buff.append(this.nameEnd);
102         buff.append(JEM_COUNT);
103         buff.append(this.typeSignature);
104         if (this.occurrenceCount > 1) {
105             buff.append(JEM_COUNT);
106             buff.append(this.occurrenceCount);
107         }
108     }
109
110     protected char getHandleMementoDelimiter() {
111         return JavaElement.JEM_LOCALVARIABLE;
112     }
113
114     public IResource getCorrespondingResource() {
115         return null;
116     }
117     
118     public String JavaDoc getElementName() {
119         return this.name;
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+1);
128     }
129     
130     public IPath getPath() {
131         return this.parent.getPath();
132     }
133
134     public IResource getResource() {
135         return this.parent.getResource();
136     }
137
138     /**
139      * @see ISourceReference
140      */

141     public String JavaDoc getSource() throws JavaModelException {
142         IOpenable openable = this.parent.getOpenableParent();
143         IBuffer buffer = openable.getBuffer();
144         if (buffer == null) {
145             return null;
146         }
147         ISourceRange range = getSourceRange();
148         int offset = range.getOffset();
149         int length = range.getLength();
150         if (offset == -1 || length == 0 ) {
151             return null;
152         }
153         try {
154             return buffer.getText(offset, length);
155         } catch(RuntimeException JavaDoc e) {
156             return null;
157         }
158     }
159     
160     /**
161      * @see ISourceReference
162      */

163     public ISourceRange getSourceRange() {
164         return new SourceRange(this.declarationSourceStart, this.declarationSourceEnd-this.declarationSourceStart+1);
165     }
166     
167     public String JavaDoc getTypeSignature() {
168         return this.typeSignature;
169     }
170
171     public IResource getUnderlyingResource() throws JavaModelException {
172         return this.parent.getUnderlyingResource();
173     }
174
175     public int hashCode() {
176         return Util.combineHashCodes(this.parent.hashCode(), this.nameStart);
177     }
178     
179     public boolean isStructureKnown() throws JavaModelException {
180         return true;
181     }
182     
183     protected void toStringInfo(int tab, StringBuffer JavaDoc buffer, Object JavaDoc 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 }
192
Popular Tags