Repository navigation
Expand file tree
/
Copy pathClassNameFinder.java
More file actions
65 lines (62 loc) · 1.73 KB
/
Copy pathClassNameFinder.java
File metadata and controls
65 lines (62 loc) · 1.73 KB
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
package btp.oneP;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.util.HashMap;
import java.util.Map;
public class ClassNameFinder {
public static String thisClass(byte[] classBytes){
Map<Integer,Integer> offsetTable = new HashMap<Integer,Integer>();
Map<Integer,String> classNameTable = new HashMap<Integer,String>();
try{
DataInputStream data = new DataInputStream(new ByteArrayInputStream(classBytes));
int magic = data.readInt();
int minorVersion = data.readShort();
int majorVersion = data.readShort();
int constant_pool_count = data.readShort();
int[] constant_pool = new int[constant_pool_count];
for(int i = 1;i<constant_pool_count;i++){
int tag = data.read();
int tableSize;
switch(tag){
case 1:
int length = data.readShort();
char[] bytes = new char[length];
for(int k = 0;k<bytes.length;k++){
bytes[k] = (char)data.read();
}
String className = new String(bytes);
classNameTable.put(i, className);
break;
case 5:
case 6:
data.readLong();
i++;
break;
case 7:
int offset = data.readShort();
offsetTable.put(i, offset);
break;
case 8:
data.readShort();
break;
case 3:
case 4:
case 9:
case 10:
case 11:
case 12:
data.readInt();
break;
default:
throw new RuntimeException();
}
}
short access_flags = data.readShort();
int this_class = data.readShort();
int super_class = data.readShort();
return classNameTable.get(offsetTable.get(this_class)).replace('/', '.');
}catch(Exception e){
throw new RuntimeException();
}
}
}