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.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 29 public class BreakpointMethodLocator extends ASTVisitor { 30 31 private int fPosition; 32 33 private String fTypeName; 34 35 private String fMethodName; 36 37 private String fMethodSignature; 38 39 private boolean fFound; 40 41 45 public BreakpointMethodLocator(int position) { 46 fPosition= position; 47 fFound= false; 48 } 49 50 55 public String getMethodName() { 56 return fMethodName; 57 } 58 59 66 public String getMethodSignature() { 67 return fMethodSignature; 68 } 69 70 75 public String 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 computeMethodSignature(MethodDeclaration node) { 86 if (node.getExtraDimensions() != 0 || Modifier.isAbstract(node.getModifiers())) { 87 return null; 88 } 89 StringBuffer signature= new StringBuffer (); 90 signature.append('('); 91 List parameters = node.parameters(); 92 for (Iterator 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 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 137 public boolean visit(CompilationUnit node) { 138 List types = node.types(); 140 for (Iterator iter = types.iterator(); iter.hasNext() && !fFound;) { 141 ((TypeDeclaration) iter.next()).accept(this); 142 } 143 return false; 144 } 145 146 149 public boolean visit(MethodDeclaration node) { 150 if (containsPosition(node)) { 151 if (node.isConstructor()) { 152 fMethodName= "<init>"; } 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 166 public boolean visit(TypeDeclaration node) { 167 if (containsPosition(node)) { 168 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 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 |