1 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 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 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 owner, 108 final String name, 109 final String 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 owner, 121 final String name, 122 final String 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 cst) { 149 if (cst instanceof Long || cst instanceof Double ) { 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 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 |