KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > jboss > transform > MainAdapter


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

5 package com.tc.jboss.transform;
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.asm.commons.LocalVariablesSorter;
12 import com.tc.object.bytecode.ClassAdapterFactory;
13
14 public class MainAdapter extends ClassAdapter implements ClassAdapterFactory {
15
16   public MainAdapter() {
17     super(null);
18   }
19
20   private MainAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
21     super(cv);
22   }
23
24   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
25     return new MainAdapter(visitor, loader);
26   }
27   
28   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
29     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
30     if (!"boot".equals(name)) {
31       return mv;
32     }
33     return new BootAdapter(access, desc, mv);
34   }
35
36   private static class BootAdapter extends LocalVariablesSorter implements Opcodes {
37
38     private int serverSlot;
39     private int configSlot;
40
41     public BootAdapter(int access, String JavaDoc desc, MethodVisitor mv) {
42       super(access, desc, mv);
43       serverSlot = newLocal(1);
44       configSlot = newLocal(1);
45     }
46
47     public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
48       super.visitMethodInsn(opcode, owner, name, desc);
49
50       if ((opcode == INVOKEVIRTUAL) && "org/jboss/system/server/ServerLoader".equals(owner) && "load".equals(name)) {
51         super.visitInsn(DUP);
52         super.visitVarInsn(ASTORE, serverSlot);
53       } else if ((opcode == INVOKEINTERFACE) && "org/jboss/system/server/Server".equals(owner) && "init".equals(name)) {
54         super.visitVarInsn(ALOAD, serverSlot);
55         super.visitMethodInsn(INVOKEINTERFACE, "org/jboss/system/server/Server", "getConfig",
56                               "()Lorg/jboss/system/server/ServerConfig;");
57         super.visitVarInsn(ASTORE, configSlot);
58         super.visitVarInsn(ALOAD, serverSlot);
59         super.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
60         super.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;");
61         super.visitVarInsn(ALOAD, configSlot);
62         super.visitMethodInsn(INVOKEINTERFACE, "org/jboss/system/server/ServerConfig", "getServerHomeDir",
63                               "()Ljava/io/File;");
64         super.visitVarInsn(ALOAD, configSlot);
65         super.visitMethodInsn(INVOKEINTERFACE, "org/jboss/system/server/ServerConfig", "getServerBaseDir",
66                               "()Ljava/io/File;");
67         super.visitVarInsn(ALOAD, configSlot);
68         super.visitMethodInsn(INVOKEINTERFACE, "org/jboss/system/server/ServerConfig", "getServerTempDir",
69                               "()Ljava/io/File;");
70         super.visitMethodInsn(INVOKESTATIC, "com/tc/jboss/JBossLoaderNaming", "initialize",
71                               "(Ljava/lang/ClassLoader;Ljava/io/File;Ljava/io/File;Ljava/io/File;)V");
72       }
73     }
74   }
75 }
76
Popular Tags