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 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;
 
  26 public class LocalVariable extends JavaElement implements ILocalVariable {
 
  28         public int declarationSourceStart, declarationSourceEnd;
 
  29         public int nameStart, nameEnd;
 
  35                         int declarationSourceStart, 
 
  36                         int declarationSourceEnd,
 
  39                         String typeSignature) {
 
  42                 this.declarationSourceStart = declarationSourceStart;
 
  43                 this.declarationSourceEnd = declarationSourceEnd;
 
  44                 this.nameStart = nameStart;
 
  45                 this.nameEnd = nameEnd;
 
  46                 this.typeSignature = typeSignature;
 
  49         protected void closing(Object info) {
 
  50                 // a local variable has no info
 
  53         protected Object createElementInfo() {
 
  54                 // a local variable has no info
 
  58         public boolean equals(Object o) {
 
  59                 if (!(o instanceof LocalVariable)) return false;
 
  60                 LocalVariable other = (LocalVariable)o;
 
  62                         this.declarationSourceStart == other.declarationSourceStart 
 
  63                         && this.declarationSourceEnd == other.declarationSourceEnd
 
  64                         && this.nameStart == other.nameStart
 
  65                         && this.nameEnd == other.nameEnd
 
  69         public boolean exists() {
 
  70                 return this.parent.exists(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=46192
 
  73         protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) {
 
  74                 // a local variable has no info
 
  77         public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) {
 
  78                 switch (token.charAt(0)) {
 
  80                                 return getHandleUpdatingCountFromMemento(memento, owner);
 
  86          * @see JavaElement#getHandleMemento()
 
  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);
 
 106                 return buff.toString();
 
 109         protected char getHandleMementoDelimiter() {
 
 110                 return JavaElement.JEM_LOCALVARIABLE;
 
 113         public IResource getCorrespondingResource() {
 
 117         public int getElementType() {
 
 118                 return LOCAL_VARIABLE;
 
 121         public ISourceRange getNameRange() {
 
 122                 return new SourceRange(this.nameStart, this.nameEnd-this.nameStart+1);
 
 125         public IPath getPath() {
 
 126                 return this.parent.getPath();
 
 129         public IResource getResource() {
 
 130                 return this.parent.getResource();
 
 134          * @see ISourceReference
 
 136         public String getSource() throws JavaModelException {
 
 137                 IOpenable openable = this.parent.getOpenableParent();
 
 138                 IBuffer buffer = openable.getBuffer();
 
 139                 if (buffer == null) {
 
 142                 ISourceRange range = getSourceRange();
 
 143                 int offset = range.getOffset();
 
 144                 int length = range.getLength();
 
 145                 if (offset == -1 || length == 0 ) {
 
 149                         return buffer.getText(offset, length);
 
 150                 } catch(RuntimeException e) {
 
 156          * @see ISourceReference
 
 158         public ISourceRange getSourceRange() {
 
 159                 return new SourceRange(this.declarationSourceStart, this.declarationSourceEnd-this.declarationSourceStart+1);
 
 162         public String getTypeSignature() {
 
 163                 return this.typeSignature;
 
 166         public IResource getUnderlyingResource() throws JavaModelException {
 
 167                 return this.parent.getUnderlyingResource();
 
 170         public int hashCode() {
 
 171                 return Util.combineHashCodes(this.parent.hashCode(), this.nameStart);
 
 174         public boolean isStructureKnown() throws JavaModelException {
 
 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$
 
 184                 toStringName(buffer);