KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > commons > CodeSizeEvaluator


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.commons;
31
32 import org.objectweb.asm.Label;
33 import org.objectweb.asm.MethodAdapter;
34 import org.objectweb.asm.MethodVisitor;
35 import org.objectweb.asm.Opcodes;
36
37 /**
38  * A {@link MethodAdapter} that can be used to approximate method size.
39  *
40  * @author Eugene Kuleshov
41  */

42 public class CodeSizeEvaluator extends MethodAdapter implements Opcodes {
43
44     private int minSize;
45
46     private int maxSize;
47
48     public CodeSizeEvaluator(final MethodVisitor mv) {
49         super(mv);
50     }
51
52     public int getMinSize() {
53         return this.minSize;
54     }
55
56     public int getMaxSize() {
57         return this.maxSize;
58     }
59
60     public void visitInsn(final int opcode) {
61         minSize += 1;
62         maxSize += 1;
63         if (mv != null) {
64             mv.visitInsn(opcode);
65         }
66     }
67
68     public void visitIntInsn(final int opcode, final int operand) {
69         if (opcode == SIPUSH) {
70             minSize += 3;
71             maxSize += 3;
72         } else {
73             minSize += 2;
74             maxSize += 2;
75         }
76         if (mv != null) {
77             mv.visitIntInsn(opcode, operand);
78         }
79     }
80
81     public void visitVarInsn(final int opcode, final int var) {
82         if (var < 4 && opcode != Opcodes.RET) {
83             minSize += 1;
84             maxSize += 1;
85         } else if (var >= 256) {
86             minSize += 4;
87             maxSize += 4;
88         } else {
89             minSize += 2;
90             maxSize += 2;
91         }
92         if (mv != null) {
93             mv.visitVarInsn(opcode, var);
94         }
95     }
96
97     public void visitTypeInsn(final int opcode, final String JavaDoc desc) {
98         minSize += 3;
99         maxSize += 3;
100         if (mv != null) {
101             mv.visitTypeInsn(opcode, desc);
102         }
103     }
104
105     public void visitFieldInsn(
106         final int opcode,
107         final String JavaDoc owner,
108         final String JavaDoc name,
109         final String JavaDoc desc)
110     {
111         minSize += 3;
112         maxSize += 3;
113         if (mv != null) {
114             mv.visitFieldInsn(opcode, owner, name, desc);
115         }
116     }
117
118     public void visitMethodInsn(
119         final int opcode,
120         final String JavaDoc owner,
121         final String JavaDoc name,
122         final String JavaDoc desc)
123     {
124         if (opcode == INVOKEINTERFACE) {
125             minSize += 5;
126             maxSize += 5;
127         } else {
128             minSize += 3;
129             maxSize += 3;
130         }
131         if (mv != null) {
132             mv.visitMethodInsn(opcode, owner, name, desc);
133         }
134     }
135
136     public void visitJumpInsn(final int opcode, final Label label) {
137         minSize += 3;
138         if (opcode == GOTO || opcode == JSR) {
139             maxSize += 5;
140         } else {
141             maxSize += 8;
142         }
143         if (mv != null) {
144             mv.visitJumpInsn(opcode, label);
145         }
146     }
147
148     public void visitLdcInsn(final Object JavaDoc cst) {
149         if (cst instanceof Long JavaDoc || cst instanceof Double JavaDoc) {
150             minSize += 3;
151             maxSize += 3;
152         } else {
153             minSize += 2;
154             maxSize += 3;
155         }
156         if (mv != null) {
157             mv.visitLdcInsn(cst);
158         }
159     }
160
161     public void visitIincInsn(final int var, final int increment) {
162         if (var > 255 || increment > 127 || increment < -128) {
163             minSize += 6;
164             maxSize += 6;
165         } else {
166             minSize += 3;
167             maxSize += 3;
168         }
169         if (mv != null) {
170             mv.visitIincInsn(var, increment);
171         }
172     }
173
174     public void visitTableSwitchInsn(
175         final int min,
176         final int max,
177         final Label dflt,
178         final Label[] labels)
179     {
180         minSize += 13 + labels.length * 4;
181         maxSize += 16 + labels.length * 4;
182         if (mv != null) {
183             mv.visitTableSwitchInsn(min, max, dflt, labels);
184         }
185     }
186
187     public void visitLookupSwitchInsn(
188         final Label dflt,
189         final int[] keys,
190         final Label[] labels)
191     {
192         minSize += 9 + keys.length * 8;
193         maxSize += 12 + keys.length * 8;
194         if (mv != null) {
195             mv.visitLookupSwitchInsn(dflt, keys, labels);
196         }
197     }
198
199     public void visitMultiANewArrayInsn(final String JavaDoc desc, final int dims) {
200         minSize += 4;
201         maxSize += 4;
202         if (mv != null) {
203             mv.visitMultiANewArrayInsn(desc, dims);
204         }
205     }
206 }
207
Popular Tags