1 26 package net.sf.javaguard.classfile; 27 28 import java.io.*; 29 import net.sf.javaguard.Tools; 30 31 32 37 abstract public class ClassItemInfo implements ClassConstants { 38 39 private int accessFlags; 40 41 private int nameIndex; 42 43 private int descriptorIndex; 44 45 private int attributesLength; 46 47 private AttrInfo attributesInfo[]; 48 49 private ClassFile classFile; 50 51 private boolean synthetic = false; 52 53 54 55 56 59 protected ClassItemInfo(ClassFile cf) { 60 setClassFile(cf); 61 } 62 63 64 65 66 70 private void setClassFile(ClassFile cf) { 71 this.classFile = cf; 72 } 73 74 75 79 private ClassFile getClassFile() { 80 return classFile; 81 } 82 83 84 85 86 90 protected void setSynthetic(boolean isSynthetic) { 91 this.synthetic = isSynthetic; 92 } 93 94 95 98 public boolean isSynthetic() { 99 return synthetic; 100 } 101 102 103 104 105 109 protected void setNameIndex(int index) { 110 nameIndex = index; 111 } 112 113 114 118 protected int getNameIndex() { 119 return nameIndex; 120 } 121 122 123 124 125 129 protected void setDescriptorIndex(int index) { 130 descriptorIndex = index; 131 } 132 133 134 138 protected int getDescriptorIndex() { 139 return descriptorIndex; 140 } 141 142 143 144 145 149 protected void setAttributesLength(int len) { 150 attributesLength = len; 151 } 152 153 154 158 protected int getAttributesLength() { 159 return attributesLength; 160 } 161 162 163 164 165 169 protected void setAttributes(AttrInfo[] attributes) { 170 attributesInfo = attributes; 171 } 172 173 174 178 protected AttrInfo[] getAttributes() { 179 return attributesInfo; 180 } 181 182 183 184 185 192 protected void setAttribute(int index, AttrInfo attr) { 193 attributesInfo[index] = attr; 194 } 195 196 197 203 protected AttrInfo getAttribute(int pos) { 204 return attributesInfo[pos]; 205 } 206 207 208 209 210 213 public String getName() { 214 return ((Utf8CpInfo) getClassFile().getCpEntry(getNameIndex())).getString(); 215 } 216 217 218 221 public String getDescriptor() { 222 return ((Utf8CpInfo) getClassFile().getCpEntry(getDescriptorIndex())).getString(); 223 } 224 225 226 227 228 232 protected void setAccessFlags(int flags) { 233 accessFlags = flags; 234 } 235 236 237 241 public int getAccessFlags() { 242 return accessFlags; 243 } 244 245 246 247 248 252 protected void trimAttrsExcept(String [] keepAttrs) { 253 for (int i = 0; i < getAttributesLength(); i++) { 255 if (Tools.isInArray(getAttribute(i).getAttrName(), keepAttrs)) { 256 getAttribute(i).trimAttrsExcept(keepAttrs); 257 } else { 258 setAttribute(i, null); 259 } 260 } 261 262 AttrInfo[] left = new AttrInfo[getAttributesLength()]; 264 int j = 0; 265 for (int i = 0; i < getAttributesLength(); i++) { 266 if (null != getAttribute(i)) { 267 left[j++] = getAttribute(i); 268 } 269 } 270 AttrInfo[] attributes = new AttrInfo[j]; 271 System.arraycopy(left, 0, attributes, 0, j); 272 setAttributesLength(j); 273 setAttributes(attributes); 274 } 275 276 277 278 279 282 protected void markUtf8Refs(ConstantPool pool) { 283 pool.incRefCount(getNameIndex()); 284 pool.incRefCount(getDescriptorIndex()); 285 for (int i = 0; i < getAttributesLength(); i++) { 286 getAttribute(i).markUtf8Refs(pool); 287 } 288 } 289 290 291 292 293 297 protected void read(DataInput din) 298 throws IOException { 299 setAccessFlags(din.readUnsignedShort()); 300 setNameIndex(din.readUnsignedShort()); 301 setDescriptorIndex(din.readUnsignedShort()); 302 setAttributesLength(din.readUnsignedShort()); 303 AttrInfo[] attrs = new AttrInfo[getAttributesLength()]; 304 for (int i = 0; i < getAttributesLength(); i++) { 305 attrs[i] = AttrInfo.create(din, getClassFile()); 306 if (attrs[i].getAttrName().equals(ATTR_Synthetic)) { 307 setSynthetic(true); 308 } 309 } 310 setAttributes(attrs); 311 } 312 313 314 318 public void write(DataOutput dout) 319 throws IOException { 320 if (null == dout) throw new IOException("No output stream was provided."); 321 dout.writeShort(getAccessFlags()); 322 dout.writeShort(getNameIndex()); 323 dout.writeShort(getDescriptorIndex()); 324 dout.writeShort(getAttributesLength()); 325 for (int i = 0; i < getAttributesLength(); i++) { 326 getAttribute(i).write(dout); 327 } 328 } 329 } 330 | Popular Tags |