1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 02:06:01 +02:00

Code cleanup on Memory Search Result Page

- Use modern java instanceof
- Log unexpected exceptions
- reorder tests
- add missing null checks
- invert some if conditions for less nesting
This commit is contained in:
Lisa-Marie Saru 2023-08-18 13:33:06 +02:00 committed by Jonah Graham
parent f7a8c506f4
commit c9a38e541f

View file

@ -110,8 +110,8 @@ public class MemorySearchResultsPage extends Page implements ISearchResultPage,
@Override @Override
public void setInput(ISearchResult search, Object uiState) { public void setInput(ISearchResult search, Object uiState) {
if (search instanceof MemorySearchResult) { if (search instanceof MemorySearchResult result) {
((MemorySearchResult) search).addListener(new ISearchResultListener() { result.addListener(new ISearchResultListener() {
@Override @Override
public void searchResultChanged(SearchResultEvent e) { public void searchResultChanged(SearchResultEvent e) {
Display.getDefault().asyncExec(() -> fTreeViewer.refresh()); Display.getDefault().asyncExec(() -> fTreeViewer.refresh());
@ -176,34 +176,41 @@ public class MemorySearchResultsPage extends Page implements ISearchResultPage,
@Override @Override
public void selectionChanged(final SelectionChangedEvent event) { public void selectionChanged(final SelectionChangedEvent event) {
if (event.getSelection() instanceof StructuredSelection) {
IMemoryRenderingContainer containers[] = ((IMemorySearchQuery) fQuery).getMemoryView() IMemoryRenderingContainer containers[] = ((IMemorySearchQuery) fQuery).getMemoryView()
.getMemoryRenderingContainers(); .getMemoryRenderingContainers();
MemoryMatch match = (MemoryMatch) ((StructuredSelection) event.getSelection()).getFirstElement(); if (containers == null || containers.length == 0)
if (match != null) { return;
if (event.getSelection() instanceof StructuredSelection sel
&& sel.getFirstElement() instanceof MemoryMatch match) {
for (int i = 0; i < containers.length; i++) { for (int i = 0; i < containers.length; i++) {
IMemoryRendering rendering = containers[i].getActiveRendering(); IMemoryRendering rendering = containers[i].getActiveRendering();
if (rendering instanceof IRepositionableMemoryRendering) { if (!(rendering instanceof IRepositionableMemoryRendering repositionable))
continue;
try { try {
((IRepositionableMemoryRendering) rendering).goToAddress(match.getStartAddress()); repositionable.goToAddress(match.getStartAddress());
} catch (DebugException e) { } catch (DebugException e) {
MemorySearchPlugin.logError( MemorySearchPlugin.logError(
Messages.getString("MemorySearchResultsPage.RepositioningMemoryViewFailed"), //$NON-NLS-1$ Messages.getString("MemorySearchResultsPage.RepositioningMemoryViewFailed"), //$NON-NLS-1$
e); e);
} }
}
if (rendering != null) {
// Temporary, until platform accepts/adds new interface for setting the selection // Temporary, until platform accepts/adds new interface for setting the selection
try { try {
Method m = rendering.getClass().getMethod("setSelection", //$NON-NLS-1$ Method m = rendering.getClass().getMethod("setSelection", //$NON-NLS-1$
new Class[] { BigInteger.class, BigInteger.class }); new Class[] { BigInteger.class, BigInteger.class });
if (m != null) if (m != null)
m.invoke(rendering, match.getStartAddress(), match.getEndAddress()); m.invoke(rendering, match.getStartAddress(), match.getEndAddress());
} catch (NoSuchMethodException e) {
// Not all renderings have a compatible setSelection
// ideally this would be an interface we can check instead
// of using reflection!
} catch (Exception e) { } catch (Exception e) {
// do nothing MemorySearchPlugin.logError(
} "Exception while invoking the setSelection method for the current rendering", //$NON-NLS-1$
} e);
} }
} }
} }
@ -214,8 +221,8 @@ public class MemorySearchResultsPage extends Page implements ISearchResultPage,
@Override @Override
public void doubleClick(DoubleClickEvent event) { public void doubleClick(DoubleClickEvent event) {
// Open the memory view to emphasize the effect of the memory address selection in the search // Open the memory view to emphasize the effect of the memory address selection in the search
if (event.getSelection() instanceof StructuredSelection if (event.getSelection() instanceof StructuredSelection sel
&& ((StructuredSelection) event.getSelection()).getFirstElement() instanceof MemoryMatch) { && sel.getFirstElement() instanceof MemoryMatch) {
IWorkbenchPart wb = ((IMemorySearchQuery) fQuery).getMemoryView().getSite().getPart(); IWorkbenchPart wb = ((IMemorySearchQuery) fQuery).getMemoryView().getSite().getPart();
if (wb == null) if (wb == null)
return; return;
@ -229,8 +236,8 @@ public class MemorySearchResultsPage extends Page implements ISearchResultPage,
@Override @Override
public String getText(Object element) { public String getText(Object element) {
if (element instanceof MemoryMatch) if (element instanceof MemoryMatch match)
return "0x" + ((MemoryMatch) element).getStartAddress().toString(16); //$NON-NLS-1$ return "0x" + match.getStartAddress().toString(16); //$NON-NLS-1$
return element.toString(); return element.toString();
} }