]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/core extension/org/eclipse/jdt/internal/corext/dom/LocalVariableIndex.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-before / core extension / org / eclipse / jdt / internal / corext / dom / LocalVariableIndex.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.dom;
12
13import org.eclipse.core.runtime.Assert;
14
15import org.eclipse.jdt.core.dom.ASTNode;
16import org.eclipse.jdt.core.dom.ASTVisitor;
17import org.eclipse.jdt.core.dom.BodyDeclaration;
18import org.eclipse.jdt.core.dom.FieldDeclaration;
19import org.eclipse.jdt.core.dom.IVariableBinding;
20import org.eclipse.jdt.core.dom.Initializer;
21import org.eclipse.jdt.core.dom.MethodDeclaration;
22import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
23import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
24
25
26public class LocalVariableIndex extends ASTVisitor {
27
28 private int fTopIndex;
29
30 /**
31 * Computes the maximum number of local variable declarations in the
32 * given body declaration.
33 *
34 * @param declaration the body declaration. Must either be a method
35 * declaration, or an initializer, or a field declaration.
36 * @return the maximum number of local variables
37 */
38 public static int perform(BodyDeclaration declaration) {
39 Assert.isTrue(declaration != null);
40 switch (declaration.getNodeType()) {
41 case ASTNode.METHOD_DECLARATION:
42 case ASTNode.FIELD_DECLARATION:
43 case ASTNode.INITIALIZER:
44 return internalPerform(declaration);
45 default:
46 throw new IllegalArgumentException(declaration.toString());
47 }
48 }
49
50 private static int internalPerform(BodyDeclaration methodOrInitializer) {
51 // we have to find the outermost method/initializer/field declaration since a local or anonymous
52 // type can reference final variables from the outer scope.
53 BodyDeclaration target= methodOrInitializer;
54 ASTNode parent= target.getParent();
55 while (parent != null) {
56 if (parent instanceof MethodDeclaration || parent instanceof Initializer || parent instanceof FieldDeclaration) {
57 target= (BodyDeclaration)parent;
58 }
59 parent= parent.getParent();
60 }
61
62 return doPerform(target);
63 }
64
65 private static int doPerform(BodyDeclaration node) {
66 LocalVariableIndex counter= new LocalVariableIndex();
67 node.accept(counter);
68 return counter.fTopIndex;
69 }
70
71 @Override
72 public boolean visit(SingleVariableDeclaration node) {
73 handleVariableBinding(node.resolveBinding());
74 return true;
75 }
76
77 @Override
78 public boolean visit(VariableDeclarationFragment node) {
79 handleVariableBinding(node.resolveBinding());
80 return true;
81 }
82
83 private void handleVariableBinding(IVariableBinding binding) {
84 if (binding == null)
85 return;
86 fTopIndex= Math.max(fTopIndex, binding.getVariableId());
87 }
88}