01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.bytecode;
05:
06: import com.tc.asm.ClassAdapter;
07: import com.tc.asm.ClassVisitor;
08: import com.tc.asm.Label;
09: import com.tc.asm.MethodAdapter;
10: import com.tc.asm.MethodVisitor;
11: import com.tc.asm.Opcodes;
12:
13: /**
14: * Class adaptor for Throwable that prints out the exception class at the
15: * beginning of the constructors.
16: * Ie.:
17: * sun.misc.MessageUtils.toStderr(this.getClass().getName());
18: */
19: public class JavaLangThrowableDebugClassAdapter extends ClassAdapter
20: implements Opcodes {
21:
22: public JavaLangThrowableDebugClassAdapter(ClassVisitor cv) {
23: super (cv);
24: }
25:
26: public MethodVisitor visitMethod(int access, String name,
27: String desc, String signature, String[] exceptions) {
28: MethodVisitor mv = super .visitMethod(access, name, desc,
29: signature, exceptions);
30: if ("<init>".equals(name)) {
31: mv = new DebugConstructorMethodVisitor(mv);
32: }
33:
34: return mv;
35: }
36:
37: private static class DebugConstructorMethodVisitor extends
38: MethodAdapter implements Opcodes {
39: public DebugConstructorMethodVisitor(MethodVisitor mv) {
40: super (mv);
41: }
42:
43: public void visitInsn(int opcode) {
44: if (RETURN == opcode) {
45: mv.visitVarInsn(ALOAD, 0);
46: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object",
47: "getClass", "()Ljava/lang/Class;");
48: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class",
49: "getName", "()Ljava/lang/String;");
50: mv.visitMethodInsn(INVOKESTATIC,
51: "sun/misc/MessageUtils", "toStderr",
52: "(Ljava/lang/String;)V");
53:
54: Label label_nomsg = new Label();
55: mv.visitVarInsn(ALOAD, 0);
56: mv.visitMethodInsn(INVOKEVIRTUAL,
57: "java/lang/Throwable", "getMessage",
58: "()Ljava/lang/String;");
59: mv.visitJumpInsn(IFNULL, label_nomsg);
60: mv.visitLdcInsn(": ");
61: mv.visitMethodInsn(INVOKESTATIC,
62: "sun/misc/MessageUtils", "toStderr",
63: "(Ljava/lang/String;)V");
64: mv.visitVarInsn(ALOAD, 0);
65: mv.visitMethodInsn(INVOKEVIRTUAL,
66: "java/lang/Throwable", "getMessage",
67: "()Ljava/lang/String;");
68: mv.visitMethodInsn(INVOKESTATIC,
69: "sun/misc/MessageUtils", "toStderr",
70: "(Ljava/lang/String;)V");
71:
72: mv.visitLabel(label_nomsg);
73: mv.visitLdcInsn("\n");
74: mv.visitMethodInsn(INVOKESTATIC,
75: "sun/misc/MessageUtils", "toStderr",
76: "(Ljava/lang/String;)V");
77: }
78:
79: super.visitInsn(opcode);
80: }
81: }
82: }
|