KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > editor > VariableSizeUpdater


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.classfile.editor;
22
23 import proguard.classfile.util.*;
24 import proguard.classfile.attribute.visitor.*;
25 import proguard.classfile.attribute.*;
26 import proguard.classfile.instruction.visitor.InstructionVisitor;
27 import proguard.classfile.instruction.*;
28 import proguard.classfile.*;
29
30 /**
31  * This AttributeVisitor computes and updates the maximum local variable frame
32  * size of the code attributes that it visits.
33  *
34  * @author Eric Lafortune
35  */

36 public class VariableSizeUpdater
37 extends SimplifiedVisitor
38 implements AttributeVisitor,
39              InstructionVisitor
40 {
41     //*
42
private static final boolean DEBUG = false;
43     /*/
44     private static boolean DEBUG = true;
45     //*/

46
47
48     // Implementations for AttributeVisitor.
49

50     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
51
52
53     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
54     {
55 // DEBUG =
56
// clazz.getName().equals("abc/Def") &&
57
// method.getName(clazz).equals("abc");
58

59         // The minimum variable size is determined by the arguments.
60
codeAttribute.u2maxLocals = ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz));
61
62         // Non-static methods also have the 'this' variable.
63
if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) == 0)
64         {
65             codeAttribute.u2maxLocals++;
66         }
67
68         if (DEBUG)
69         {
70             System.out.println("VariableSizeUpdater: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
71             System.out.println("Max locals: "+codeAttribute.u2maxLocals+" <- parameters");
72         }
73
74         // Go over all instructions.
75
codeAttribute.instructionsAccept(clazz, method, this);
76     }
77
78
79     // Implementations for InstructionVisitor.
80

81     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
82
83
84     public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
85     {
86         int variableSize = variableInstruction.variableIndex + 1;
87         if (variableInstruction.isCategory2())
88         {
89             variableSize++;
90         }
91
92         if (codeAttribute.u2maxLocals < variableSize)
93         {
94             codeAttribute.u2maxLocals = variableSize;
95
96             if (DEBUG)
97             {
98                 System.out.println("Max locals: "+codeAttribute.u2maxLocals+" <- "+variableInstruction.toString(offset));
99             }
100         }
101     }
102 }
103
Popular Tags