KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > LocalVariableIndex


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.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     /**
29      * Computes the maximum number of local variable declarations in the
30      * given body declaration.
31      *
32      * @param declaration the body declaration. Must either be a method
33      * declaration or an initializer.
34      * @return the maximum number of local variables
35      */

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         // we have to find the outermost method declaration since a local or anonymous
51
// type can reference final variables from the outer scope.
52
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