1 12 package org.eclipse.jdt.internal.corext.callhierarchy; 13 14 import org.eclipse.core.runtime.IAdaptable; 15 16 import org.eclipse.jdt.core.IBuffer; 17 import org.eclipse.jdt.core.IJavaElement; 18 import org.eclipse.jdt.core.IMember; 19 import org.eclipse.jdt.core.IOpenable; 20 import org.eclipse.jdt.core.JavaModelException; 21 22 import org.eclipse.jface.text.BadLocationException; 23 import org.eclipse.jface.text.Document; 24 25 import org.eclipse.jdt.internal.ui.JavaPlugin; 26 27 public class CallLocation implements IAdaptable { 28 public static final int UNKNOWN_LINE_NUMBER= -1; 29 private IMember fMember; 30 private IMember fCalledMember; 31 private int fStart; 32 private int fEnd; 33 34 private String fCallText; 35 private int fLineNumber; 36 37 public CallLocation(IMember member, IMember calledMember, int start, int end, int lineNumber) { 38 this.fMember = member; 39 this.fCalledMember = calledMember; 40 this.fStart = start; 41 this.fEnd = end; 42 this.fLineNumber= lineNumber; 43 } 44 45 48 public IMember getCalledMember() { 49 return fCalledMember; 50 } 51 52 55 public int getEnd() { 56 return fEnd; 57 } 58 59 public IMember getMember() { 60 return fMember; 61 } 62 63 66 public int getStart() { 67 return fStart; 68 } 69 70 public int getLineNumber() { 71 initCallTextAndLineNumber(); 72 return fLineNumber; 73 } 74 75 public String getCallText() { 76 initCallTextAndLineNumber(); 77 return fCallText; 78 } 79 80 private void initCallTextAndLineNumber() { 81 if (fCallText != null) 82 return; 83 84 IBuffer buffer= getBufferForMember(); 85 if (buffer == null || buffer.getLength() < fEnd) { fCallText= ""; fLineNumber= UNKNOWN_LINE_NUMBER; 88 return; 89 } 90 91 fCallText= buffer.getText(fStart, (fEnd - fStart)); 92 93 if (fLineNumber == UNKNOWN_LINE_NUMBER) { 94 Document document= new Document(buffer.getContents()); 95 try { 96 fLineNumber= document.getLineOfOffset(fStart) + 1; 97 } catch (BadLocationException e) { 98 JavaPlugin.log(e); 99 } 100 } 101 } 102 103 109 private IBuffer getBufferForMember() { 110 IBuffer buffer = null; 111 try { 112 IOpenable openable = fMember.getOpenable(); 113 if (openable != null && fMember.exists()) { 114 buffer = openable.getBuffer(); 115 } 116 } catch (JavaModelException e) { 117 JavaPlugin.log(e); 118 } 119 return buffer; 120 } 121 122 public String toString() { 123 return getCallText(); 124 } 125 126 public Object getAdapter(Class adapter) { 127 if (IJavaElement.class.isAssignableFrom(adapter)) { 128 return getMember(); 129 } 130 131 return null; 132 } 133 } 134 | Popular Tags |