1 30 package org.objectweb.asm.tree; 31 32 import org.objectweb.asm.AnnotationVisitor; 33 import org.objectweb.asm.Attribute; 34 import org.objectweb.asm.ClassVisitor; 35 import org.objectweb.asm.MethodVisitor; 36 import org.objectweb.asm.Label; 37 import org.objectweb.asm.Opcodes; 38 import org.objectweb.asm.Type; 39 40 import java.util.List ; 41 import java.util.ArrayList ; 42 import java.util.Arrays ; 43 44 49 public class MethodNode extends MemberNode implements MethodVisitor { 50 51 55 public int access; 56 57 60 public String name; 61 62 65 public String desc; 66 67 70 public String signature; 71 72 77 public final List exceptions; 78 79 87 public Object annotationDefault; 88 89 96 public List [] visibleParameterAnnotations; 97 98 105 public List [] invisibleParameterAnnotations; 106 107 114 public final List instructions; 115 116 122 public final List tryCatchBlocks; 123 124 127 public int maxStack; 128 129 132 public int maxLocals; 133 134 140 public List localVariables; 141 142 148 public List lineNumbers; 149 150 163 public MethodNode( 164 final int access, 165 final String name, 166 final String desc, 167 final String signature, 168 final String [] exceptions) 169 { 170 this.access = access; 171 this.name = name; 172 this.desc = desc; 173 this.signature = signature; 174 this.exceptions = new ArrayList (exceptions == null 175 ? 0 176 : exceptions.length); 177 boolean isAbstract = (access & Opcodes.ACC_ABSTRACT) != 0; 178 this.instructions = new ArrayList (isAbstract ? 0 : 24); 179 if (!isAbstract) { 180 this.localVariables = new ArrayList (5); 181 this.lineNumbers = new ArrayList (5); 182 } 183 this.tryCatchBlocks = new ArrayList (); 184 if (exceptions != null) { 185 this.exceptions.addAll(Arrays.asList(exceptions)); 186 } 187 } 188 189 193 public AnnotationVisitor visitAnnotationDefault() { 194 return new AnnotationNode(new ArrayList (0) { 195 public boolean add(Object o) { 196 annotationDefault = o; 197 return super.add(o); 198 } 199 }); 200 } 201 202 public AnnotationVisitor visitParameterAnnotation( 203 final int parameter, 204 final String desc, 205 final boolean visible) 206 { 207 AnnotationNode an = new AnnotationNode(desc); 208 if (visible) { 209 if (visibleParameterAnnotations == null) { 210 int params = Type.getArgumentTypes(this.desc).length; 211 visibleParameterAnnotations = new List [params]; 212 } 213 if (visibleParameterAnnotations[parameter] == null) { 214 visibleParameterAnnotations[parameter] = new ArrayList (1); 215 } 216 visibleParameterAnnotations[parameter].add(an); 217 } else { 218 if (invisibleParameterAnnotations == null) { 219 int params = Type.getArgumentTypes(this.desc).length; 220 invisibleParameterAnnotations = new List [params]; 221 } 222 if (invisibleParameterAnnotations[parameter] == null) { 223 invisibleParameterAnnotations[parameter] = new ArrayList (1); 224 } 225 invisibleParameterAnnotations[parameter].add(an); 226 } 227 return an; 228 } 229 230 public void visitCode() { 231 } 232 233 public void visitInsn(final int opcode) { 234 instructions.add(new InsnNode(opcode)); 235 } 236 237 public void visitIntInsn(final int opcode, final int operand) { 238 instructions.add(new IntInsnNode(opcode, operand)); 239 } 240 241 public void visitVarInsn(final int opcode, final int var) { 242 instructions.add(new VarInsnNode(opcode, var)); 243 } 244 245 public void visitTypeInsn(final int opcode, final String desc) { 246 instructions.add(new TypeInsnNode(opcode, desc)); 247 } 248 249 public void visitFieldInsn( 250 final int opcode, 251 final String owner, 252 final String name, 253 final String desc) 254 { 255 instructions.add(new FieldInsnNode(opcode, owner, name, desc)); 256 } 257 258 public void visitMethodInsn( 259 final int opcode, 260 final String owner, 261 final String name, 262 final String desc) 263 { 264 instructions.add(new MethodInsnNode(opcode, owner, name, desc)); 265 } 266 267 public void visitJumpInsn(final int opcode, final Label label) { 268 instructions.add(new JumpInsnNode(opcode, label)); 269 } 270 271 public void visitLabel(final Label label) { 272 instructions.add(new LabelNode(label)); 273 } 274 275 public void visitLdcInsn(final Object cst) { 276 instructions.add(new LdcInsnNode(cst)); 277 } 278 279 public void visitIincInsn(final int var, final int increment) { 280 instructions.add(new IincInsnNode(var, increment)); 281 } 282 283 public void visitTableSwitchInsn( 284 final int min, 285 final int max, 286 final Label dflt, 287 final Label[] labels) 288 { 289 instructions.add(new TableSwitchInsnNode(min, max, dflt, labels)); 290 } 291 292 public void visitLookupSwitchInsn( 293 final Label dflt, 294 final int[] keys, 295 final Label[] labels) 296 { 297 instructions.add(new LookupSwitchInsnNode(dflt, keys, labels)); 298 } 299 300 public void visitMultiANewArrayInsn(final String desc, final int dims) { 301 instructions.add(new MultiANewArrayInsnNode(desc, dims)); 302 } 303 304 public void visitTryCatchBlock( 305 final Label start, 306 final Label end, 307 final Label handler, 308 final String type) 309 { 310 tryCatchBlocks.add(new TryCatchBlockNode(start, end, handler, type)); 311 } 312 313 public void visitLocalVariable( 314 final String name, 315 final String desc, 316 final String signature, 317 final Label start, 318 final Label end, 319 final int index) 320 { 321 if (localVariables == null) { 322 localVariables = new ArrayList (1); 323 } 324 localVariables.add(new LocalVariableNode(name, 325 desc, 326 signature, 327 start, 328 end, 329 index)); 330 } 331 332 public void visitLineNumber(final int line, final Label start) { 333 if (lineNumbers == null) { 334 lineNumbers = new ArrayList (1); 335 } 336 lineNumbers.add(new LineNumberNode(line, start)); 337 } 338 339 public void visitMaxs(final int maxStack, final int maxLocals) { 340 this.maxStack = maxStack; 341 this.maxLocals = maxLocals; 342 } 343 344 348 353 public void accept(final ClassVisitor cv) { 354 String [] exceptions = new String [this.exceptions.size()]; 355 this.exceptions.toArray(exceptions); 356 MethodVisitor mv = cv.visitMethod(access, 357 name, 358 desc, 359 signature, 360 exceptions); 361 if (mv == null) { 362 return; 363 } 364 365 int i, j, n; 367 if (annotationDefault != null) { 368 AnnotationVisitor av = mv.visitAnnotationDefault(); 369 AnnotationNode.accept(av, null, annotationDefault); 370 av.visitEnd(); 371 } 372 n = visibleAnnotations == null ? 0 : visibleAnnotations.size(); 373 for (i = 0; i < n; ++i) { 374 AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i); 375 an.accept(mv.visitAnnotation(an.desc, true)); 376 } 377 n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size(); 378 for (i = 0; i < n; ++i) { 379 AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i); 380 an.accept(mv.visitAnnotation(an.desc, false)); 381 } 382 n = visibleParameterAnnotations == null 383 ? 0 384 : visibleParameterAnnotations.length; 385 for (i = 0; i < n; ++i) { 386 List l = visibleParameterAnnotations[i]; 387 if (l == null) { 388 continue; 389 } 390 for (j = 0; j < l.size(); ++j) { 391 AnnotationNode an = (AnnotationNode) l.get(j); 392 an.accept(mv.visitParameterAnnotation(i, an.desc, true)); 393 } 394 } 395 n = invisibleParameterAnnotations == null 396 ? 0 397 : invisibleParameterAnnotations.length; 398 for (i = 0; i < n; ++i) { 399 List l = invisibleParameterAnnotations[i]; 400 if (l == null) { 401 continue; 402 } 403 for (j = 0; j < l.size(); ++j) { 404 AnnotationNode an = (AnnotationNode) l.get(j); 405 an.accept(mv.visitParameterAnnotation(i, an.desc, false)); 406 } 407 } 408 n = attrs == null ? 0 : attrs.size(); 409 for (i = 0; i < n; ++i) { 410 mv.visitAttribute((Attribute) attrs.get(i)); 411 } 412 if (instructions.size() > 0) { 414 mv.visitCode(); 415 for (i = 0; i < instructions.size(); ++i) { 417 ((AbstractInsnNode) instructions.get(i)).accept(mv); 418 } 419 for (i = 0; i < tryCatchBlocks.size(); ++i) { 421 ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv); 422 } 423 n = localVariables == null ? 0 : localVariables.size(); 425 for (i = 0; i < n; ++i) { 426 ((LocalVariableNode) localVariables.get(i)).accept(mv); 427 } 428 n = lineNumbers == null ? 0 : lineNumbers.size(); 430 for (i = 0; i < n; ++i) { 431 ((LineNumberNode) lineNumbers.get(i)).accept(mv); 432 } 433 mv.visitMaxs(maxStack, maxLocals); 435 } 436 mv.visitEnd(); 437 } 438 } 439 | Popular Tags |