/** * Convert the object representation of the class into its class file format. * * @exception IOException error writing the class */ public ByteArray getFileFormat() throws IOException { int classFileSize = 4 + (10 * 2); classFileSize += cptEstimatedSize; if (interfaces != null) classFileSize += (interfaces.length * 2); if (field_info != null) classFileSize += field_info.classFileSize(); if (method_info != null) classFileSize += method_info.classFileSize(); if (attribute_info != null) classFileSize += attribute_info.classFileSize(); ClassFormatOutput cfo = new ClassFormatOutput(classFileSize + 200); put(cfo); return new ByteArray(cfo.getData(), 0, cfo.size()); }
private void put(ClassFormatOutput out) throws IOException { /* Write out the header */ out.putU4(VMDescriptor.JAVA_CLASS_FORMAT_MAGIC); out.putU2(minor_version); out.putU2(major_version); // special case checking that the number of constant // pool entries does not exceed the limit of 65535 // (as it is stored as a U2). // Special case to allow somewhat easier debugging // of the resulting failure. out.putU2("constant_pool", cptEntries.size()); cptPut(out); out.putU2(access_flags); out.putU2(this_class); out.putU2(super_class); if (interfaces != null) { int ilen = interfaces.length; out.putU2(ilen); for (int i = 0; i < ilen; i++) { out.putU2(interfaces[i]); } } else { out.putU2(0); } if (field_info != null) { out.putU2(field_info.size()); field_info.put(out); } else { out.putU2(0); } if (method_info != null) { out.putU2(method_info.size()); method_info.put(out); } else { out.putU2(0); } if (attribute_info != null) { out.putU2(attribute_info.size()); attribute_info.put(out); } else { out.putU2(0); } }