1 26 package com.yworks.yguard.obf.classfile; 27 28 import java.io.*; 29 import java.util.*; 30 31 37 public class ConstantPool 38 { 39 41 42 private ClassFile myClassFile; 44 private Vector pool; 45 46 47 49 50 52 public ConstantPool(ClassFile classFile, CpInfo[] cpInfo) 53 { 54 myClassFile = classFile; 55 int length = cpInfo.length; 56 pool = new Vector(length); 57 pool.setSize(length); 58 for (int i = 0; i < length; i++) 59 { 60 pool.setElementAt(cpInfo[i], i); 61 } 62 } 63 64 65 public Enumeration elements() 66 { 67 return pool.elements(); 68 } 69 70 71 public int length() 72 { 73 return pool.size(); 74 } 75 76 77 public CpInfo getCpEntry(int i) 78 { 79 if (i < pool.size()) 80 { 81 return (CpInfo)pool.elementAt(i); 82 } 83 throw new IndexOutOfBoundsException ("Constant Pool index out of range."); 84 } 85 86 87 public void updateRefCount() 88 { 89 walkPool(new PoolAction() { 91 public void defaultAction(CpInfo cpInfo) {cpInfo.resetRefCount();} 92 }); 93 94 myClassFile.markUtf8Refs(this); 96 97 myClassFile.markNTRefs(this); 99 100 walkPool(new PoolAction() { 102 public void utf8Action(Utf8CpInfo cpInfo) {if (cpInfo.getRefCount() == 0) cpInfo.clearString();} 103 }); 104 } 105 106 107 public void incRefCount(int i) 108 { 109 CpInfo cpInfo = (CpInfo)pool.elementAt(i); 110 if (cpInfo == null) 111 { 112 } 115 else 116 { 117 cpInfo.incRefCount(); 118 } 119 } 120 121 122 public int remapUtf8To(String newString, int oldIndex) 123 { 124 decRefCount(oldIndex); 125 return addUtf8Entry(newString); 126 } 127 128 129 public void decRefCount(int i) 130 { 131 CpInfo cpInfo = (CpInfo)pool.elementAt(i); 132 if (cpInfo == null) 133 { 134 } 137 else 138 { 139 cpInfo.decRefCount(); 140 } 141 } 142 143 144 public int addEntry(CpInfo entry) 145 { 146 int oldLength = pool.size(); 147 pool.setSize(oldLength + 1); 148 pool.setElementAt(entry, oldLength); 149 return oldLength; 150 } 151 152 private int addUtf8Entry(String s) 154 { 155 for (int i = 0; i < pool.size(); i++) 157 { 158 Object o = pool.elementAt(i); 159 if (o instanceof Utf8CpInfo) 160 { 161 Utf8CpInfo entry = (Utf8CpInfo)o; 162 if (entry.getString().equals(s)) 163 { 164 entry.incRefCount(); 165 return i; 166 } 167 } 168 } 169 170 for (int i = 0; i < pool.size(); i++) 172 { 173 Object o = pool.elementAt(i); 174 if (o instanceof Utf8CpInfo) 175 { 176 Utf8CpInfo entry = (Utf8CpInfo)o; 177 if (entry.getRefCount() == 0) 178 { 179 entry.setString(s); 180 entry.incRefCount(); 181 return i; 182 } 183 } 184 } 185 186 return addEntry(new Utf8CpInfo(s)); 188 } 189 190 class PoolAction {public void utf8Action(Utf8CpInfo cpInfo) {defaultAction(cpInfo);} 192 public void defaultAction(CpInfo cpInfo) {}} 193 private void walkPool(PoolAction pa) 194 { 195 for (Enumeration enumeration = pool.elements(); enumeration.hasMoreElements(); ) 196 { 197 Object o = enumeration.nextElement(); 198 if (o instanceof Utf8CpInfo) 199 { 200 pa.utf8Action((Utf8CpInfo)o); 201 } 202 else if (o instanceof CpInfo) 203 { 204 pa.defaultAction((CpInfo)o); 205 } 206 } 207 } 208 } 209 | Popular Tags |