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.loaders;
05:
06: import com.tc.asm.ClassAdapter;
07: import com.tc.asm.ClassVisitor;
08: import com.tc.asm.MethodVisitor;
09: import com.tc.asm.Opcodes;
10: import com.tc.object.bytecode.ByteCodeUtil;
11:
12: /**
13: * Adds NamedClassLoader interface to a class with a hard coded name (known at instrumentation time)
14: */
15: public class StandardClassLoaderAdapter extends ClassAdapter implements
16: Opcodes {
17:
18: private final String loaderName;
19:
20: public StandardClassLoaderAdapter(ClassVisitor cv, String loaderName) {
21: super (cv);
22: this .loaderName = loaderName;
23: }
24:
25: public void visit(int version, int access, String name,
26: String signature, String super Name, String[] interfaces) {
27: interfaces = ByteCodeUtil.addInterfaces(interfaces,
28: new String[] { ByteCodeUtil.NAMEDCLASSLOADER_CLASS });
29: super .visit(version, access, name, signature, super Name,
30: interfaces);
31: }
32:
33: public void visitEnd() {
34: addNamedClassLoaderMethods();
35: super .visitEnd();
36: }
37:
38: private void addNamedClassLoaderMethods() {
39: MethodVisitor mv = super .visitMethod(ACC_PUBLIC | ACC_FINAL
40: | ACC_SYNTHETIC, "__tc_getClassLoaderName",
41: "()Ljava/lang/String;", null, null);
42: mv.visitCode();
43: mv.visitLdcInsn(loaderName);
44: mv.visitInsn(ARETURN);
45: mv.visitMaxs(0, 0);
46: mv.visitEnd();
47:
48: mv = super .visitMethod(ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC,
49: "__tc_setClassLoaderName", "(Ljava/lang/String;)V",
50: null, null);
51: mv.visitCode();
52: mv.visitTypeInsn(NEW, "java/lang/AssertionError");
53: mv.visitInsn(DUP);
54: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/AssertionError",
55: "<init>", "()V");
56: mv.visitInsn(ATHROW);
57: mv.visitMaxs(0, 0);
58: mv.visitEnd();
59: }
60:
61: }
|