KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > callhierarchy > CallLocation


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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

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 JavaDoc 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     /**
46      * @return IMethod
47      */

48     public IMember getCalledMember() {
49         return fCalledMember;
50     }
51
52     /**
53      *
54      */

55     public int getEnd() {
56         return fEnd;
57     }
58
59     public IMember getMember() {
60         return fMember;
61     }
62
63     /**
64      *
65      */

66     public int getStart() {
67         return fStart;
68     }
69
70     public int getLineNumber() {
71         initCallTextAndLineNumber();
72         return fLineNumber;
73     }
74     
75     public String JavaDoc 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) { //binary, without source attachment || buffer contents out of sync (bug 121900)
86
fCallText= ""; //$NON-NLS-1$
87
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     /**
104      * Returns the IBuffer for the IMember represented by this CallLocation.
105      *
106      * @return IBuffer for the IMember or null if the member doesn't have a buffer (for
107      * example if it is a binary file without source attachment).
108      */

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 JavaDoc toString() {
123         return getCallText();
124     }
125     
126     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
127         if (IJavaElement.class.isAssignableFrom(adapter)) {
128             return getMember();
129         }
130
131         return null;
132     }
133 }
134
Popular Tags