Skip to content

Commit

Permalink
using method name to find unqualified Stacktraces in Java Stack Trace
Browse files Browse the repository at this point in the history
console - WIP
  • Loading branch information
SougandhS committed Sep 17, 2024
1 parent be4bbd6 commit ba3d244
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.eclipse.jdt.debug.tests.console;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.debug.testplugin.JavaProjectHelper;
import org.eclipse.jdt.debug.tests.TestUtil;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.console.IHyperlink;
import org.eclipse.ui.texteditor.ITextEditor;

public class JavaStackTraceAmbiguityTest extends AbstractJavaStackTraceConsoleTest {

public JavaStackTraceAmbiguityTest(String name) {
super(name);
}

public void testLinkAmbiguityNavigationTrue() throws Exception {
String projectName = "StackTest";
IJavaProject project = createProject(projectName, "testfiles/Ambiguity/", JavaProjectHelper.JAVA_SE_1_8_EE_NAME, false);
waitForBuild();
waitForJobs();
consoleDocumentWithText("Sample.tes3(int) line: 31");
IHyperlink[] hyperlinks = fConsole.getHyperlinks();
assertEquals("Wrong hyperlinks, listing all links: " + allLinks(), 2, hyperlinks.length);
String expectedText = "System.out.println(\"Expected\");";
try {
for (IHyperlink hyperlink : hyperlinks) {
closeAllEditors();
hyperlink.linkActivated();
IEditorPart editor = waitForEditorOpen();
String[] selectedText = new String[1];
sync(() -> selectedText[0] = getSelectedText(editor));
selectedText[0] = selectedText[0].trim();
assertEquals("Wrong text selected after hyperlink navigation", expectedText, selectedText[0]);

}
} finally {
closeAllEditors();
boolean force = true;
project.getProject().delete(force, new NullProgressMonitor());
}
}
private void waitForJobs() throws Exception {
TestUtil.waitForJobs(getName(), 250, 1000);
}
private static String getSelectedText(IEditorPart editor) {
ITextEditor textEditor = (ITextEditor) editor;
ISelectionProvider selectionProvider = textEditor.getSelectionProvider();
ISelection selection = selectionProvider.getSelection();
ITextSelection textSelection = (ITextSelection) selection;
String selectedText = textSelection.getText();
return selectedText;
}

private IEditorPart waitForEditorOpen() throws Exception {
waitForJobs();
IEditorPart[] editor = new IEditorPart[1];
sync(() -> editor[0] = getActivePage().getActiveEditor());
long timeout = 30_000;
long start = System.currentTimeMillis();
while (editor[0] == null && System.currentTimeMillis() - start < timeout) {
waitForJobs();
sync(() -> editor[0] = getActivePage().getActiveEditor());
}
if (editor[0] == null) {
throw new AssertionError("Timeout occurred while waiting for editor to open");
}
return editor[0];
}
}
37 changes: 37 additions & 0 deletions org.eclipse.jdt.debug.tests/testfiles/Ambiguity/a/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2024 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;

/**
* Test class
*/
public class Sample {
public static void main(String[] args) {
System.out.println("Main Method");
test();
}
public static void test() {
System.out.println("Testing..");
}
public void testMethod() {
System.out.println("Random");
}
public static void tes3(int x) {
System.out.println("Expected");
}
public void tes2() {

}

}
37 changes: 37 additions & 0 deletions org.eclipse.jdt.debug.tests/testfiles/Ambiguity/b/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2024 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 b;

/**
* Test class
*/
public class Sample {
public static void main(String[] args) {
System.out.println("Main Method");
test();
}
public static void test() {
System.out.println("Testing..");
}
public void testMethod() {
System.out.println("Random");
}
public static void tes3() {
System.out.println("Expected");
}
public void tes2() {

}

}
37 changes: 37 additions & 0 deletions org.eclipse.jdt.debug.tests/testfiles/Ambiguity/c/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2024 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 c;

