|
| 1 | +/*************************************************************************** |
| 2 | + * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * |
| 3 | + * Copyright (C) 2014 Konloch - Konloch.com / BytecodeViewer.com * |
| 4 | + * * |
| 5 | + * This program is free software: you can redistribute it and/or modify * |
| 6 | + * it under the terms of the GNU General Public License as published by * |
| 7 | + * the Free Software Foundation, either version 3 of the License, or * |
| 8 | + * (at your option) any later version. * |
| 9 | + * * |
| 10 | + * This program is distributed in the hope that it will be useful, * |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | + * GNU General Public License for more details. * |
| 14 | + * * |
| 15 | + * You should have received a copy of the GNU General Public License * |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | + ***************************************************************************/ |
| 18 | + |
| 19 | +package the.bytecode.club.bytecodeviewer.decompilers.impl; |
| 20 | + |
| 21 | +import me.konloch.kontainer.io.DiskReader; |
| 22 | +import org.objectweb.asm.tree.ClassNode; |
| 23 | +import the.bytecode.club.bytecodeviewer.Constants; |
| 24 | +import the.bytecode.club.bytecodeviewer.api.ExceptionUI; |
| 25 | +import the.bytecode.club.bytecodeviewer.decompilers.AbstractDecompiler; |
| 26 | +import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings; |
| 27 | +import the.bytecode.club.bytecodeviewer.util.ExceptionUtils; |
| 28 | +import the.bytecode.club.bytecodeviewer.util.TempFile; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileOutputStream; |
| 32 | + |
| 33 | +import static the.bytecode.club.bytecodeviewer.Constants.*; |
| 34 | +import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.*; |
| 35 | + |
| 36 | +/** |
| 37 | + * This is an unused class. This is meant to act as a blank decompiler template to aid in developers creating new decompilers / disassemblers. |
| 38 | + * |
| 39 | + * @author Konloch |
| 40 | + * @since 10/02/2024 |
| 41 | + */ |
| 42 | +public class BlankDecompilerBase extends AbstractDecompiler |
| 43 | +{ |
| 44 | + public BlankDecompilerBase() |
| 45 | + { |
| 46 | + super("[Your] Decompiler", "yourdecompiler"); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String decompileClassNode(ClassNode cn, byte[] bytes) |
| 51 | + { |
| 52 | + TempFile tempFile = null; |
| 53 | + String exception; |
| 54 | + |
| 55 | + try |
| 56 | + { |
| 57 | + //create the temporary files |
| 58 | + tempFile = TempFile.createTemporaryFile(true, ".class"); |
| 59 | + File tempInputClassFile = tempFile.getFile(); |
| 60 | + File tempOutputJavaFile = tempFile.createFileFromExtension(false, true, ".java"); |
| 61 | + |
| 62 | + //write the class-file with bytes |
| 63 | + try (FileOutputStream fos = new FileOutputStream(tempInputClassFile)) |
| 64 | + { |
| 65 | + fos.write(bytes); |
| 66 | + } |
| 67 | + |
| 68 | + //decompile the class-file |
| 69 | + //TODO this is where you would call your decompiler api |
| 70 | + // such as YourDecompiler.decompile(tempClassFile, tempOutputJavaFile); |
| 71 | + |
| 72 | + //handle simulated errors |
| 73 | + if(Constants.DEV_FLAG_DECOMPILERS_SIMULATED_ERRORS) |
| 74 | + throw new RuntimeException(DEV_MODE_SIMULATED_ERROR.toString()); |
| 75 | + |
| 76 | + //if the output file is found, read it |
| 77 | + if (tempOutputJavaFile.exists()) |
| 78 | + return DiskReader.loadAsString(tempOutputJavaFile.getAbsolutePath()); |
| 79 | + else |
| 80 | + exception = getDecompilerName() + " " + ERROR + "! " + tempOutputJavaFile.getAbsolutePath() + " does not exist."; |
| 81 | + } |
| 82 | + catch (Throwable e) |
| 83 | + { |
| 84 | + exception = ExceptionUtils.exceptionToString(e); |
| 85 | + } |
| 86 | + finally |
| 87 | + { |
| 88 | + //cleanup temp files |
| 89 | + if(tempFile != null) |
| 90 | + tempFile.cleanup(); |
| 91 | + } |
| 92 | + |
| 93 | + return getDecompilerName() + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL |
| 94 | + + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void decompileToZip(String sourceJar, String zipName) |
| 99 | + { |
| 100 | + decompileToZipFallBack(sourceJar, zipName); |
| 101 | + } |
| 102 | +} |
0 commit comments