001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.modules.surefire_2_3;
006:
007: import com.tc.asm.ClassAdapter;
008: import com.tc.asm.ClassVisitor;
009: import com.tc.asm.Label;
010: import com.tc.asm.MethodAdapter;
011: import com.tc.asm.MethodVisitor;
012: import com.tc.asm.Opcodes;
013: import com.tc.asm.Type;
014: import com.tc.object.bytecode.ClassAdapterFactory;
015:
016: /**
017: * ClassLoader adapter for Surefire org.apache.maven.surefire.booter.IsolatedClassLoader
018: *
019: * @author Eugene Kuleshov
020: */
021: public class IsolatedClassLoaderAdapter extends ClassAdapter implements
022: ClassAdapterFactory, Opcodes {
023:
024: public IsolatedClassLoaderAdapter() {
025: super (null);
026: }
027:
028: public IsolatedClassLoaderAdapter(ClassVisitor cv) {
029: super (cv);
030: }
031:
032: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
033: return new IsolatedClassLoaderAdapter(visitor);
034: }
035:
036: public MethodVisitor visitMethod(int access, final String name,
037: final String desc, String signature, String[] exceptions) {
038: MethodVisitor mv = super .visitMethod(access, name, desc,
039: signature, exceptions);
040:
041: if (name.equals("<init>")) {
042: return new MethodAdapter(mv) {
043: public void visitInsn(int opcode) {
044: if (opcode == RETURN) {
045: if (Type.getArgumentTypes(desc).length > 0) {
046: // mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
047: // mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
048: // mv.visitInsn(DUP);
049: // mv.visitLdcInsn("### isolated class loader " + name + desc + " ");
050: // mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V");
051: // mv.visitVarInsn(ALOAD, 0);
052: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
053: // "(Ljava/lang/Object;)Ljava/lang/StringBuilder;");
054: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
055: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
056: //
057: // mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
058: // mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
059: // mv.visitInsn(DUP);
060: // mv.visitLdcInsn("### isolated class loader parent ");
061: // mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V");
062: // mv.visitVarInsn(ALOAD, 1);
063: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
064: // "(Ljava/lang/Object;)Ljava/lang/StringBuilder;");
065: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
066: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
067: //
068: // mv.visitTypeInsn(NEW, "java/lang/Throwable");
069: // mv.visitInsn(DUP);
070: // mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Throwable", "<init>", "()V");
071: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V");
072:
073: mv.visitVarInsn(ALOAD, 1);
074: Label l1 = new Label();
075: mv.visitJumpInsn(IFNONNULL, l1);
076:
077: mv.visitVarInsn(ALOAD, 0);
078: mv
079: .visitTypeInsn(CHECKCAST,
080: "com/tc/object/loaders/NamedClassLoader");
081: mv
082: .visitMethodInsn(
083: INVOKESTATIC,
084: "com/tc/object/bytecode/hook/impl/ClassProcessorHelper",
085: "registerGlobalLoader",
086: "(Lcom/tc/object/loaders/NamedClassLoader;)V");
087:
088: mv.visitLabel(l1);
089: }
090: }
091: super .visitInsn(opcode);
092: }
093: };
094: }
095:
096: return mv;
097: }
098:
099: public void visitEnd() {
100: MethodVisitor mv = cv.visitMethod(ACC_PUBLIC,
101: "__tc_getClassLoaderName", "()Ljava/lang/String;",
102: null, null);
103: mv.visitCode();
104: mv.visitLdcInsn("Maven.IsolatedClassLoader");
105: mv.visitInsn(ARETURN);
106: mv.visitMaxs(1, 1);
107: mv.visitEnd();
108:
109: super.visitEnd();
110: }
111:
112: }
|