Skip to content

Instantly share code, notes, and snippets.

@asdf913
Last active June 29, 2023 23:15
Show Gist options
  • Save asdf913/336421aaa1660de726a172e1c52a2a04 to your computer and use it in GitHub Desktop.
Save asdf913/336421aaa1660de726a172e1c52a2a04 to your computer and use it in GitHub Desktop.
FilterThrowableStackTrace - Filter "stackTrace" in "java.lang.Throwable" by "java.lang.Class"
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.oxbow.swingbits.dialog.task.TaskDialogs;
import io.github.toolfactory.narcissus.Narcissus;
import net.miginfocom.swing.MigLayout;
public class FilterThrowableStackTrace extends JFrame implements ActionListener {
private static final long serialVersionUID = 1852392683389778784L;
private FilterThrowableStackTrace() {
}
private AbstractButton jcbFilter, jbExecute = null;
private void init() {
//
add(jcbFilter = new JCheckBox("Filter"));
//
add(jbExecute = new JButton("Execute"));
//
jbExecute.addActionListener(this);
//
}
public static void main(final String[] args) {
//
final FilterThrowableStackTrace instance = new FilterThrowableStackTrace();
//
instance.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//
instance.setLayout(new MigLayout());
//
instance.init();
//
instance.pack();
//
instance.setVisible(true);
//
}
@Override
public void actionPerformed(final ActionEvent evt) {
//
if (Objects.equals(evt != null ? evt.getSource() : null, jbExecute)) {
//
final Throwable throwable = new Throwable();
//
if (!GraphicsEnvironment.isHeadless()) {
//
if (throwable.getMessage() == null) {
//
try {
//
Narcissus.setObjectField(throwable, Throwable.class.getDeclaredField("detailMessage"), "");
//
} catch (final NoSuchFieldException e) {
//
e.printStackTrace();
//
} // try
//
} // if
//
if (jcbFilter != null && jcbFilter.isSelected()) {
//
filterStackTrace(throwable, getClass());
//
} // if
//
TaskDialogs.showException(throwable);
//
} // if
//
} // if
//
}
private static void filterStackTrace(final Throwable throwable, final Class<?> clz) {
//
final StackTraceElement[] stes = throwable != null ? throwable.getStackTrace() : null;
//
Integer index = null;
//
for (int i = (stes != null ? stes.length : 0) - 1; i >= 0; i--) {
//
if (Objects.equals(clz, forName(getClassName(stes[i])))) {
//
index = Integer.valueOf(i);
//
break;
//
} // if
//
} // for
//
if (index != null) {
//
try {
//
Narcissus.setObjectField(throwable, Throwable.class.getDeclaredField("stackTrace"),
ArrayUtils.subarray(stes, 0, index.intValue() + 1));
//
} catch (final NoSuchFieldException ex) {
//
ex.printStackTrace();
//
} // try
//
} // if
//
}
private static String getClassName(final StackTraceElement instance) {
return instance != null ? instance.getClassName() : null;
}
private static Class<?> forName(final String className) {
try {
return StringUtils.isNotBlank(className) ? Class.forName(className) : null;
} catch (final ClassNotFoundException e) {
return null;
}
}
}
@asdf913
Copy link
Author

asdf913 commented Jun 29, 2023

Maven Dependencies

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
 <version>3.12.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.bidib.org.oxbow/swingbits -->
<dependency>
 <groupId>org.bidib.org.oxbow</groupId>
 <artifactId>swingbits</artifactId>
 <version>1.2.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.toolfactory/narcissus -->
<dependency>
 <groupId>io.github.toolfactory</groupId>
 <artifactId>narcissus</artifactId>
 <version>1.0.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.miglayout/miglayout-swing -->
<dependency>
 <groupId>com.miglayout</groupId>
 <artifactId>miglayout-swing</artifactId>
 <version>11.1</version>
</dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment