KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > BreakpointMethodLocator


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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.debug.ui.actions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.ASTVisitor;
18 import org.eclipse.jdt.core.dom.CompilationUnit;
19 import org.eclipse.jdt.core.dom.MethodDeclaration;
20 import org.eclipse.jdt.core.dom.Modifier;
21 import org.eclipse.jdt.core.dom.PrimitiveType;
22 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
23 import org.eclipse.jdt.core.dom.Type;
24 import org.eclipse.jdt.core.dom.TypeDeclaration;
25
26 /**
27  * Compute the name of field declared at a given position from an JDOM CompilationUnit.
28  */

29 public class BreakpointMethodLocator extends ASTVisitor {
30     
31     private int fPosition;
32     
33     private String JavaDoc fTypeName;
34     
35     private String JavaDoc fMethodName;
36     
37     private String JavaDoc fMethodSignature;
38
39     private boolean fFound;
40
41     /**
42      * Constructor
43      * @param position the position in the compilation unit.
44      */

45     public BreakpointMethodLocator(int position) {
46         fPosition= position;
47         fFound= false;
48     }
49
50     /**
51      * Return the name of the method declared at the given position.
52      * Return <code>null</code> if there is no method declaration at the given position.
53      * .
54      */

55     public String JavaDoc getMethodName() {
56         return fMethodName;
57     }
58
59     /**
60      * Return the name of the method declared at the given position.
61      * Return <code>null</code> if there is no method declaration at the given position or
62      * if not possible to compute the signature of the method declared at the given
63      * position.
64      * @see BreakpointFieldLocator#getMethodName()
65      */

66     public String JavaDoc getMethodSignature() {
67         return fMethodSignature;
68     }
69
70     /**
71      * Return the name of type in which the method is declared.
72      * Return <code>null</code> if there is no method declaration at the given position.
73      * @see BreakpointFieldLocator#getMethodName()
74      */

75     public String JavaDoc getTypeName() {
76         return fTypeName;
77     }
78     
79     private boolean containsPosition(ASTNode node) {
80         int startPosition= node.getStartPosition();
81         int endPosition = startPosition + node.getLength();
82         return startPosition <= fPosition && fPosition <= endPosition;
83     }
84     
85     private String JavaDoc computeMethodSignature(MethodDeclaration node) {
86         if (node.getExtraDimensions() != 0 || Modifier.isAbstract(node.getModifiers())) {
87             return null;
88         }
89         StringBuffer JavaDoc signature= new StringBuffer JavaDoc();
90         signature.append('(');
91         List JavaDoc parameters = node.parameters();
92         for (Iterator JavaDoc iter = parameters.iterator(); iter.hasNext();) {
93             Type type = ((SingleVariableDeclaration) iter.next()).getType();
94             if (type instanceof PrimitiveType) {
95                 appendTypeLetter(signature, (PrimitiveType)type);
96             } else {
97                 return null;
98             }
99         }
100         signature.append(')');
101         Type returnType;
102         returnType= node.getReturnType2();
103         if (returnType instanceof PrimitiveType) {
104             appendTypeLetter(signature, (PrimitiveType)returnType);
105         } else {
106             return null;
107         }
108         return signature.toString();
109     }
110     
111     private void appendTypeLetter(StringBuffer JavaDoc signature, PrimitiveType type) {
112         PrimitiveType.Code code= type.getPrimitiveTypeCode();
113         if (code == PrimitiveType.BYTE) {
114             signature.append('B');
115         } else if (code == PrimitiveType.CHAR) {
116             signature.append('C');
117         } else if (code == PrimitiveType.DOUBLE) {
118             signature.append('D');
119         } else if (code == PrimitiveType.FLOAT) {
120             signature.append('F');
121         } else if (code == PrimitiveType.INT) {
122             signature.append('I');
123         } else if (code == PrimitiveType.LONG) {
124             signature.append('J');
125         } else if (code == PrimitiveType.SHORT) {
126             signature.append('S');
127         } else if (code == PrimitiveType.VOID) {
128             signature.append('V');
129         } else if (code == PrimitiveType.BOOLEAN) {
130             signature.append('Z');
131         }
132     }
133
134     /**
135      * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.CompilationUnit)
136      */

137     public boolean visit(CompilationUnit node) {
138         // visit only the type declarations
139
List JavaDoc types = node.types();
140         for (Iterator JavaDoc iter = types.iterator(); iter.hasNext() && !fFound;) {
141             ((TypeDeclaration) iter.next()).accept(this);
142         }
143         return false;
144     }
145
146     /**
147      * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.FieldDeclaration)
148      */

149     public boolean visit(MethodDeclaration node) {
150         if (containsPosition(node)) {
151             if (node.isConstructor()) {
152                 fMethodName= "<init>"; //$NON-NLS-1$
153
} else {
154                 fMethodName= node.getName().getIdentifier();
155             }
156             fMethodSignature= computeMethodSignature(node);
157             fTypeName= ValidBreakpointLocationLocator.computeTypeName(node);
158             fFound= true;
159         }
160         return false;
161     }
162
163     /**
164      * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.TypeDeclaration)
165      */

166     public boolean visit(TypeDeclaration node) {
167         if (containsPosition(node)) {
168             // visit the methode declarations
169
MethodDeclaration[] methods = node.getMethods();
170             for (int i = 0, length = methods.length; i < length && !fFound; i++) {
171                 methods[i].accept(this);
172             }
173             if (!fFound) {
174                 // visit inner types
175
TypeDeclaration[] types = node.getTypes();
176                 for (int i = 0, length = types.length; i < length && !fFound; i++) {
177                     types[i].accept(this);
178                 }
179             }
180         }
181         return false;
182     }
183
184 }
185
Popular Tags