1 30 package com.tc.asm.tree; 31 32 import com.tc.asm.AnnotationVisitor; 33 import com.tc.asm.Attribute; 34 import com.tc.asm.ClassVisitor; 35 import com.tc.asm.MethodVisitor; 36 import com.tc.asm.Label; 37 import com.tc.asm.Opcodes; 38 import com.tc.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 List exceptions; 78 79 87 public Object annotationDefault; 88 89 96 public List [] visibleParameterAnnotations; 97 98 105 public List [] invisibleParameterAnnotations; 106 107 114 public List instructions; 115 116 122 public 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 localVariables.add(new LocalVariableNode(name, 322 desc, 323 signature, 324 start, 325 end, 326 index)); 327 } 328 329 public void visitLineNumber(final int line, final Label start) { 330 lineNumbers.add(new LineNumberNode(line, start)); 331 } 332 333 public void visitMaxs(final int maxStack, final int maxLocals) { 334 this.maxStack = maxStack; 335 this.maxLocals = maxLocals; 336 } 337 338 342 347 public void accept(final ClassVisitor cv) { 348 String [] exceptions = new String [this.exceptions.size()]; 349 this.exceptions.toArray(exceptions); 350 MethodVisitor mv = cv.visitMethod(access, 351 name, 352 desc, 353 signature, 354 exceptions); 355 if (mv != null) { 356 accept(mv); 357 } 358 } 359 360 365 public void accept(final MethodVisitor mv) { 366 int i, j, n; 368 if (annotationDefault != null) { 369 AnnotationVisitor av = mv.visitAnnotationDefault(); 370 AnnotationNode.accept(av, null, annotationDefault); 371 av.visitEnd(); 372 } 373 n = visibleAnnotations == null ? 0 : visibleAnnotations.size(); 374 for (i = 0; i < n; ++i) { 375 AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i); 376 an.accept(mv.visitAnnotation(an.desc, true)); 377 } 378 n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size(); 379 for (i = 0; i < n; ++i) { 380 AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i); 381 an.accept(mv.visitAnnotation(an.desc, false)); 382 } 383 n = visibleParameterAnnotations == null 384 ? 0 385 : visibleParameterAnnotations.length; 386 for (i = 0; i < n; ++i) { 387 List l = visibleParameterAnnotations[i]; 388 if (l == null) { 389 continue; 390 } 391 for (j = 0; j < l.size(); ++j) { 392 AnnotationNode an = (AnnotationNode) l.get(j); 393 an.accept(mv.visitParameterAnnotation(i, an.desc, true)); 394 } 395 } 396 n = invisibleParameterAnnotations == null 397 ? 0 398 : invisibleParameterAnnotations.length; 399 for (i = 0; i < n; ++i) { 400 List l = invisibleParameterAnnotations[i]; 401 if (l == null) { 402 continue; 403 } 404 for (j = 0; j < l.size(); ++j) { 405 AnnotationNode an = (AnnotationNode) l.get(j); 406 an.accept(mv.visitParameterAnnotation(i, an.desc, false)); 407 } 408 } 409 n = attrs == null ? 0 : attrs.size(); 410 for (i = 0; i < n; ++i) { 411 mv.visitAttribute((Attribute) attrs.get(i)); 412 } 413 if (instructions.size() > 0) { 415 mv.visitCode(); 416 for (i = 0; i < tryCatchBlocks.size(); ++i) { 418 ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv); 419 } 420 for (i = 0; i < instructions.size(); ++i) { 422 ((AbstractInsnNode) instructions.get(i)).accept(mv); 423 } 424 n = localVariables == null ? 0 : localVariables.size(); 426 for (i = 0; i < n; ++i) { 427 ((LocalVariableNode) localVariables.get(i)).accept(mv); 428 } 429 n = lineNumbers == null ? 0 : lineNumbers.size(); 431 for (i = 0; i < n; ++i) { 432 ((LineNumberNode) lineNumbers.get(i)).accept(mv); 433 } 434 mv.visitMaxs(maxStack, maxLocals); 436 } 437 mv.visitEnd(); 438 } 439 } 440 | Popular Tags |