Skip to content

Commit

Permalink
Change how exceptions are handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
deathmarine committed Nov 17, 2016
1 parent 40118fb commit 15d23d8
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/us/deathmarine/luyten/ConfigSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void loadConfig() {
findWindowPosition = loadWindowPosition(prefs, FIND_WINDOW_ID_PREFIX);
luytenPreferences = loadLuytenPreferences(prefs);
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public void saveConfig() {
saveWindowPosition(prefs, FIND_WINDOW_ID_PREFIX, findWindowPosition);
saveLuytenPreferences(prefs);
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/us/deathmarine/luyten/DecompilerLinkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void writeDefinition(String text, Object definition, boolean isLocal) {
}
}
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand Down Expand Up @@ -88,7 +88,7 @@ public void writeReference(String text, Object reference, boolean isLocal) {
}
}
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}
};
Expand Down Expand Up @@ -360,7 +360,7 @@ public String getLinkDescription(String uniqueStr) {
}
} catch (Exception e) {
readableLink = null;
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
return readableLink;
}
Expand Down
2 changes: 1 addition & 1 deletion src/us/deathmarine/luyten/DropListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void drop(DropTargetDropEvent event) {
}
}
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}
event.dropComplete(true);
Expand Down
18 changes: 9 additions & 9 deletions src/us/deathmarine/luyten/FileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void run() {
Thread.sleep(500);
initSaveDialog();
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
};
}.start();
Expand Down Expand Up @@ -140,8 +140,8 @@ private void retrieveOpenDialogDir(JFileChooser fc) {
fc.setCurrentDirectory(currentDir);
}
}
} catch (Exception exc) {
exc.printStackTrace();
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand All @@ -151,8 +151,8 @@ private void saveOpenDialogDir(JFileChooser fc) {
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
luytenPrefs.setFileOpenCurrentDirectory(currentDir.getAbsolutePath());
}
} catch (Exception exc) {
exc.printStackTrace();
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand All @@ -165,8 +165,8 @@ private void retrieveSaveDialogDir(JFileChooser fc) {
fc.setCurrentDirectory(currentDir);
}
}
} catch (Exception exc) {
exc.printStackTrace();
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand All @@ -176,8 +176,8 @@ private void saveSaveDialogDir(JFileChooser fc) {
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
luytenPrefs.setFileSaveCurrentDirectory(currentDir.getAbsolutePath());
}
} catch (Exception exc) {
exc.printStackTrace();
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!",e);
}
}
}
14 changes: 8 additions & 6 deletions src/us/deathmarine/luyten/FileSaver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.deathmarine.luyten;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
Expand All @@ -11,6 +12,7 @@
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Enumeration;
Expand All @@ -25,8 +27,11 @@
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import com.strobel.assembler.metadata.ITypeLoader;
Expand Down Expand Up @@ -88,8 +93,7 @@ public void run() {
label.setText("Completed: " + getTime(time));
} catch (Exception e1) {
label.setText("Cannot save file: " + file.getName());
e1.printStackTrace();
JOptionPane.showMessageDialog(null, e1.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
Luyten.showExceptionDialog("Unable to save file!\n",e1);
} finally {
setExtracting(false);
bar.setVisible(false);
Expand Down Expand Up @@ -125,9 +129,8 @@ public void run() {
label.setText("Completed: " + getTime(time));
}
} catch (Exception e1) {
e1.printStackTrace();
label.setText("Cannot save file: " + outFile.getName());
JOptionPane.showMessageDialog(null, e1.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
Luyten.showExceptionDialog("Unable to save file!\n",e1);
} finally {
setExtracting(false);
bar.setVisible(false);
Expand Down Expand Up @@ -195,9 +198,8 @@ private void doSaveJarDecompiled(File inFile, File outFile) throws Exception {
settings.getLanguage().decompileType(resolvedType, plainTextOutput, decompilationOptions);
writer.flush();
} catch(Exception e){
//e.printStackTrace();
label.setText("Cannot decompile file: " + entry.getName());
JOptionPane.showMessageDialog(null, "Unable to Decompile file!\n"+e.toString()+"\nSkipping file...", "Error!", JOptionPane.ERROR_MESSAGE);
Luyten.showExceptionDialog("Unable to Decompile file!\nSkipping file...",e);
} finally {
out.closeEntry();
}
Expand Down
8 changes: 3 additions & 5 deletions src/us/deathmarine/luyten/FindAllBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void mouseClicked(MouseEvent evt) {
try {
mainWindow.getModel().extractClassToTextPane(type, array[array.length-1], entryName, null);
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}


Expand Down Expand Up @@ -218,10 +218,8 @@ public void run() {
findButton.setText("Find");
}
jfile.close();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!",e);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/us/deathmarine/luyten/JFontChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ private void update(DocumentEvent event) {
Document doc = event.getDocument();
newValue = doc.getText(0, doc.getLength());
} catch (BadLocationException e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}

if (newValue.length() > 0) {
Expand Down
87 changes: 87 additions & 0 deletions src/us/deathmarine/luyten/Luyten.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
package us.deathmarine.luyten;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicReference;
import java.util.List;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.text.DefaultEditorKit;

/**
* Starter, the main class
Expand Down Expand Up @@ -101,4 +125,67 @@ public static String getVersion(){
return result;

}

/**
* Method allows for users to copy the stacktrace for reporting any issues.
* Enhanced for mouse users.
*
* @param message
* @param e
*/
public static void showExceptionDialog(String message, Exception e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String stacktrace = sw.toString();
try {
sw.close();
pw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(stacktrace);

JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
if(message.contains("\n")){
for(String s:message.split("\n")){
pane.add(new JLabel(s));
}
}else{
pane.add(new JLabel(message));
}
pane.add(new JLabel(" \n")); //Whitespace
final JTextArea exception = new JTextArea(25,100);
exception.setFont(new Font(Font.SANS_SERIF,Font.PLAIN, 10));
exception.setText(stacktrace);
exception.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
new JPopupMenu() {
{
JMenuItem menuitem = new JMenuItem("Select All");
menuitem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exception.requestFocus();
exception.selectAll();
}
});
this.add(menuitem);
menuitem = new JMenuItem("Copy");
menuitem.addActionListener(new DefaultEditorKit.CopyAction());
this.add(menuitem);
}
private static final long serialVersionUID = 562054483562666832L;
}.show(e.getComponent(), e.getX(), e.getY());
}
}
});
JScrollPane scroll = new JScrollPane(exception);
scroll.setBorder(new CompoundBorder(BorderFactory.createTitledBorder("Stacktrace"),new BevelBorder(BevelBorder.LOWERED)));
pane.add(scroll);
JOptionPane.showMessageDialog(null, pane, "Error!", JOptionPane.ERROR_MESSAGE);
}
}
4 changes: 2 additions & 2 deletions src/us/deathmarine/luyten/MainMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void run() {
buildHelpMenu(helpMenu);
refreshMenuPopup(helpMenu);
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand All @@ -117,7 +117,7 @@ private void refreshMenuPopup(JMenu menu) {
menu.getPopupMenu().setVisible(true);
}
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}
}.start();
Expand Down
18 changes: 9 additions & 9 deletions src/us/deathmarine/luyten/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public int getLastDividerLocation() {
dt.addDropTargetListener(new DropListener(this));
this.setDropTarget(dt);
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}

