/
Disassembling.java
112 lines (97 loc) · 3.11 KB
/
Disassembling.java
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
package org.isk.jvmhardcore.pjba;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import org.isk.jvmhardcore.pjba.structure.ClassFile;
import org.junit.Test;
public class Disassembling {
private static String GENERATED_DIRECTORY;
/**
* Find the current directory and set the one that will use used to generate the class files.
*
* @throws Exception
*/
static {
final File directory = new File(".");
try {
if (directory.getCanonicalPath().contains("pjba")) {
GENERATED_DIRECTORY = "02_build/class-classes/";
} else {
GENERATED_DIRECTORY = "03_projects/pjba/02_build/class-classes/";
}
} catch (IOException e) {
}
}
/**
* Disassembles all files in the directory <code>pjb-classes/</code> of the current project and reassembles them in
* the directory <code>class-classes</code>.
*
* @throws Exception
*/
@Test
public void disassembleClasses() throws Exception {
final Enumeration<URL> enums = Thread.currentThread().getContextClassLoader().getResources(".");
URL resource = null;
while (enums.hasMoreElements()) {
resource = enums.nextElement();
if (resource.getPath().contains("pjb-classes")) {
break;
}
// If not found => NullPointerException
resource = null;
}
final File folder = new File(resource.getPath());
this.goThroughEachFolder(folder);
}
private void goThroughEachFolder(final File file) throws Exception {
final File[] listFiles = file.listFiles();
if (listFiles != null) {
for (final File fileEntry : listFiles) {
if (fileEntry.isFile()) {
this.disassemble(fileEntry);
} else {
this.goThroughEachFolder(fileEntry);
}
}
}
}
/**
* Generate a PJBA ClassFile.
*
* @param filepath
* @throws Exception
*/
public void disassemble(final File file) throws Exception {
final InputStream inputStream = new FileInputStream(file);
final DataInput dataInput = new DataInputStream(inputStream);
final Disassembler disassambler = new Disassembler(dataInput);
final ClassFile classFile = disassambler.disassemble();
this.createFile(classFile);
}
/**
* Create a class file in the directory <code>02_build/pjb-classes</code>.
*
* @param classFile
* @throws Exception
*/
public void createFile(final ClassFile classFile) throws Exception {
final String directoryStr = GENERATED_DIRECTORY + classFile.getDirectories();
final File directory = new File(directoryStr);
if (!directory.exists()) {
directory.mkdirs();
}
final FileOutputStream file = new FileOutputStream(directoryStr + classFile.getClassName() + ".class");
final DataOutput bytecode = new DataOutputStream(file);
final Assembler assembler = new Assembler(classFile, bytecode);
assembler.assemble();
file.close();
}
}