KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.tc.object.loaders;
6
7 import com.tc.asm.ClassAdapter;
8 import com.tc.asm.ClassVisitor;
9 import com.tc.asm.Label;
10 import com.tc.asm.MethodVisitor;
11 import com.tc.asm.Opcodes;
12 import com.tc.object.bytecode.ByteCodeUtil;
13 import com.tc.object.bytecode.ClassAdapterFactory;
14
15 /**
16  * Adds the NamedClassLoader interface (and required impl) to a loader implementation
17  */

18 public class NamedLoaderAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory {
19
20   private static final String JavaDoc LOADER_NAME_FIELD = ByteCodeUtil.TC_FIELD_PREFIX + "loaderName";
21   private String JavaDoc owner;
22
23   public NamedLoaderAdapter() {
24     super(null);
25   }
26
27   private NamedLoaderAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
28     super(cv);
29   }
30
31   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
32     return new NamedLoaderAdapter(visitor, loader);
33   }
34
35   public void visit(int version, int access, String JavaDoc name, String JavaDoc signature, String JavaDoc superName, String JavaDoc[] interfaces) {
36     interfaces = ByteCodeUtil.addInterfaces(interfaces, new String JavaDoc[] { NamedClassLoader.CLASS });
37     super.visit(version, access, name, signature, superName, interfaces);
38     this.owner = name;
39   }
40
41   public void visitEnd() {
42     super.visitField(ACC_SYNTHETIC | ACC_VOLATILE | ACC_TRANSIENT | ACC_PRIVATE, LOADER_NAME_FIELD,
43                      "Ljava/lang/String;", null, null);
44
45     MethodVisitor mv = super.visitMethod(ACC_PUBLIC | ACC_SYNTHETIC, "__tc_setClassLoaderName",
46                                          "(Ljava/lang/String;)V", null, null);
47     mv.visitVarInsn(ALOAD, 0);
48     mv.visitVarInsn(ALOAD, 1);
49     mv.visitFieldInsn(PUTFIELD, owner, LOADER_NAME_FIELD, "Ljava/lang/String;");
50     mv.visitInsn(RETURN);
51     mv.visitMaxs(0, 0);
52     mv.visitEnd();
53
54     mv = super.visitMethod(ACC_PUBLIC | ACC_SYNTHETIC, "__tc_getClassLoaderName", "()Ljava/lang/String;", null, null);
55     mv.visitCode();
56     mv.visitVarInsn(ALOAD, 0);
57     mv.visitFieldInsn(GETFIELD, owner, LOADER_NAME_FIELD, "Ljava/lang/String;");
58     Label l1 = new Label();
59     mv.visitJumpInsn(IFNONNULL, l1);
60     mv.visitTypeInsn(NEW, "java/lang/IllegalStateException");
61     mv.visitInsn(DUP);
62     mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
63     mv.visitInsn(DUP);
64     mv
65         .visitLdcInsn("Classloader name not set, instances defined from this loader not supported in Terracotta (loader: ");
66     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
67     mv.visitVarInsn(ALOAD, 0);
68     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
69     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
70     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
71                        "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
72     mv.visitLdcInsn(")");
73     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
74                        "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
75     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
76     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V");
77     mv.visitInsn(ATHROW);
78     mv.visitLabel(l1);
79     mv.visitVarInsn(ALOAD, 0);
80     mv.visitFieldInsn(GETFIELD, owner, LOADER_NAME_FIELD, "Ljava/lang/String;");
81     mv.visitInsn(ARETURN);
82     mv.visitMaxs(0, 0);
83     mv.visitEnd();
84
85     super.visitEnd();
86   }
87
88   String JavaDoc __tc_loaderName;
89
90   String JavaDoc get() {
91     if (__tc_loaderName == null) {
92       //
93
throw new IllegalStateException JavaDoc(
94                                       "Classloader name not set, instances defined from this loader not supported in Terracotta (loader: "
95                                           + getClass().getName() + ")");
96     }
97     return __tc_loaderName;
98   }
99
100 }
101
Popular Tags