/**
* Test class
*/
public class Sample {
public static void main(String[] args) {
System.out.println("Main Method");
test();
}
public static void test() {
System.out.println("Testing..");
}
public void testMethod() {
System.out.println("Random");
}
public static void tes32() {
System.out.println("Expected");
}
public void tes2() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.eclipse.jdt.debug.tests.console.ConsoleTerminateAllActionTests;
import org.eclipse.jdt.debug.tests.console.IOConsoleTests;
import org.eclipse.jdt.debug.tests.console.JavaDebugStackTraceConsoleTest;
import org.eclipse.jdt.debug.tests.console.JavaStackTraceAmbiguityTest;
import org.eclipse.jdt.debug.tests.console.JavaStackTraceConsoleTest;
import org.eclipse.jdt.debug.tests.core.AlternateStratumTests;
import org.eclipse.jdt.debug.tests.core.ArgumentTests;
Expand Down Expand Up @@ -273,6 +274,7 @@ public AutomatedSuite() {
addTest(new TestSuite(JavaDebugStackTraceConsoleTest.class));
addTest(new TestSuite(IOConsoleTests.class));
addTest(new TestSuite(ConsoleTerminateAllActionTests.class));
addTest(new TestSuite(JavaStackTraceAmbiguityTest.class));

//Core tests
addTest(new TestSuite(DebugEventTests.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2017 IBM Corporation and others.
* Copyright (c) 2024 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 @@ -27,12 +27,15 @@
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.TypeNameMatch;
import org.eclipse.jdt.core.search.TypeNameMatchRequestor;
import org.eclipse.jdt.internal.core.SourceType;
import org.eclipse.jdt.internal.debug.core.JavaDebugUtils;
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jdt.internal.debug.ui.actions.OpenFromClipboardAction;
Expand All @@ -57,6 +60,7 @@
public class JavaStackTraceHyperlink implements IHyperlink {

private final TextConsole fConsole;
private String originalHyperLink;

/**
* Constructor
Expand Down Expand Up @@ -89,6 +93,7 @@ public void linkActivated() {
int lineNumber;
try {
String linkText = getLinkText();
originalHyperLink = linkText;
typeName = getTypeName(linkText);
lineNumber = getLineNumber(linkText);
} catch (CoreException e1) {
Expand Down Expand Up @@ -190,8 +195,37 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
} else if (source instanceof List) { // ambiguous
@SuppressWarnings("unchecked")
List<Object> matches = (List<Object>) source;
List<Object> exactMatchesFiltered = new ArrayList<>();
String originalHyperLink2 = originalHyperLink;
int firstMethodStartIndex = originalHyperLink2.indexOf("."); //$NON-NLS-1$
int firstMethodClosing = originalHyperLink2.indexOf(")"); //$NON-NLS-1$
if (firstMethodStartIndex != -1 && firstMethodClosing != -1) {
String firstMethod = originalHyperLink2.substring(firstMethodStartIndex + 1, firstMethodClosing + 1);
for (Object obj : matches) {
@SuppressWarnings("restriction")
SourceType source = (SourceType) obj;
try {
@SuppressWarnings("restriction")
IMethod[] methods = source.getMethods();
for (IMethod method : methods) {
int indexOfClosing = method.toString().indexOf(")"); //$NON-NLS-1$
int indexOfStart = method.toString().substring(0,indexOfClosing+1).lastIndexOf(" "); //$NON-NLS-1$
String methodName = method.toString().substring(indexOfStart + 1, indexOfClosing + 1);
if (methodName.equals(firstMethod)) {
exactMatchesFiltered.add(obj);
}
}
} catch (JavaModelException e) {
JDIDebugUIPlugin.log(e);
}
}
}
if (exactMatchesFiltered.size() == 1) {
processSearchResult(exactMatchesFiltered.get(0), typeName, lineNumber);
return Status.OK_STATUS;
}
int line = lineNumber + 1; // lineNumber starts with 0, but line with 1, see #linkActivated
OpenFromClipboardAction.handleMatches(matches, line, typeName, ConsoleMessages.JavaDebugStackTraceHyperlink_dialog_title);
OpenFromClipboardAction.handleMatches(exactMatchesFiltered, line, typeName, ConsoleMessages.JavaDebugStackTraceHyperlink_dialog_title);
return Status.OK_STATUS;
} else {
processSearchResult(source, typeName, lineNumber);
Expand Down

0 comments on commit ba3d244

Please sign in to comment.