Created
June 5, 2011 18:14
-
-
Save skratchdot/1009238 to your computer and use it in GitHub Desktop.
An example class for testing a method of setting conditional breakpoints based off of the hash of the current stack trace.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
/** | |
* An example class for testing a method of setting conditional breakpoints | |
* based off of the hash of the current stack trace. | |
* | |
* Eventually, I'd like to edit the Java breakpoint condition editor: | |
* org.eclipse.jdt.debug.ui.breakpoints.JavaBreakpointConditionEditor.java | |
* to allow keeping track of the stack trace history for a given breakpoint, and | |
* only break on certain hashes. It would also be nice to keep a count of each | |
* occurrence. | |
*/ | |
public class TestDebugStackTrace { | |
private JFrame frame; | |
private ClickListener clickListener; | |
private int actionNum = 0; | |
private JTextArea textArea; | |
/** | |
* Launch the application. | |
*/ | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
try { | |
TestDebugStackTrace window = new TestDebugStackTrace(); | |
window.frame.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
/** | |
* Create the application. | |
*/ | |
public TestDebugStackTrace() { | |
initialize(); | |
} | |
/** | |
* Initialize the contents of the frame. | |
*/ | |
private void initialize() { | |
clickListener = new ClickListener(); | |
frame = new JFrame(); | |
frame.setTitle("Test Breakpoints by hasing stack trace"); | |
frame.setBounds(100, 100, 450, 300); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// HASH1: ActionListener = clickListener | |
JButton btnOne = new JButton("Button 1 - HASH 1"); | |
btnOne.addActionListener(clickListener); | |
frame.getContentPane().add(btnOne, BorderLayout.WEST); | |
// HASH1: ActionListener = clickListener | |
JButton btnTwo = new JButton("Button 2 - HASH 1"); | |
btnTwo.addActionListener(clickListener); | |
frame.getContentPane().add(btnTwo, BorderLayout.EAST); | |
// HASH2: ActionListener = new ActionListener | |
JButton btnThree = new JButton("Button 3 - HASH 2"); | |
btnThree.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
performAction(e); | |
} | |
}); | |
frame.getContentPane().add(btnThree, BorderLayout.SOUTH); | |
// A place to show debug info (instead of writing to console) | |
JScrollPane scrollPane = new JScrollPane(); | |
frame.getContentPane().add(scrollPane, BorderLayout.CENTER); | |
textArea = new JTextArea(); | |
scrollPane.setViewportView(textArea); | |
} | |
/** | |
* This will call our private function {@link TestDebugStackTrace#performAction(ActionEvent)} | |
*/ | |
private class ClickListener implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
performAction(e); | |
} | |
} | |
/** | |
* Will increment the internal counter "actionNum", and write some | |
* debug info to our scrollable textArea. | |
* @param e A pass-through for the current ActionEvent | |
*/ | |
private void performAction(ActionEvent e) { | |
actionNum++; | |
textArea.append("(" + actionNum + ") " + getStackTraceInfo() + "\n"); | |
} | |
/** | |
* @return A debug string containing the stack trace as one line, prefixed | |
* by the hashCode of that string. | |
*/ | |
private String getStackTraceInfo() { | |
// Get stack trace as one line string (easier to compare differences in console) | |
String stackTrace = Arrays.toString(new Throwable().getStackTrace()); | |
// Store the hash of the stack trace | |
int hash = stackTrace.hashCode(); | |
// Get a pretty string for the console | |
String debugString = "" + hash + " ==> " + stackTrace; | |
return debugString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment