📄 javaclassparser.java
字号:
break;
case Constants.ATTRIBUTE_Code:
int max_stack = in.readUnsignedShort();
int max_locals = in.readUnsignedShort();
int code_length = in.readInt();
Attribute_Code.Opcode[] codes = null;
if (code_length != 0) {
byte[] bcode = new byte[code_length];
in.read(bcode);
codes = parseOpcodes(bcode);
}
int exception_table_length = in.readUnsignedShort();
Attribute_Code.ExceptionTableItem[] exceptionTable = null;
if (exception_table_length != 0) {
exceptionTable = new Attribute_Code.ExceptionTableItem[exception_table_length];
for (int counter = 0; counter < exception_table_length; counter++) {
exceptionTable[counter] = readExceptionTableItem(in);
}
}
int attributes_count = in.readUnsignedShort();
Attribute[] attributes = null;
if (attributes_count != 0) {
attributes = new Attribute[attributes_count];
for (int counter = 0; counter < attributes_count; counter++) {
attributes[counter] = readAttribute(in);
}
}
attribute = new Attribute_Code(attribute_length, max_stack, max_locals, code_length, codes, exception_table_length, exceptionTable,
attributes_count, attributes);
break;
case Constants.ATTRIBUTE_Exceptions:
int number_of_exceptions = in.readUnsignedShort();
int[] exception_index_table = null;
if (number_of_exceptions != 0) {
exception_index_table = new int[number_of_exceptions];
for (int counter = 0; counter < number_of_exceptions; counter++) {
exception_index_table[counter] = in.readUnsignedShort();
}
}
attribute = new Attribute_Exceptions(attribute_length, number_of_exceptions, exception_index_table);
break;
case Constants.ATTRIBUTE_InnerClasses:
int number_of_classes = in.readUnsignedShort();
Attribute_InnerClasses.InnerClass[] innerClasses = null;
if (number_of_classes != 0) {
innerClasses = new Attribute_InnerClasses.InnerClass[number_of_classes];
for (int counter = 0; counter < number_of_classes; counter++) {
innerClasses[counter] = readInnerClass(in);
}
}
attribute = new Attribute_InnerClasses(attribute_length, number_of_classes, innerClasses);
break;
case Constants.ATTRIBUTE_Synthetic:
attribute = new Attribute_Synthetic();
break;
case Constants.ATTRIBUTE_LineNumberTable:
int line_number_table_length = in.readUnsignedShort();
Attribute_LineNumberTable.LineNumber[] line_number_table = null;
if (line_number_table_length != 0) {
line_number_table = new Attribute_LineNumberTable.LineNumber[line_number_table_length];
for (int counter = 0; counter < line_number_table_length; counter++) {
line_number_table[counter] = readLineNumber(in);
}
}
attribute = new Attribute_LineNumberTable(attribute_length, line_number_table_length, line_number_table);
break;
case Constants.ATTRIBUTE_LocalVariableTable:
int local_variable_table_length = in.readUnsignedShort();
Attribute_LocalVariableTable.LocalVariable[] local_variable_table = null;
if (local_variable_table_length != 0) {
local_variable_table = new Attribute_LocalVariableTable.LocalVariable[local_variable_table_length];
for (int counter = 0; counter < local_variable_table_length; counter++) {
local_variable_table[counter] = readLocalVariable(in);
}
}
attribute = new Attribute_LocalVariableTable(attribute_length, local_variable_table_length, local_variable_table);
break;
case Constants.ATTRIBUTE_Deprecated:
attribute = new Attribute_Deprecated();
break;
}
} else {
byte[] info = new byte[attribute_length];
in.read(info);
attribute = new Attribute(attribute_name_index, attribute_length, info);
}
return attribute;
}
private Attribute_Code.ExceptionTableItem readExceptionTableItem(DataInputStream in) throws IOException {
return new Attribute_Code.ExceptionTableItem(in.readUnsignedShort(), in.readUnsignedShort(), in.readUnsignedShort(), in.readUnsignedShort());
}
private Attribute_InnerClasses.InnerClass readInnerClass(DataInputStream in) throws IOException {
return new Attribute_InnerClasses.InnerClass(in.readUnsignedShort(), in.readUnsignedShort(), in.readUnsignedShort(), in.readUnsignedShort());
}
private Attribute_LineNumberTable.LineNumber readLineNumber(DataInputStream in) throws IOException {
return new Attribute_LineNumberTable.LineNumber(in.readUnsignedShort(), in.readUnsignedShort());
}
private Attribute_LocalVariableTable.LocalVariable readLocalVariable(DataInputStream in) throws IOException {
return new Attribute_LocalVariableTable.LocalVariable(in.readUnsignedShort(), in.readUnsignedShort(), in.readUnsignedShort(), in
.readUnsignedShort(), in.readUnsignedShort());
}
private Attribute_Code.Opcode[] parseOpcodes(byte[] bytes) {
ArrayList ret = new ArrayList(bytes.length);
Attribute_Code.Opcode op;
OpcodeInfo opInfo;
int offset;
byte[][] operands = null;
boolean wide = false;
for (int i = 0; i < bytes.length; i++) {
offset = i;
opInfo = OpcodeHelper.OPCODES[0xFF & bytes[i]];
if (opInfo.operandsLength == null) {
operands = null;
} else {
if (opInfo.opcode == Constants.TABLESWITCH) {
int padnum = i % 4;
padnum = 3 - padnum;
i = i + padnum + 1;
// defualt value
byte[] defaultb = new byte[4];
for (int t = 0; t < 4; t++) {
defaultb[t] = bytes[i + t];
}
i = i + 4;
// low value
byte[] lowb = new byte[4];
for (int t = 0; t < 4; t++) {
lowb[t] = bytes[i + t];
}
i = i + 4;
// high byte
byte[] highb = new byte[4];
for (int t = 0; t < 4; t++) {
highb[t] = bytes[i + t];
}
i = i + 4;
int high = Util.getNum(highb);
int low = Util.getNum(lowb);
int total = high - low + 1 + 3 + 1; // number of jump offsets + one byte of opcode + high byte +low byte+defualt byte+padding byte
if (total < 0) {
total = 1;
}
operands = new byte[total][4];
operands[0] = new byte[padnum];
for (int ti = 0; ti < padnum; ti++) {
operands[0][ti] = (byte) 0;
}
operands[1] = defaultb;
operands[2] = lowb;
operands[3] = highb;
for (int t = 4; t < total; t++) {
operands[t][0] = bytes[i++];
operands[t][1] = bytes[i++];
operands[t][2] = bytes[i++];
operands[t][3] = bytes[i++];
}
i--;
} else if (opInfo.opcode == Constants.LOOKUPSWITCH) {
int padnum = i % 4;
padnum = 3 - padnum;
i = i + padnum + 1;
// defualt value
byte[] defaultb = new byte[4];
for (int t = 0; t < 4; t++) {
defaultb[t] = bytes[i + t];
}
i = i + 4;
// npair value
byte[] npairb = new byte[4];
for (int t = 0; t < 4; t++) {
npairb[t] = bytes[i + t];
}
i = i + 4;
int npair = Util.getNum(npairb);
int total = npair * 2 + 3; // npair *2 +defualt byte+one byte of opcode+padding bytes
operands = new byte[total][4];
operands[0] = new byte[padnum];
for (int ti = 0; ti < padnum; ti++) {
operands[0][ti] = (byte) 0;
}
operands[1] = defaultb;
operands[2] = npairb;
for (int t = 3; t < total; t++) {
operands[t][0] = bytes[i++];
operands[t][1] = bytes[i++];
operands[t][2] = bytes[i++];
operands[t][3] = bytes[i++];
}
i--;
} else if (opInfo.opcode == Constants.WIDE) {
wide = true;
} else if (wide == true) {
operands = new byte[opInfo.operandsLength.length][];
for (int j = 0; j < opInfo.operandsLength.length; j++) {
operands[j] = new byte[opInfo.operandsLength[j]];
for (int t = 0; t < opInfo.operandsLength[j] + 1; t++) {
operands[j][t] = bytes[++i];
}
}
wide = false;
} else {
operands = new byte[opInfo.operandsLength.length][];
for (int j = 0; j < opInfo.operandsLength.length; j++) {
operands[j] = new byte[opInfo.operandsLength[j]];
for (int t = 0; t < opInfo.operandsLength[j]; t++) {
operands[j][t] = bytes[++i];
}
}
}
}
op = new Attribute_Code.Opcode(offset, opInfo.opcode, operands);
ret.add(op);
}
return (Attribute_Code.Opcode[]) ret.toArray(new Attribute_Code.Opcode[0]);
}
private static void prt(Object s) {
// System.out.println(s);
} // class DataInputStream {
// java.io.DataInputStream in;
//
// public DataInputStream(FileInputStream ins) {
// in = new java.io.DataInputStream(ins);
// }
//
// public int readInt() throws IOException {
// int i = in.readInt();
// prt("int:" + i);
// return i;
// }
//
// public int readUnsignedShort() throws IOException {
// int i = in.readUnsignedShort();
// prt("sho:" + i);
// return i;
// }
//
// public byte readByte() throws IOException {
// byte b = in.readByte();
// prt("bye:" + b);
// return b;
// }
//
// public int read(byte[] b) throws IOException {
// int i = in.read(b);
// StringBuffer buf = new StringBuffer();
// for (int t = 0; t < i; t++) {
// buf.append(Integer.toString(b[t] & 0xFF) + ",");
// }
// prt(buf.toString());
// return i;
// }
//
// public float readFloat() throws IOException {
// float f = in.readFloat();
// prt("flo:" + f);
// return f;
//
// }
//
// public long readLong() throws IOException {
// long l = in.readLong();
// prt("long:" + l);
// return l;
// }
//
// public double readDouble() throws IOException {
// double d = in.readDouble();
// prt("dou:" + d);
// return d;
// }
//
// public String readUTF() throws IOException {
// String d = in.readUTF();
// prt("str:" + d);
// return d;
// }
//
// public void close() throws IOException {
// in.close();
// }
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -