Skip to content

Commit

Permalink
Default formatter for Exception objects #125
Browse files Browse the repository at this point in the history
This commit modifies the default detail formatter of Exception objects
by showing Exception class name and its stack trace by default if no
specific detail formatter configured for Exception objects

Fixes : #125
  • Loading branch information
SougandhS committed Feb 3, 2025
1 parent 12984e7 commit 3f7854e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ void assert15Project() {
cfgs.add(createLaunchConfiguration(jp, "a.b.c.GenericMethodEntryTest"));
cfgs.add(createLaunchConfiguration(jp, "org.eclipse.debug.tests.targets.HcrClass", true));
cfgs.add(createLaunchConfiguration(jp, "a.b.c.Bug570988"));
cfgs.add(createLaunchConfiguration(jp, "a.b.c.ExceptionDefaultTest"));
loaded15 = true;
waitForBuild();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) Mar 12, 2016 IBM Corporation and others.
* Copyright (c) 2016, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -467,4 +467,51 @@ public void testFormatterForPrimitivesCharArray() throws Exception {
}
}

public void testFormatterForExceptionObjectsWithoutFormatter() throws Exception {
IJavaThread thread = null;
JavaDetailFormattersManager jdfm = JavaDetailFormattersManager.getDefault();
try {
String typename = "a.b.c.ExceptionDefaultTest";
createLineBreakpoint(19, typename);
thread = launchToBreakpoint(typename);
assertNotNull("The program did not suspend", thread);
IJavaVariable var = thread.findVariable("e");
assertNotNull("the variable 'e' must exist in the frame", var);
jdfm.computeValueDetail((IJavaValue) var.getValue(), thread, fListener);
waitForListenerValue();
assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value);
String value = fListener.result.toString().trim();
value = value.split("\n")[2];
assertEquals("a.b.c.ExceptionDefaultTest.main(ExceptionDefaultTest.java:18)", value);
} finally {
terminateAndRemove(thread);
removeAllBreakpoints();
}
}

public void testFormatterForExceptionObjectsWithFormatter() throws Exception {
IJavaThread thread = null;
DetailFormatter formatter = null;
JavaDetailFormattersManager jdfm = JavaDetailFormattersManager.getDefault();
try {
String typename = "a.b.c.ExceptionDefaultTest";
createLineBreakpoint(19, typename);
thread = launchToBreakpoint(typename);
assertNotNull("The program did not suspend", thread);
String snippet = "this.toString()";
formatter = new DetailFormatter("java.lang.Exception", snippet, true);
jdfm.setAssociatedDetailFormatter(formatter);
IJavaVariable var2 = thread.findVariable("e");
assertNotNull("the variable 'e' must exist in the frame", var2);
jdfm.computeValueDetail((IJavaValue) var2.getValue(), thread, fListener);
waitForListenerValue();
assertNotNull("The IValue of the detailComputed callback cannot be null", fListener.value);
assertEquals("java.lang.Exception", fListener.result.toString());
} finally {
jdfm.removeAssociatedDetailFormatter(formatter);
terminateAndRemove(thread);
removeAllBreakpoints();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package a.b.c;

public class ExceptionDefaultTest {
public static void main(String[] args) {
Exception e = new Exception();
int p=10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ private String getDetailFormatter(JDIType type) throws DebugException {
* @return the snippet for the given class / super class
* @throws DebugException if there is a problem computing the snippet
*/
@SuppressWarnings("nls")
private String getDetailFormatterSuperClass(IJavaClassType type) throws DebugException {
if (type == null) {
return null;
Expand All @@ -390,6 +391,17 @@ private String getDetailFormatterSuperClass(IJavaClassType type) throws DebugExc
if (detailFormatter != null && detailFormatter.isEnabled()) {
return detailFormatter.getSnippet();
}
if ((detailFormatter == null || !detailFormatter.isEnabled()) && type.getName().equals("java.lang.Throwable")) {
String snippet = """
StringBuilder stackTrace = new StringBuilder();
stackTrace.append(this.getClass().getName()+\" \\n\\n\");
for (StackTraceElement element : this.getStackTrace()) {
stackTrace.append(element.toString()+\" \\n\");
}
return stackTrace.toString();
""";
return snippet;
}
return getDetailFormatterSuperClass(type.getSuperclass());
}

Expand Down

0 comments on commit 3f7854e

Please sign in to comment.