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;
31 public class LocalVariable extends JavaElement implements ILocalVariable {
33 public int declarationSourceStart, declarationSourceEnd;
35 public int nameStart, nameEnd;
39 public LocalVariable(JavaElement parent, String name,
40 int declarationSourceStart, int declarationSourceEnd,
41 int nameStart, int nameEnd, String typeSignature) {
44 this.declarationSourceStart = declarationSourceStart;
45 this.declarationSourceEnd = declarationSourceEnd;
46 this.nameStart = nameStart;
47 this.nameEnd = nameEnd;
48 this.typeSignature = typeSignature;
51 protected void closing(Object info) {
52 // a local variable has no info
55 protected Object createElementInfo() {
56 // a local variable has no info
60 public boolean equals(Object o) {
61 if (!(o instanceof LocalVariable))
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);
70 public boolean exists() {
71 return this.parent.exists(); // see
72 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=46192
75 protected void generateInfos(Object info, HashMap newElements,
76 IProgressMonitor pm) {
77 // a local variable has no info
80 public IJavaElement getHandleFromMemento(String token,
81 MementoTokenizer memento, WorkingCopyOwner owner) {
82 switch (token.charAt(0)) {
84 return getHandleUpdatingCountFromMemento(memento, owner);
90 * @see JavaElement#getHandleMemento()
92 public String getHandleMemento() {
93 StringBuffer buff = new StringBuffer(((JavaElement) getParent())
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);
111 return buff.toString();
114 protected char getHandleMementoDelimiter() {
115 return JavaElement.JEM_LOCALVARIABLE;
118 public IResource getCorrespondingResource() {
122 public int getElementType() {
123 return LOCAL_VARIABLE;
126 public ISourceRange getNameRange() {
127 return new SourceRange(this.nameStart, this.nameEnd - this.nameStart
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,
166 this.declarationSourceEnd - this.declarationSourceStart + 1);
169 public String getTypeSignature() {
170 return this.typeSignature;
173 public IResource getUnderlyingResource() throws JavaModelException {
174 return this.parent.getUnderlyingResource();
177 public int hashCode() {
178 return Util.combineHashCodes(this.parent.hashCode(), this.nameStart);
181 public boolean isStructureKnown() throws JavaModelException {
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$
191 toStringName(buffer);