fileDialog = new FileDialog(this);
Expand Down Expand Up @@ -199,7 +199,7 @@ public void onSelectAllMenu() {
pane.setSelectionEnd(pane.getText().length());
}
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand All @@ -212,7 +212,7 @@ public void onFindMenu() {
findBox.showFindBox();
}
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand All @@ -223,7 +223,7 @@ public void onFindAllMenu() {
findAllBox.showFindBox();

} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ private static Iterator<?> list(ClassLoader CL){
Vector<?> classes = (Vector<?>) ClassLoader_classes_field.get(CL);
return classes.iterator();
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
return null;
}
Expand All @@ -298,7 +298,7 @@ private String getLegalStr() {
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");
} catch (IOException e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
return sb.toString();
}
Expand Down Expand Up @@ -329,7 +329,7 @@ public void onFileLoadEnded(File file, boolean isSuccess) {
this.setTitle(TITLE);
}
} catch (Exception e) {
e.printStackTrace();
Luyten.showExceptionDialog("Exception!",e);
}
}

Expand Down Expand Up @@ -414,8 +414,8 @@ private void quit() {
try {
windowPosition.readPositionFromWindow(this);
configSaver.saveConfig();
} catch (Exception exc) {
exc.printStackTrace();
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!",e);
} finally {
try {
this.dispose();
Expand Down
Loading

0 comments on commit 15d23d8

Please sign in to comment.