KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > StringGetCharsAdapter


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.bytecode;
5
6 import com.tc.asm.ClassAdapter;
7 import com.tc.asm.ClassVisitor;
8 import com.tc.asm.MethodAdapter;
9 import com.tc.asm.MethodVisitor;
10 import com.tc.asm.Opcodes;
11
12 public class StringGetCharsAdapter extends ClassAdapter {
13
14   private final String JavaDoc[] includePatterns;
15
16   public StringGetCharsAdapter(ClassVisitor cv, String JavaDoc[] includePatterns) {
17     super(cv);
18     this.includePatterns = includePatterns;
19   }
20
21   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
22     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
23
24     for (int i = 0; i < includePatterns.length; i++) {
25       if (name.matches(includePatterns[i])) { return new RewriteStringGetChars(mv); }
26     }
27
28     return mv;
29   }
30
31   private static class RewriteStringGetChars extends MethodAdapter implements Opcodes {
32
33     public RewriteStringGetChars(MethodVisitor mv) {
34       super(mv);
35     }
36
37     public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
38       if ((INVOKEVIRTUAL == opcode)
39           && ("java/lang/String".equals(owner) && "getChars".equals(name) && ("(II[CI)V".equals(desc) || "([CI)V"
40               .equals(desc)))) {
41         super.visitMethodInsn(opcode, owner, "getCharsFast", desc);
42       } else {
43         super.visitMethodInsn(opcode, owner, name, desc);
44       }
45     }
46   }
47
48 }
49
Popular Tags