Wrong format of JSR-45 related attribute - SourceDebugExtension
According to JSR-45 the format of SourceDebugExtension should be as follow:
SourceDebugExtension_attribute {
u2 attribute_name_index;
u4 attribute_length;
u1 debug_extension[attribute_length];
}
Note: in comment, the JSR creators wrote that format should be UTF-8, but
actually, it is not - the byte array should not include the string's length,
since there is an argument above (integer), which holds this information.
Since current version of ASM - 3.0RC1, in ClassWriter implementation of
visitSource method, writes the debug-info as a UTF-8 string (which will include
also the length), the string format become corrupted and JVM (tested on Sun JDK
1.4.2) doesn't recognize the 'smap' section.
Following is a possible solution for this problem. The bytes calculated by
standard String API, and inserted as a byte array into ByteVector:
public void visitSource (final String file, final String debug) {
if (file != null) {
sourceFile = newUTF8(file);
}
if (debug != null) {
sourceDebug = new ByteVector();
try {
byte[] arr = debug.getBytes("UTF-8");
sourceDebug.putByteArray(arr, 0, arr.length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//the old and wrong implementation
//sourceDebug.putUTF8(debug);
}
}
P.S. Just curios, why in ByteVector you've reimplemented UTF-8 encoding ? Why
standard API is not enough?