1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 import org.apache.bcel.Repository;
20 import org.apache.bcel.classfile.ClassParser;
21 import org.apache.bcel.classfile.JavaClass;
22 import org.apache.bcel.classfile.Method;
23 import org.apache.bcel.generic.ConstantPoolGen;
24 import org.apache.bcel.generic.MethodGen;
25 
26 /**
27  * Read class file(s) and examine all of its methods, determining the
28  * maximum stack depth used by analyzing control flow.
29  *
30  * @version $Id$
31  */
32 public final class maxstack {
33 
main(String[] argv)34     public static void main(String[] argv) throws Exception {
35         for (String class_name : argv) {
36             JavaClass java_class = Repository.lookupClass(class_name);
37 
38             if (java_class == null) {
39                 java_class = new ClassParser(class_name).parse();
40             }
41 
42             ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());
43 
44             for (Method m : java_class.getMethods()) {
45                 if (!(m.isAbstract() || m.isNative())) {
46                     MethodGen mg = new MethodGen(m, class_name, cp);
47 
48                     int compiled_stack = mg.getMaxStack();
49                     int compiled_locals = mg.getMaxLocals();
50                     mg.setMaxStack(); // Recompute value
51                     mg.setMaxLocals();
52                     int computed_stack = mg.getMaxStack();
53                     int computed_locals = mg.getMaxLocals();
54 
55                     mg.getInstructionList().dispose(); // Reuse instruction handles
56 
57                     System.out.println(m);
58 
59                     if (computed_stack == compiled_stack) {
60                         System.out.println("Stack ok(" + computed_stack + ")");
61                     } else {
62                         System.out.println("\nCompiled stack size " + compiled_stack + " computed size " + computed_stack);
63                     }
64 
65                     if (computed_locals == compiled_locals) {
66                         System.out.println("Locals ok(" + computed_locals + ")");
67                     } else {
68                         System.out.println("\nCompiled locals " + compiled_locals + " computed size " + computed_locals);
69                     }
70                 }
71             }
72         }
73     }
74 }
75