1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
import java.io.*;
class InstallSDE {
static final boolean verbose = true;
static final String nameSDE = "SourceDebugExtension";
byte[] orig;
byte[] sdeAttr;
byte[] gen;
int origPos = 0;
int genPos = 0;
int sdeIndex;
public static void main(String[] args) throws IOException {
if (args.length == 2) {
install(new File(args[0]), new File(args[1]));
} else if (args.length == 3) {
install(new File(args[0]), new File(args[1]), new File(args[2]));
} else {
abort("Usage: <command> <input class file> " +
"<attribute file> <output class file name>\n" +
"<command> <input/output class file> <attribute file>");
}
}
static void install(File inClassFile, File attrFile, File outClassFile)
throws IOException {
new InstallSDE(inClassFile, attrFile, outClassFile);
}
static void install(File inOutClassFile, File attrFile) throws IOException {
File tmpFile = new File(inOutClassFile.getPath() + "tmp-out");
File tmpInOutClassFile = new File(inOutClassFile.getPath() + "tmp-in");
// Workaround delayed file deletes on Windows using a tmp file name
if (!inOutClassFile.renameTo(tmpInOutClassFile)) {
throw new IOException("tmp copy of inOutClassFile failed");
}
new InstallSDE(tmpInOutClassFile, attrFile, tmpFile);
if (!tmpInOutClassFile.delete()) {
throw new IOException("tmpInOutClassFile.delete() failed");
}
if (!tmpFile.renameTo(inOutClassFile)) {
throw new IOException("tmpFile.renameTo(inOutClassFile) failed");
}
}
static void abort(String msg) {
System.err.println(msg);
System.exit(1);
}
InstallSDE(File inClassFile, File attrFile, File outClassFile) throws IOException {
if (!inClassFile.exists()) {
abort("no such file: " + inClassFile);
}
if (!attrFile.exists()) {
abort("no such file: " + attrFile);
}
// get the bytes
orig = readWhole(inClassFile);
sdeAttr = readWhole(attrFile);
gen = new byte[orig.length + sdeAttr.length + 100];
// do it
addSDE();
// write result
FileOutputStream outStream = new FileOutputStream(outClassFile);
outStream.write(gen, 0, genPos);
outStream.close();
}
byte[] readWhole(File input) throws IOException {
FileInputStream inStream = new FileInputStream(input);
int len = (int)input.length();
byte[] bytes = new byte[len];
if (inStream.read(bytes, 0, len) != len) {
abort("expected size: " + len);
}
inStream.close();
return bytes;
}
void addSDE() throws UnsupportedEncodingException {
int i;
copy(4 + 2 + 2); // magic min/maj version
int constantPoolCountPos = genPos;
int constantPoolCount = readU2();
writeU2(constantPoolCount);
// copy old constant pool return index of SDE symbol, if found
sdeIndex = copyConstantPool(constantPoolCount);
if (sdeIndex < 0) {
// if "SourceDebugExtension" symbol not there add it
writeUtf8ForSDE();
// increment the countantPoolCount
sdeIndex = constantPoolCount;
++constantPoolCount;
randomAccessWriteU2(constantPoolCountPos, constantPoolCount);
if (verbose) {
System.out.println("SourceDebugExtension not found, installed at: " +
sdeIndex);
}
} else {
if (verbose) {
System.out.println("SourceDebugExtension found at: " +
sdeIndex);
}
}
copy(2 + 2 + 2); // access, this, super
int interfaceCount = readU2();
writeU2(interfaceCount);
if (verbose) {
System.out.println("interfaceCount: " + interfaceCount);
}
copy(interfaceCount * 2);
copyMembers(); // fields
copyMembers(); // methods
int attrCountPos = genPos;
int attrCount = readU2();
writeU2(attrCount);
if (verbose) {
System.out.println("class attrCount: " + attrCount);
}
// copy the class attributes, return true if SDE attr found (not copied)
if (!copyAttrs(attrCount)) {
// we will be adding SDE and it isn't already counted
++attrCount;
randomAccessWriteU2(attrCountPos, attrCount);
if (verbose) {
System.out.println("class attrCount incremented");
}
}
writeAttrForSDE(sdeIndex);
}
void copyMembers() {
int count = readU2();
writeU2(count);
if (verbose) {
System.out.println("members count: " + count);
}
for (int i = 0; i < count; ++i) {
copy(6); // access, name, descriptor
int attrCount = readU2();
writeU2(attrCount);
if (verbose) {
System.out.println("member attr count: " + attrCount);
}
copyAttrs(attrCount);
}
}
boolean copyAttrs(int attrCount) {
boolean sdeFound = false;
for (int i = 0; i < attrCount; ++i) {
int nameIndex = readU2();
// don't write old SDE
if (nameIndex == sdeIndex) {
sdeFound = true;
if (verbose) {
System.out.println("SDE attr found");
}
} else {
writeU2(nameIndex); // name
int len = readU4();
writeU4(len);
copy(len);
if (verbose) {
System.out.println("attr len: " + len);
}
}
}
return sdeFound;
}
void writeAttrForSDE(int index) {
writeU2(index);
writeU4(sdeAttr.length);
for (int i = 0; i < sdeAttr.length; ++i) {
writeU1(sdeAttr[i]);
}
}
void randomAccessWriteU2(int pos, int val) {
int savePos = genPos;
genPos = pos;
writeU2(val);
genPos = savePos;
}
int readU1() {
return ((int)orig[origPos++]) & 0xFF;
}
int readU2() {
int res = readU1();
return (res << 8) + readU1();
}
int readU4() {
int res = readU2();
return (res << 16) + readU2();
}
void writeU1(int val) {
gen[genPos++] = (byte)val;
}
void writeU2(int val) {
writeU1(val >> 8);
writeU1(val & 0xFF);
}
void writeU4(int val) {
writeU2(val >> 16);
writeU2(val & 0xFFFF);
}
void copy(int count) {
for (int i = 0; i < count; ++i) {
gen[genPos++] = orig[origPos++];
}
}
byte[] readBytes(int count) {
byte[] bytes = new byte[count];
for (int i = 0; i < count; ++i) {
bytes[i] = orig[origPos++];
}
return bytes;
}
void writeBytes(byte[] bytes) {
for (int i = 0; i < bytes.length; ++i) {
gen[genPos++] = bytes[i];
}
}
int copyConstantPool(int constantPoolCount) throws UnsupportedEncodingException {
int sdeIndex = -1;
// copy const pool index zero not in class file
for (int i = 1; i < constantPoolCount; ++i) {
int tag = readU1();
writeU1(tag);
switch (tag) {
case 7: // Class
case 8: // String
copy(2);
break;
case 9: // Field
case 10: // Method
case 11: // InterfaceMethod
case 3: // Integer
case 4: // Float
case 12: // NameAndType
case 18: // InvokeDynamic
copy(4);
break;
case 5: // Long
case 6: // Double
copy(8);
break;
case 15: // MethodHandle
copy(3);
break;
case 1: // Utf8
int len = readU2();
writeU2(len);
byte[] utf8 = readBytes(len);
String str = new String(utf8, "UTF-8");
if (verbose) {
System.out.println(i + " read class attr -- '" + str + "'");
}
if (str.equals(nameSDE)) {
sdeIndex = i;
}
writeBytes(utf8);
break;
default:
abort("unexpected tag: " + tag);
break;
}
}
return sdeIndex;
}
void writeUtf8ForSDE() {
int len = nameSDE.length();
writeU1(1); // Utf8 tag
writeU2(len);
for (int i = 0; i < len; ++i) {
writeU1(nameSDE.charAt(i));
}
}
}
|