1 7 8 package org.gjt.jclasslib.structures; 9 10 import org.gjt.jclasslib.structures.constants.*; 11 12 19 20 public class ConstantPoolUtil { 21 22 private ConstantPoolUtil() { 23 } 24 25 36 public static int addConstantMethodrefInfo(ClassFile classFile, 37 String className, 38 String methodName, 39 String 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 64 public static int addConstantFieldrefInfo(ClassFile classFile, 65 String className, 66 String fieldName, 67 String 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 91 public static int addConstantNameAndTypeInfo(ClassFile classFile, 92 String name, 93 String 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 116 public static int addConstantClassInfo(ClassFile classFile, 117 String 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 138 public static int addConstantUTF8Info(ClassFile classFile, 139 String 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 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 |