1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import org.eclipse.jdt.core.dom.ASTNode; 14 import org.eclipse.jdt.core.dom.ASTVisitor; 15 import org.eclipse.jdt.core.dom.BodyDeclaration; 16 import org.eclipse.jdt.core.dom.IVariableBinding; 17 import org.eclipse.jdt.core.dom.Initializer; 18 import org.eclipse.jdt.core.dom.MethodDeclaration; 19 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 20 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 21 22 import org.eclipse.jdt.internal.corext.Assert; 23 24 public class LocalVariableIndex extends ASTVisitor { 25 26 private int fTopIndex; 27 28 36 public static int perform(BodyDeclaration declaration) { 37 Assert.isTrue(declaration != null); 38 switch (declaration.getNodeType()) { 39 case ASTNode.METHOD_DECLARATION: 40 return internalPerform((MethodDeclaration)declaration); 41 case ASTNode.INITIALIZER: 42 return internalPerform((Initializer)declaration); 43 default: 44 Assert.isTrue(false); 45 } 46 return -1; 47 } 48 49 private static int internalPerform(MethodDeclaration method) { 50 MethodDeclaration target= method; 53 while (ASTNodes.getParent(target, ASTNode.METHOD_DECLARATION) != null) { 54 target= (MethodDeclaration)ASTNodes.getParent(target, ASTNode.METHOD_DECLARATION); 55 } 56 return doPerform(target); 57 } 58 59 private static int internalPerform(Initializer initializer) { 60 return doPerform(initializer); 61 } 62 63 private static int doPerform(BodyDeclaration node) { 64 LocalVariableIndex counter= new LocalVariableIndex(); 65 node.accept(counter); 66 return counter.fTopIndex; 67 } 68 69 public boolean visit(SingleVariableDeclaration node) { 70 handleVariableBinding(node.resolveBinding()); 71 return true; 72 } 73 74 public boolean visit(VariableDeclarationFragment node) { 75 handleVariableBinding(node.resolveBinding()); 76 return true; 77 } 78 79 private void handleVariableBinding(IVariableBinding binding) { 80 if (binding == null) 81 return; 82 fTopIndex= Math.max(fTopIndex, binding.getVariableId()); 83 } 84 } 85 | Popular Tags |