|
| 1 | +package ch20.unitTest; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | + |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.DataInputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author: yuki |
| 13 | + * @date: 2018/11/2 |
| 14 | + */ |
| 15 | +public class ClassNameFinder { |
| 16 | + |
| 17 | + public static String thisClass(byte[] classBytes) { |
| 18 | + |
| 19 | + Map<Integer, Integer> offsetTable = new HashMap<>(); |
| 20 | + Map<Integer, String> classNameTable = new HashMap<>(); |
| 21 | + try { |
| 22 | + //解析字节流 |
| 23 | + DataInputStream data = new DataInputStream( |
| 24 | + new ByteArrayInputStream(classBytes) |
| 25 | + ); |
| 26 | + // 0xcafebabe |
| 27 | + int magic = data.readInt(); |
| 28 | + int minorVersion = data.readShort(); |
| 29 | + int majorVersion = data.readShort(); |
| 30 | + |
| 31 | + int constant_pool_count = data.readShort(); |
| 32 | + int[] constant_pool = new int[constant_pool_count]; |
| 33 | + for (int i = 1; i < constant_pool_count; i++) { |
| 34 | + int tag = data.read(); |
| 35 | + int tableSize; |
| 36 | + //JVM Page 169 |
| 37 | + switch (tag) { |
| 38 | + case 1: |
| 39 | + //UTF 编码的字符串 |
| 40 | + int length = data.readShort(); |
| 41 | + char[] bytes = new char[length]; |
| 42 | + for (int k = 0; k < bytes.length; k++) { |
| 43 | + bytes[k] = (char) data.read(); |
| 44 | + } |
| 45 | + String className = new String(bytes); |
| 46 | + classNameTable.put(i, className); |
| 47 | + break; |
| 48 | + case 5: |
| 49 | + //long |
| 50 | + case 6: |
| 51 | + //double |
| 52 | + data.readLong(); |
| 53 | + i++; |
| 54 | + break; |
| 55 | + case 7: |
| 56 | + //class |
| 57 | + int offset = data.readShort(); |
| 58 | + offsetTable.put(i, offset); |
| 59 | + break; |
| 60 | + case 8: |
| 61 | + //String 字符串类型字面量 |
| 62 | + data.readShort(); |
| 63 | + break; |
| 64 | + case 3: |
| 65 | + //integer |
| 66 | + case 4: |
| 67 | + //float |
| 68 | + case 9: |
| 69 | + //field ref |
| 70 | + case 10: |
| 71 | + //method ref |
| 72 | + case 11: |
| 73 | + //interface method ref |
| 74 | + case 12: |
| 75 | + // name and type |
| 76 | + data.readInt(); |
| 77 | + break; |
| 78 | + default: |
| 79 | + throw new RuntimeException("Bad tag " + tag); |
| 80 | + } |
| 81 | + } |
| 82 | + short access_flag = data.readShort(); |
| 83 | + int this_class = data.readShort(); |
| 84 | + int super_class = data.readShort(); |
| 85 | + System.out.println(this_class); |
| 86 | + System.out.println(JSON.toJSON(offsetTable).toString()); |
| 87 | + System.out.println(JSON.toJSON(classNameTable).toString()); |
| 88 | + return classNameTable.get(offsetTable.get(this_class)) |
| 89 | + .replace('/', '.'); |
| 90 | + |
| 91 | + } catch (Exception e) { |
| 92 | + throw new RuntimeException(e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String[] args) throws IOException { |
| 97 | + String path ="target/classes/ch20/unitTest/Testable2.class"; |
| 98 | + String result = thisClass(BinaryFile.read(path)); |
| 99 | + System.out.println(result); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments