KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > convert > TransformReadField


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.convert;
17
18 import javassist.bytecode.*;
19 import javassist.ClassPool;
20 import javassist.CtClass;
21 import javassist.CtField;
22 import javassist.NotFoundException;
23 import javassist.Modifier;
24
25 public class TransformReadField extends Transformer {
26     protected String JavaDoc fieldname;
27     protected CtClass fieldClass;
28     protected boolean isPrivate;
29     protected String JavaDoc methodClassname, methodName;
30
31     public TransformReadField(Transformer next, CtField field,
32                               String JavaDoc methodClassname, String JavaDoc methodName)
33     {
34         super(next);
35         this.fieldClass = field.getDeclaringClass();
36         this.fieldname = field.getName();
37         this.methodClassname = methodClassname;
38         this.methodName = methodName;
39         this.isPrivate = Modifier.isPrivate(field.getModifiers());
40     }
41
42     static String JavaDoc isField(ClassPool pool, ConstPool cp, CtClass fclass,
43                           String JavaDoc fname, boolean is_private, int index) {
44         if (!cp.getFieldrefName(index).equals(fname))
45             return null;
46
47         try {
48             CtClass c = pool.get(cp.getFieldrefClassName(index));
49             if (is_private ? c == fclass : c.subclassOf(fclass))
50                 return cp.getFieldrefType(index);
51         }
52         catch (NotFoundException e) {}
53         return null;
54     }
55
56     public int transform(CtClass tclazz, int pos, CodeIterator iterator,
57                          ConstPool cp) throws BadBytecode
58     {
59         int c = iterator.byteAt(pos);
60         if (c == GETFIELD || c == GETSTATIC) {
61             int index = iterator.u16bitAt(pos + 1);
62             String JavaDoc typedesc = isField(tclazz.getClassPool(), cp,
63                                 fieldClass, fieldname, isPrivate, index);
64             if (typedesc != null) {
65                 if (c == GETSTATIC) {
66                     iterator.move(pos);
67                     iterator.insertGap(1); // insertGap() may insert 4 bytes.
68
iterator.writeByte(ACONST_NULL, pos);
69                     pos = iterator.next();
70                 }
71
72                 String JavaDoc type = "(Ljava/lang/Object;)" + typedesc;
73                 int mi = cp.addClassInfo(methodClassname);
74                 int methodref = cp.addMethodrefInfo(mi, methodName, type);
75                 iterator.writeByte(INVOKESTATIC, pos);
76                 iterator.write16bit(methodref, pos + 1);
77                 return pos;
78             }
79         }
80
81         return pos;
82     }
83 }
84
Popular Tags