1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 import java.util.Iterator ; 14 import java.util.List ; 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 26 public class BreakpointFieldLocator extends ASTVisitor { 27 28 private int fPosition; 29 30 private String fTypeName; 31 32 private String fFieldName; 33 34 private boolean fFound; 35 36 40 public BreakpointFieldLocator(int position) { 41 fPosition= position; 42 fFound= false; 43 } 44 45 49 public String getFieldName() { 50 return fFieldName; 51 } 52 53 57 public String 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 70 public boolean visit(CompilationUnit node) { 71 List types = node.types(); 73 for (Iterator iter = types.iterator(); iter.hasNext() && !fFound;) { 74 ((TypeDeclaration) iter.next()).accept(this); 75 } 76 return false; 77 } 78 79 82 public boolean visit(FieldDeclaration node) { 83 if (containsPosition(node)) { 84 List 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 iter = fragments.iterator(); iter.hasNext() && !fFound;) { 93 ((VariableDeclarationFragment) iter.next()).accept(this); 94 } 95 } 96 return false; 97 } 98 99 102 public boolean visit(TypeDeclaration node) { 103 if (containsPosition(node)) { 104 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 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 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 |