KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stat > StatClassTransformer


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package stat;
25
26 import org.objectweb.fractal.julia.asm.ClassTransformer;
27
28 import org.objectweb.asm.ClassAdapter;
29 import org.objectweb.asm.ClassVisitor;
30 import org.objectweb.asm.CodeVisitor;
31 import org.objectweb.asm.Attribute;
32 import org.objectweb.asm.CodeAdapter;
33 import org.objectweb.asm.Constants;
34
35 /**
36  * This class transformer inserts call to "getField" and "setField" methods
37  * before each read and write field access instructions in the class it visits.
38  */

39
40 public class StatClassTransformer extends ClassAdapter
41   implements ClassTransformer
42 {
43
44   private String JavaDoc className;
45
46   public StatClassTransformer () {
47     super(null);
48   }
49
50   public void setClassVisitor (final ClassVisitor cv) {
51     this.cv = cv;
52   }
53
54   public void visit (
55     final int version,
56     final int access,
57     final String JavaDoc name,
58     final String JavaDoc superName,
59     final String JavaDoc[] interfaces,
60     final String JavaDoc sourceFile)
61   {
62     cv.visit(version, access, name, superName, interfaces, sourceFile);
63     this.className = name;
64   }
65
66   public CodeVisitor visitMethod (
67     final int access,
68     final String JavaDoc name,
69     final String JavaDoc desc,
70     final String JavaDoc[] exceptions,
71     final Attribute attrs)
72   {
73     CodeVisitor v = cv.visitMethod(access, name, desc, exceptions, attrs);
74     if (v != null) {
75       v = new StatCodeTransformer(className, v);
76     }
77     return v;
78   }
79
80   private static class StatCodeTransformer extends CodeAdapter
81     implements Constants
82  {
83
84     private String JavaDoc className;
85
86     public StatCodeTransformer (final String JavaDoc className, final CodeVisitor c) {
87       super(c);
88       this.className = className;
89     }
90
91     public void visitFieldInsn (
92       final int opcode,
93       final String JavaDoc owner,
94       final String JavaDoc name,
95       final String JavaDoc desc)
96     {
97       if (opcode == GETFIELD && owner.equals(className)) {
98         cv.visitVarInsn(ALOAD, 0);
99         cv.visitLdcInsn(name);
100         cv.visitMethodInsn(
101           INVOKEVIRTUAL, className, "getField", "(Ljava/lang/String;)V");
102       } else if (opcode == PUTFIELD && owner.equals(className)) {
103         cv.visitVarInsn(ALOAD, 0);
104         cv.visitLdcInsn(name);
105         cv.visitMethodInsn(
106           INVOKEVIRTUAL, className, "setField", "(Ljava/lang/String;)V");
107       }
108       cv.visitFieldInsn(opcode, owner, name, desc);
109     }
110   }
111 }
112
Popular Tags