KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > loaders > StandardClassLoaderAdapter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.loaders;
5
6
7 import com.tc.asm.ClassAdapter;
8 import com.tc.asm.ClassVisitor;
9 import com.tc.asm.MethodVisitor;
10 import com.tc.asm.Opcodes;
11 import com.tc.object.bytecode.ByteCodeUtil;
12
13 /**
14  * Adds NamedClassLoader interface to a class with a hard coded name (known at instrumentation time)
15  */

16 public class StandardClassLoaderAdapter extends ClassAdapter implements Opcodes {
17
18   private final String JavaDoc loaderName;
19
20   public StandardClassLoaderAdapter(ClassVisitor cv, String JavaDoc loaderName) {
21     super(cv);
22     this.loaderName = loaderName;
23   }
24
25   public void visit(int version, int access, String JavaDoc name, String JavaDoc signature, String JavaDoc superName, String JavaDoc[] interfaces) {
26     interfaces = ByteCodeUtil.addInterfaces(interfaces, new String JavaDoc[] { NamedClassLoader.CLASS });
27     super.visit(version, access, name, signature, superName, interfaces);
28   }
29
30   public void visitEnd() {
31     addNamedClassLoaderMethods();
32     super.visitEnd();
33   }
34
35   private void addNamedClassLoaderMethods() {
36     MethodVisitor mv = super.visitMethod(ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, "__tc_getClassLoaderName",
37                                          "()Ljava/lang/String;", null, null);
38     mv.visitCode();
39     mv.visitLdcInsn(loaderName);
40     mv.visitInsn(ARETURN);
41     mv.visitMaxs(0, 0);
42     mv.visitEnd();
43
44     mv = super.visitMethod(ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, "__tc_setClassLoaderName", "(Ljava/lang/String;)V",
45                            null, null);
46     mv.visitCode();
47     mv.visitTypeInsn(NEW, "java/lang/AssertionError");
48     mv.visitInsn(DUP);
49     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/AssertionError", "<init>", "()V");
50     mv.visitInsn(ATHROW);
51     mv.visitMaxs(0, 0);
52     mv.visitEnd();
53   }
54
55 }
56
Popular Tags