KickJava   Java API By Example, From Geeks To Geeks.

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


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.FieldDeclaration;
20 import org.eclipse.jdt.core.dom.TypeDeclaration;
21 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
22
23 /**
24  * Compute the name of field declared at a given position from an JDOM CompilationUnit.
25  */

26 public class BreakpointFieldLocator extends ASTVisitor {
27     
28     private int fPosition;
29     
30     private String JavaDoc fTypeName;
31     
32     private String JavaDoc fFieldName;
33
34     private boolean fFound;
35
36     /**
37      * Constructor
38      * @param position the position in the compilation unit.
39      */

40     public BreakpointFieldLocator(int position) {
41         fPosition= position;
42         fFound= false;
43     }
44
45     /**
46      * Return the name of the field declared at the given position.
47      * Return <code>null</code> if there is no field declaration at the given position.
48      */

49     public String JavaDoc getFieldName() {
50         return fFieldName;
51     }
52
53     /**
54      * Return the name of type in which the field is declared.
55      * Return <code>null</code> if there is no field declaration at the given position.
56      */

57     public String JavaDoc getTypeName() {
58         return fTypeName;
59     }
60     
61     private boolean containsPosition(ASTNode node) {
62         int startPosition= node.getStartPosition();
63         int endPosition = startPosition + node.getLength();
64         return startPosition <= fPosition && fPosition <= endPosition;
65     }
66
67     /**
68      * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.CompilationUnit)
69      */

70     public boolean visit(CompilationUnit node) {
71         // visit only the type declarations
72
List JavaDoc types = node.types();
73         for (Iterator JavaDoc iter = types.iterator(); iter.hasNext() && !fFound;) {
74             ((TypeDeclaration) iter.next()).accept(this);
75         }
76         return false;
77     }
78
79     /**
80      * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.FieldDeclaration)
81      */

82     public boolean visit(FieldDeclaration node) {
83         if (containsPosition(node)) {
84             // visit only the variable declaration fragments
85
List JavaDoc fragments = node.fragments();
86             if (fragments.size() == 1) {
87                 fFieldName= ((VariableDeclarationFragment)fragments.get(0)).getName().getIdentifier();
88                 fTypeName= ValidBreakpointLocationLocator.computeTypeName(node);
89                 fFound= true;
90                 return false;
91             }
92             for (Iterator JavaDoc iter = fragments.iterator(); iter.hasNext() && !fFound;) {
93                 ((VariableDeclarationFragment) iter.next()).accept(this);
94             }
95         }
96         return false;
97     }
98
99     /**
100      * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.TypeDeclaration)
101      */

102     public boolean visit(TypeDeclaration node) {
103         if (containsPosition(node)) {
104             // visit the field declarations
105
FieldDeclaration[] fields = node.getFields();
106             for (int i = 0, length = fields.length; i < length && !fFound; i++) {
107                 fields[i].accept(this);
108             }
109             if (!fFound) {
110                 // visit inner types
111
TypeDeclaration[] types = node.getTypes();
112                 for (int i = 0, length = types.length; i < length && !fFound; i++) {
113                     types[i].accept(this);
114                 }
115             }
116         }
117         return false;
118     }
119
120     /**
121      * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.VariableDeclarationFragment)
122      */

123     public boolean visit(VariableDeclarationFragment node) {
124         if (containsPosition(node)) {
125             fFieldName= node.getName().getIdentifier();
126             fTypeName= ValidBreakpointLocationLocator.computeTypeName(node);
127             fFound= true;
128         }
129         return false;
130     }
131
132 }
133
Popular Tags