bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/api/ExceptionUI.java

146 lines
5.3 KiB
Java
Raw Normal View History

package the.bytecode.club.bytecodeviewer.api;
import java.awt.CardLayout;
import java.awt.Dimension;
2021-07-25 16:54:08 +00:00
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
2021-06-29 18:24:47 +00:00
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.gui.components.JFrameConsole;
2021-07-24 03:26:23 +00:00
import the.bytecode.club.bytecodeviewer.plugin.PluginManager;
2021-07-11 14:10:42 +00:00
import the.bytecode.club.bytecodeviewer.resources.IconResources;
2021-07-23 16:20:23 +00:00
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import static the.bytecode.club.bytecodeviewer.Constants.FAT_JAR;
import static the.bytecode.club.bytecodeviewer.Constants.VERSION;
import static the.bytecode.club.bytecodeviewer.Constants.nl;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://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/>. *
***************************************************************************/
/**
* A simple class designed to show exceptions in the UI.
2018-01-31 15:41:24 +00:00
*
* @author Konloch
*/
public class ExceptionUI extends JFrameConsole
2021-07-06 22:10:50 +00:00
{
2021-07-23 05:17:20 +00:00
public static final String KONLOCH = "https://github.com/Konloch/bytecode-viewer/issues or Konloch at https://the.bytecode.club or konloch@gmail.com";
public static final String SEND_STACKTRACE_TO = buildErrorLogHeader(KONLOCH);
2021-07-11 16:52:07 +00:00
public static final String SEND_STACKTRACE_TO_NL = SEND_STACKTRACE_TO + nl + nl;
2021-07-07 00:52:00 +00:00
2018-01-31 15:41:24 +00:00
/**
* @param e The exception to be shown
*/
public ExceptionUI(Throwable e) {
setupException(e, KONLOCH);
2018-01-31 15:41:24 +00:00
}
/**
* @param e The exception to be shown
*/
public ExceptionUI(String e) {
setupFrame(e, KONLOCH);
2018-01-31 15:41:24 +00:00
}
/**
* @param e The exception to be shown
* @param author the author of the plugin throwing this exception.
*/
public ExceptionUI(Throwable e, String author) {
2021-07-06 22:10:50 +00:00
setupException(e, author);
2018-01-31 15:41:24 +00:00
}
/**
* @param e The exception to be shown
* @param author the author of the plugin throwing this exception.
*/
public ExceptionUI(String e, String author) {
2021-07-06 22:10:50 +00:00
setupFrame(e, author);
2018-01-31 15:41:24 +00:00
}
/**
* Handles error suppression and prints stacktraces to strings
*/
2021-07-06 22:10:50 +00:00
private void setupException(Throwable error, String author)
2021-06-29 18:24:47 +00:00
{
//exceptions are completely hidden
if(Configuration.silenceExceptionGUI > 0)
return;
2021-07-06 22:10:50 +00:00
2021-06-29 18:24:47 +00:00
//exception GUI is disabled but printstack is still enabled
if(Configuration.pauseExceptionGUI > 0)
{
2021-07-06 22:10:50 +00:00
error.printStackTrace();
2021-06-29 18:24:47 +00:00
return;
}
2021-07-25 16:54:08 +00:00
try (StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw)) {
error.printStackTrace(pw);
error.printStackTrace();
setupFrame(sw.toString(), author);
} catch (IOException ignored) {
}
2021-07-06 22:10:50 +00:00
}
/**
* Creates a new frame and fills it with the error log
*/
2021-07-06 22:10:50 +00:00
private void setupFrame(String error, String author)
{
setIconImages(IconResources.iconList);
2018-01-31 15:41:24 +00:00
setSize(new Dimension(600, 400));
setTitle("Bytecode Viewer " + VERSION + " - Error Log - Send this to " + author);
2018-01-31 15:41:24 +00:00
getContentPane().setLayout(new CardLayout(0, 0));
2021-07-06 22:10:50 +00:00
getTextArea().setText(buildErrorLogHeader(author) + nl + nl + error);
getTextArea().setCaretPosition(0);
//embed error log as a new tab
if(Configuration.errorLogsAsNewTab)
2021-07-24 03:51:55 +00:00
PluginManager.addExceptionUI(this);
//pop open a new window frame
else
{
setLocationRelativeTo(BytecodeViewer.viewer);
setVisible(true);
}
2018-01-31 15:41:24 +00:00
}
2021-07-06 22:10:50 +00:00
/**
* Returns the error log header
*/
public static String buildErrorLogHeader(String author)
{
2021-07-21 14:44:25 +00:00
String fatJar = FAT_JAR ? " [Fat Jar]" : "";
2021-09-16 11:56:26 +00:00
return TranslatedStrings.PLEASE_SEND_THIS_ERROR_LOG_TO + " " + author +
"\n" + TranslatedStrings.PLEASE_SEND_RESOURCES +
2021-07-10 13:46:45 +00:00
"\nBytecode Viewer Version: " + VERSION + fatJar +
", OS: " + System.getProperty("os.name") +
", Java: " + System.getProperty("java.version");
}
2021-07-06 22:10:50 +00:00
private static final long serialVersionUID = -5230501978224926296L;
}