-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathJavaCompiler.java
More file actions
154 lines (132 loc) · 6.48 KB
/
JavaCompiler.java
File metadata and controls
154 lines (132 loc) · 6.48 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
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
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Konloch - Konloch.com / BytecodeViewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
package the.bytecode.club.bytecodeviewer.compilers.impl;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import me.konloch.kontainer.io.DiskWriter;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler;
import the.bytecode.club.bytecodeviewer.resources.ExternalResources;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import the.bytecode.club.bytecodeviewer.util.SleepUtil;
import static the.bytecode.club.bytecodeviewer.Constants.fs;
import static the.bytecode.club.bytecodeviewer.Constants.nl;
import static the.bytecode.club.bytecodeviewer.Constants.tempDirectory;
/**
* Java Compiler
*
* @author Konloch
*/
public class JavaCompiler extends InternalCompiler
{
@Override
public byte[] compile(String contents, String fullyQualifiedName)
{
String fileStart = tempDirectory + fs + "temp" + MiscUtils.randomString(12) + fs;
String fileStart2 = tempDirectory + fs + "temp" + MiscUtils.randomString(12) + fs;
File java = new File(fileStart + fs + fullyQualifiedName + ".java");
File clazz = new File(fileStart2 + fs + fullyQualifiedName + ".class");
File cp = new File(tempDirectory + fs + "cpath_" + MiscUtils.randomString(12) + ".jar");
File tempD = new File(fileStart + fs + fullyQualifiedName.substring(0, fullyQualifiedName.length() - fullyQualifiedName.split("/")[fullyQualifiedName.split("/").length - 1].length()));
tempD.mkdirs();
new File(fileStart2).mkdirs();
if (Configuration.javac.isEmpty() || !new File(Configuration.javac).exists()) {
BytecodeViewer.showMessage("You need to set your Javac path, this requires the JDK to be downloaded." + nl + "(C:/Program Files/Java/JDK_xx/bin/javac.exe)");
ExternalResources.getSingleton().selectJavac();
}
if (Configuration.javac.isEmpty() || !new File(Configuration.javac).exists()) {
BytecodeViewer.showMessage("You need to set Javac!");
return null;
}
DiskWriter.replaceFile(java.getAbsolutePath(), contents, false);
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), cp.getAbsolutePath());
boolean cont = true;
try {
StringBuilder log = new StringBuilder();
ProcessBuilder pb;
if (Configuration.library.isEmpty()) {
pb = new ProcessBuilder(
Configuration.javac,
"-d", fileStart2,
"-classpath", cp.getAbsolutePath(),
java.getAbsolutePath()
);
} else {
pb = new ProcessBuilder(
Configuration.javac,
"-d", fileStart2,
"-classpath",
cp.getAbsolutePath() + File.pathSeparator + Configuration.library,
java.getAbsolutePath()
);
}
Process process = pb.start();
BytecodeViewer.createdProcesses.add(process);
Thread failSafe = new Thread(() ->
{
//wait 10 seconds
SleepUtil.sleep(10_000);
if (process.isAlive())
{
System.out.println("Force killing javac process, assuming it's gotten stuck");
process.destroyForcibly().destroy();
}
}, "Javac Fail-Safe");
failSafe.start();
int exitValue = process.waitFor();
//Read out dir output
try (InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr)) {
String line;
while ((line = br.readLine()) != null)
log.append(nl).append(line);
}
log.append(nl).append(nl).append(TranslatedStrings.ERROR2).append(nl).append(nl);
try (InputStream is = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr)) {
String line;
while ((line = br.readLine()) != null)
log.append(nl).append(line);
}
log.append(nl).append(nl).append(TranslatedStrings.EXIT_VALUE_IS).append(" ").append(exitValue);
System.out.println(log);
if (!clazz.exists())
throw new Exception(log.toString());
} catch (Exception e) {
cont = false;
e.printStackTrace();
}
cp.delete();
if (cont)
try {
return org.apache.commons.io.FileUtils.readFileToByteArray(clazz);
} catch (IOException e) {
e.printStackTrace();
//BytecodeViewer.handleException(e);
}
return null;
}
}