1 18 package org.objectweb.speedo.generation.enhancer; 19 20 import org.objectweb.asm.CodeVisitor; 21 import org.objectweb.asm.Label; 22 import org.objectweb.asm.Attribute; 23 import org.objectweb.util.monolog.api.Logger; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 29 32 public class CodeDuplicator extends LoggedClass implements CodeVisitor { 33 34 37 private CodeVisitor[] cvs; 38 39 44 private Map [] labels; 45 46 public CodeDuplicator(Logger logger) { 47 super(logger); 48 } 49 50 55 public CodeDuplicator(List cvs) { 56 this.cvs = (CodeVisitor[]) cvs.toArray(new CodeVisitor[cvs.size()]); 57 this.labels = new Map [this.cvs.length]; 58 } 59 60 65 public CodeDuplicator(CodeVisitor[] cvs) { 66 this.cvs = cvs; 67 this.labels = new Map [cvs.length]; 68 } 69 70 73 public void visitInsn(final int opcode) { 74 for (int i = 0; i < cvs.length; ++i) { 75 cvs[i].visitInsn(opcode); 76 } 77 } 78 79 public void visitIntInsn(final int opcode, final int operand) { 80 for (int i = 0; i < cvs.length; ++i) { 81 cvs[i].visitIntInsn(opcode, operand); 82 } 83 } 84 85 public void visitVarInsn(final int opcode, final int var) { 86 for (int i = 0; i < cvs.length; ++i) { 87 cvs[i].visitVarInsn(opcode, var); 88 } 89 } 90 91 public void visitTypeInsn(final int opcode, final String desc) { 92 for (int i = 0; i < cvs.length; ++i) { 93 cvs[i].visitTypeInsn(opcode, desc); 94 } 95 } 96 97 public void visitFieldInsn(final int opcode, 98 final String owner, 99 final String name, 100 final String desc) { 101 for (int i = 0; i < cvs.length; ++i) { 102 cvs[i].visitFieldInsn(opcode, owner, name, desc); 103 } 104 } 105 106 public void visitMethodInsn(final int opcode, 107 final String owner, 108 final String name, 109 final String desc) { 110 for (int i = 0; i < cvs.length; ++i) { 111 cvs[i].visitMethodInsn(opcode, owner, name, desc); 112 } 113 } 114 115 public void visitJumpInsn(final int opcode, final Label label) { 116 for (int i = 0; i < cvs.length; ++i) { 117 cvs[i].visitJumpInsn(opcode, getLabel(label, i)); 118 } 119 } 120 121 public void visitLabel(final Label label) { 122 for (int i = 0; i < cvs.length; ++i) { 123 cvs[i].visitLabel(getLabel(label, i)); 124 } 125 } 126 127 public void visitLdcInsn(final Object cst) { 128 for (int i = 0; i < cvs.length; ++i) { 129 cvs[i].visitLdcInsn(cst); 130 } 131 } 132 133 public void visitIincInsn(final int var, final int increment) { 134 for (int i = 0; i < cvs.length; ++i) { 135 cvs[i].visitIincInsn(var, increment); 136 } 137 } 138 139 public void visitTableSwitchInsn(final int min, 140 final int max, 141 final Label dflt, 142 final Label labels[]) { 143 for (int i = 0; i < cvs.length; ++i) { 144 cvs[i].visitTableSwitchInsn(min, 145 max, getLabel(dflt, i), getLabels(labels, i)); 146 } 147 } 148 149 public void visitLookupSwitchInsn(final Label dflt, 150 final int keys[], 151 final Label labels[]) { 152 for (int i = 0; i < cvs.length; ++i) { 153 cvs[i].visitLookupSwitchInsn(getLabel(dflt, i), 154 keys, getLabels(labels, i)); 155 } 156 } 157 158 public void visitMultiANewArrayInsn(final String desc, final int dims) { 159 for (int i = 0; i < cvs.length; ++i) { 160 cvs[i].visitMultiANewArrayInsn(desc, dims); 161 } 162 } 163 164 public void visitTryCatchBlock(final Label start, 165 final Label end, 166 final Label handler, 167 final String type) { 168 for (int i = 0; i < cvs.length; ++i) { 169 cvs[i].visitTryCatchBlock(getLabel(start, i), 170 getLabel(end, i), getLabel(handler, i), type); 171 } 172 } 173 174 public void visitMaxs(final int maxStack, final int maxLocals) { 175 for (int i = 0; i < cvs.length; ++i) { 176 cvs[i].visitMaxs(maxStack, maxLocals); 177 } 178 } 179 180 public void visitLocalVariable(final String name, 181 final String desc, 182 final Label start, 183 final Label end, 184 final int index) { 185 for (int i = 0; i < cvs.length; ++i) { 186 cvs[i].visitLocalVariable(name, 187 desc, getLabel(start, i), getLabel(end, i), index); 188 } 189 } 190 191 public void visitLineNumber(final int line, final Label start) { 192 for (int i = 0; i < cvs.length; ++i) { 193 cvs[i].visitLineNumber(line, getLabel(start, i)); 194 } 195 } 196 197 public void visitAttribute(Attribute attribute) { 198 for (int i = 0; i < cvs.length; ++i) { 199 cvs[i].visitAttribute(attribute); 200 } 201 } 202 203 206 213 private Label getLabel(final Label label, final int i) { 214 if (labels[i] == null) { 215 labels[i] = new HashMap (); 216 } 217 Label l = (Label) labels[i].get(label); 218 if (l == null) { 219 l = new Label(); 220 labels[i].put(label, l); 221 } 222 return l; 223 } 224 225 232 private Label[] getLabels(final Label[] labels, final int i) { 233 Label[] l = new Label[labels.length]; 234 for (int j = 0; j < l.length; ++j) { 235 l[j] = getLabel(labels[j], i); 236 } 237 return l; 238 } 239 } 240 | Popular Tags |