Compiler Cleanup

This commit is contained in:
Konloch 2021-07-03 23:24:54 -07:00
parent 7b26b62084
commit 0c53463fd7
5 changed files with 46 additions and 36 deletions

View File

@ -18,6 +18,10 @@ package the.bytecode.club.bytecodeviewer.compilers;
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
import the.bytecode.club.bytecodeviewer.compilers.impl.JavaCompiler;
import the.bytecode.club.bytecodeviewer.compilers.impl.KrakatauAssembler;
import the.bytecode.club.bytecodeviewer.compilers.impl.SmaliAssembler;
/**
* A collection of all of the supported compilers/assemblers inside of BCV
*

View File

@ -26,7 +26,5 @@ package the.bytecode.club.bytecodeviewer.compilers;
public abstract class InternalCompiler
{
public abstract byte[] compile(String contents, String name);
}
}

View File

@ -1,4 +1,4 @@
package the.bytecode.club.bytecodeviewer.compilers;
package the.bytecode.club.bytecodeviewer.compilers.impl;
import java.io.BufferedReader;
import java.io.File;
@ -8,6 +8,7 @@ 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.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
@ -39,7 +40,6 @@ import static the.bytecode.club.bytecodeviewer.Constants.*;
public class JavaCompiler extends InternalCompiler
{
@Override
public byte[] compile(String contents, String name)
{
@ -102,7 +102,8 @@ public class JavaCompiler extends InternalCompiler
}
}
if (process.isAlive()) {
if (process.isAlive())
{
System.out.println("Force killing javac process, assuming it's gotten stuck");
process.destroyForcibly().destroy();
}
@ -116,18 +117,20 @@ public class JavaCompiler extends InternalCompiler
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line);
}
br.close();
log.append(nl).append(nl).append("Error:").append(nl).append(nl);
is = process.getErrorStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line);
}
br.close();
log.append(nl).append(nl).append("Exit Value is ").append(exitValue);
@ -135,12 +138,12 @@ public class JavaCompiler extends InternalCompiler
if (!clazz.exists())
throw new Exception(log.toString());
} catch (Exception e) {
cont = false;
e.printStackTrace();
} finally {
BytecodeViewer.sm.setBlocking();
}
BytecodeViewer.sm.setBlocking();
cp.delete();

View File

@ -1,13 +1,15 @@
package the.bytecode.club.bytecodeviewer.compilers;
package the.bytecode.club.bytecodeviewer.compilers.impl;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.io.FileUtils;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.Constants;
import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
@ -51,11 +53,9 @@ public class KrakatauAssembler extends InternalCompiler
return null;
}
String origName = name;
name = MiscUtils.randomString(20);
String origName = MiscUtils.randomString(20);
File tempD =
new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs);
File tempD = new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs);
tempD.mkdir();
File tempJ = new File(tempD.getAbsolutePath() + fs + name + ".j");
@ -63,12 +63,15 @@ public class KrakatauAssembler extends InternalCompiler
final File tempDirectory = new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs);
tempDirectory.mkdir();
final File tempJar = new File(Constants.tempDirectory + fs + "temp" + MiscUtils.randomString(32) + ".jar");
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), tempJar.getAbsolutePath());
BytecodeViewer.sm.stopBlocking();
StringBuilder log = new StringBuilder();
try {
BytecodeViewer.sm.stopBlocking();
try
{
ProcessBuilder pb = new ProcessBuilder(
Configuration.python,
"-O", //love you storyyeller <3
@ -86,26 +89,27 @@ public class KrakatauAssembler extends InternalCompiler
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line);
}
br.close();
log.append(nl).append(nl).append("Error:").append(nl).append(nl);
is = process.getErrorStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line);
}
br.close();
int exitValue = process.waitFor();
log.append(nl).append(nl).append("Exit Value is ").append(exitValue);
System.out.println(log);
byte[] b =
org.apache.commons.io.FileUtils.readFileToByteArray(new File(tempDirectory.getAbsolutePath() + fs + origName + ".class"));
byte[] b = FileUtils.readFileToByteArray(new File(tempDirectory.getAbsolutePath() + fs + origName + ".class"));
tempDirectory.delete();
tempJar.delete();
return b;

View File

@ -1,9 +1,11 @@
package the.bytecode.club.bytecodeviewer.compilers;
package the.bytecode.club.bytecodeviewer.compilers.impl;
import java.io.File;
import java.util.Objects;
import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.io.FileUtils;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler;
import the.bytecode.club.bytecodeviewer.util.Dex2Jar;
import the.bytecode.club.bytecodeviewer.util.Enjarify;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
@ -37,9 +39,9 @@ import static the.bytecode.club.bytecodeviewer.Constants.*;
public class SmaliAssembler extends InternalCompiler
{
@Override
public byte[] compile(String contents, String name) {
public byte[] compile(String contents, String name)
{
String fileStart = tempDirectory + fs + "temp";
int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex");
@ -79,21 +81,20 @@ public class SmaliAssembler extends InternalCompiler
boolean found = false;
File current = tempJarFolder;
try {
while (!found) {
while (!found)
{
File f = Objects.requireNonNull(current.listFiles())[0];
if (f.isDirectory())
current = f;
else {
else
{
outputClass = f;
found = true;
}
}
return org.apache.commons.io.FileUtils.readFileToByteArray(outputClass);
} catch (java.lang.NullPointerException ignored) {
}
return FileUtils.readFileToByteArray(outputClass);
} catch (java.lang.NullPointerException ignored) { }
} catch (Exception e) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
}