KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > structures > ConstantPoolUtil


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.structures;
9
10 import org.gjt.jclasslib.structures.constants.*;
11
12 /**
13     Utility methods for working on the constant pool of a <tt>ClassFile</tt>
14     object.
15
16     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
17     @version $Revision: 1.6 $ $Date: 2003/08/18 07:52:54 $
18 */

19
20 public class ConstantPoolUtil {
21
22     private ConstantPoolUtil() {
23     }
24
25     /**
26         Add a <tt>ConstantMethodRef</tt> constant pool entry to the constant pool
27         of a <tt>ClassFile</tt>.
28         @param classFile the class file whose constant pool is to be edited
29         @param className the name of the referenced class
30         @param methodName the name of the referenced method
31         @param methodSignature the signature of the referenced method
32         @param sizeDelta the minimum increment by which the array holding the
33                          constant pool is to be enlarged. Set to 0 if unsure.
34         @return the constant pool index of the added <tt>ConstantMethodRef</tt>
35      */

36     public static int addConstantMethodrefInfo(ClassFile classFile,
37                                                String JavaDoc className,
38                                                String JavaDoc methodName,
39                                                String JavaDoc methodSignature,
40                                                int sizeDelta)
41     {
42         sizeDelta = Math.max(sizeDelta, 6);
43         int classIndex = addConstantClassInfo(classFile, className, sizeDelta);
44         int nameAndTypeIndex = addConstantNameAndTypeInfo(classFile, methodName, methodSignature, sizeDelta);
45
46         ConstantMethodrefInfo methodrefInfo = new ConstantMethodrefInfo();
47         methodrefInfo.setClassFile(classFile);
48         methodrefInfo.setClassIndex(classIndex);
49         methodrefInfo.setNameAndTypeIndex(nameAndTypeIndex);
50         return addConstantPoolEntry(classFile, methodrefInfo, sizeDelta);
51     }
52
53     /**
54         Add a <tt>ConstantFieldRef</tt> constant pool entry to the constant pool
55         of a <tt>ClassFile</tt>.
56         @param classFile the class file whose constant pool is to be edited
57         @param className the name of the referenced class
58         @param fieldName the name of the referenced field
59         @param fieldType the type of the referenced field
60         @param sizeDelta the minimum increment by which the array holding the
61                          constant pool is to be enlarged. Set to 0 if unsure.
62         @return the constant pool index of the added <tt>ConstantMethodRef</tt>
63      */

64     public static int addConstantFieldrefInfo(ClassFile classFile,
65                                               String JavaDoc className,
66                                               String JavaDoc fieldName,
67                                               String JavaDoc fieldType,
68                                               int sizeDelta)
69     {
70         sizeDelta = Math.max(sizeDelta, 6);
71         int classIndex = addConstantClassInfo(classFile, className, sizeDelta);
72         int nameAndTypeIndex = addConstantNameAndTypeInfo(classFile, fieldName, fieldType, sizeDelta);
73
74         ConstantFieldrefInfo fieldrefInfo = new ConstantFieldrefInfo();
75         fieldrefInfo.setClassFile(classFile);
76         fieldrefInfo.setClassIndex(classIndex);
77         fieldrefInfo.setNameAndTypeIndex(nameAndTypeIndex);
78         return addConstantPoolEntry(classFile, fieldrefInfo, sizeDelta);
79     }
80
81     /**
82         Add a <tt>ConstantNameAndTypeInfo</tt> constant pool entry to the
83         constant pool of a <tt>ClassFile</tt>.
84         @param classFile the class file whose constant pool is to be edited
85         @param name the name
86         @param descriptor the descriptor
87         @param sizeDelta the minimum increment by which the array holding the
88                          constant pool is to be enlarged. Set to 0 if unsure.
89         @return the constant pool index of the added <tt>ConstantNameAndTypeInfo</tt>
90      */

91     public static int addConstantNameAndTypeInfo(ClassFile classFile,
92                                                  String JavaDoc name,
93                                                  String JavaDoc descriptor,
94                                                  int sizeDelta)
95     {
96         sizeDelta = Math.max(sizeDelta, 3);
97         int nameIndex = addConstantUTF8Info(classFile, name, sizeDelta);
98         int descriptorIndex = addConstantUTF8Info(classFile, descriptor, sizeDelta);
99
100         ConstantNameAndTypeInfo nameAndTypeInfo = new ConstantNameAndTypeInfo();
101         nameAndTypeInfo.setClassFile(classFile);
102         nameAndTypeInfo.setNameIndex(nameIndex);
103         nameAndTypeInfo.setDescriptorIndex(descriptorIndex);
104         return addConstantPoolEntry(classFile, nameAndTypeInfo, sizeDelta);
105     }
106
107     /**
108         Add a <tt>ConstantClassInfo</tt> constant pool entry to the
109         constant pool of a <tt>ClassFile</tt>.
110         @param classFile the class file whose constant pool is to be edited
111         @param className the name of the referenced class
112         @param sizeDelta the minimum increment by which the array holding the
113                          constant pool is to be enlarged. Set to 0 if unsure.
114         @return the constant pool index of the added <tt>ConstantClassInfo</tt>
115      */

116     public static int addConstantClassInfo(ClassFile classFile,
117                                            String JavaDoc className,
118                                            int sizeDelta)
119     {
120         sizeDelta = Math.max(sizeDelta, 2);
121         int nameIndex = addConstantUTF8Info(classFile, className, sizeDelta);
122
123         ConstantClassInfo classInfo = new ConstantClassInfo();
124         classInfo.setClassFile(classFile);
125         classInfo.setNameIndex(nameIndex);
126         return addConstantPoolEntry(classFile, classInfo, sizeDelta);
127     }
128
129     /**
130         Add a <tt>ConstantUTF8Info</tt> constant pool entry to the
131         constant pool of a <tt>ClassFile</tt>.
132         @param classFile the class file whose constant pool is to be edited
133         @param string the string
134         @param sizeDelta the minimum increment by which the array holding the
135                          constant pool is to be enlarged. Set to 0 if unsure.
136         @return the constant pool index of the added <tt>ConstantUTF8Info</tt>
137      */

138     public static int addConstantUTF8Info(ClassFile classFile,
139                                           String JavaDoc string,
140                                           int sizeDelta)
141     {
142         ConstantUtf8Info utf8Info = new ConstantUtf8Info();
143         utf8Info.setClassFile(classFile);
144         utf8Info.setString(string);
145         return addConstantPoolEntry(classFile, utf8Info, sizeDelta);
146     }
147
148     /**
149         Add a constant pool entry to the
150         constant pool of a <tt>ClassFile</tt>.
151         @param classFile the class file whose constant pool is to be edited
152         @param newEntry the new constant pool entry
153         @param sizeDelta the minimum increment by which the array holding the
154                          constant pool is to be enlarged. Set to 0 if unsure.
155         @return the constant pool index of the added constant pool entry
156      */

157     public static int addConstantPoolEntry(ClassFile classFile,
158                                            CPInfo newEntry,
159                                            int sizeDelta)
160     {
161         CPInfo[] constantPool = classFile.getConstantPool();
162
163         int index = classFile.getConstantPoolIndex(newEntry);
164         if (index > -1) {
165             return index;
166         }
167
168         int lastFreeIndex;
169         for (lastFreeIndex = constantPool.length - 1;
170              lastFreeIndex >= 0 && constantPool[lastFreeIndex] == null;
171              lastFreeIndex--) {}
172         if (lastFreeIndex == constantPool.length - 1) {
173             CPInfo[] newConstantPool = new CPInfo[constantPool.length + Math.max(1, sizeDelta)];
174             System.arraycopy(constantPool, 0, newConstantPool, 0, constantPool.length);
175             classFile.enlargeConstantPool(newConstantPool);
176             constantPool = newConstantPool;
177         }
178         int newIndex = lastFreeIndex + 1;
179         constantPool[newIndex] = newEntry;
180         classFile.registerConstantPoolEntry(newIndex);
181         return newIndex;
182     }
183
184 }
185
Popular Tags