From b3346e843db4322dc130ab0f0f7667eeade4c41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Tue, 23 Jan 2024 11:52:35 +0100 Subject: [PATCH] fix "The allocated object is never used" warnings --- .../jface/text/hyperlink/URLHyperlinkDetector.java | 3 ++- .../ui/refactoring/ChangeExceptionHandler.java | 3 ++- .../internal/ui/refactoring/PreviewWizardPage.java | 3 ++- .../history/RefactoringPropertyPage.java | 3 ++- .../search/ui/text/AbstractTextSearchViewPage.java | 4 +++- .../internal/ui/SearchHistoryDropDownAction.java | 3 ++- .../org/eclipse/search2/internal/ui/SearchView.java | 3 ++- .../search/internal/ui/text/FileLabelProvider.java | 6 ++++-- .../search/internal/ui/text/FileSearchQuery.java | 5 ++++- .../internal/ui/text/ReplaceConfigurationPage.java | 3 ++- .../internal/ui/text/SearchResultUpdater.java | 2 -- .../eclipse/ui/texteditor/FindReplaceDialog.java | 3 ++- .../eclipse/text/tests/link/LinkedPositionTest.java | 9 ++++++--- .../tests/revisions/ChangeRegionTest.java | 13 +++++++++---- 14 files changed, 42 insertions(+), 21 deletions(-) diff --git a/bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java b/bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java index 96bdefe2555..dd2b2836be5 100644 --- a/bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java +++ b/bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java @@ -153,7 +153,8 @@ else if (nextWhitespace != -1) // Set and validate URL string try { urlString= line.substring(urlOffsetInLine, urlOffsetInLine + urlLength); - new URL(urlString); + @SuppressWarnings("unused") + URL validated= new URL(urlString); } catch (MalformedURLException ex) { urlString= null; return null; diff --git a/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/ChangeExceptionHandler.java b/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/ChangeExceptionHandler.java index 913c531c468..b6113bec067 100644 --- a/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/ChangeExceptionHandler.java +++ b/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/ChangeExceptionHandler.java @@ -62,7 +62,8 @@ protected Control createMessageArea (Composite parent) { Control result= super.createMessageArea(parent); // Panic code: use 'parent' instead of 'result' in case super implementation changes in the future - new Label(parent, SWT.NONE); // filler as parent has 2 columns (icon and label) + @SuppressWarnings("unused") + Label filler= new Label(parent, SWT.NONE); // filler as parent has 2 columns (icon and label) Label label= new Label(parent, SWT.NONE); label.setText(RefactoringUIMessages.ChangeExceptionHandler_button_explanation); label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); diff --git a/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/PreviewWizardPage.java b/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/PreviewWizardPage.java index e21b1bccf4a..d0b5207371e 100644 --- a/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/PreviewWizardPage.java +++ b/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/PreviewWizardPage.java @@ -241,7 +241,8 @@ public Menu getMenu(Control parent) { for (FilterAction fFilterAction : fFilterActions) { new ActionContributionItem(fFilterAction).fill(fMenu, -1); } - new MenuItem(fMenu, SWT.SEPARATOR); + @SuppressWarnings("unused") + MenuItem menuItem= new MenuItem(fMenu, SWT.SEPARATOR); } new ActionContributionItem(fHideDerivedAction).fill(fMenu, -1); return fMenu; diff --git a/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/history/RefactoringPropertyPage.java b/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/history/RefactoringPropertyPage.java index 4980d5dfe89..454f3cd35ae 100644 --- a/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/history/RefactoringPropertyPage.java +++ b/bundles/org.eclipse.ltk.ui.refactoring/src/org/eclipse/ltk/internal/ui/refactoring/history/RefactoringPropertyPage.java @@ -197,7 +197,8 @@ public final void widgetSelected(final SelectionEvent event) { fShareHistoryButton.setLayoutData(data); fShareHistoryButton.setSelection(hasSharedRefactoringHistory()); - new Label(composite, SWT.NONE); + @SuppressWarnings("unused") + Label label= new Label(composite, SWT.NONE); final IProject project= getCurrentProject(); if (project != null) { diff --git a/bundles/org.eclipse.search/newsearch/org/eclipse/search/ui/text/AbstractTextSearchViewPage.java b/bundles/org.eclipse.search/newsearch/org/eclipse/search/ui/text/AbstractTextSearchViewPage.java index aba0d224119..3b4dabdbf06 100644 --- a/bundles/org.eclipse.search/newsearch/org/eclipse/search/ui/text/AbstractTextSearchViewPage.java +++ b/bundles/org.eclipse.search/newsearch/org/eclipse/search/ui/text/AbstractTextSearchViewPage.java @@ -273,6 +273,7 @@ public void selectionChanged(SelectionChangedEvent event) { * Flag (value 2) denoting tree layout. */ public static final int FLAG_LAYOUT_TREE = 2; + private OpenAndLinkWithEditorHelper openAndLinkWithEditorHelper; /** @@ -741,7 +742,7 @@ private void createViewer(Composite parent, int layout) { fillToolbar(tbm); tbm.update(false); - new OpenAndLinkWithEditorHelper(fViewer) { + openAndLinkWithEditorHelper = new OpenAndLinkWithEditorHelper(fViewer) { @Override protected void activate(ISelection selection) { @@ -1135,6 +1136,7 @@ public void dispose() { AnnotationManagers.removeSearchResult(getSite().getWorkbenchWindow(), oldSearch); super.dispose(); NewSearchUI.removeQueryListener(fQueryListener); + openAndLinkWithEditorHelper.dispose(); } @Override diff --git a/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchHistoryDropDownAction.java b/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchHistoryDropDownAction.java index d19ab5645b3..71a311ee795 100644 --- a/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchHistoryDropDownAction.java +++ b/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchHistoryDropDownAction.java @@ -126,7 +126,8 @@ public Menu getMenu(Control parent) { action.setChecked(searchResult.equals(currentSearch)); addActionToMenu(fMenu, action); } - new MenuItem(fMenu, SWT.SEPARATOR); + @SuppressWarnings("unused") + MenuItem menuItem = new MenuItem(fMenu, SWT.SEPARATOR); addActionToMenu(fMenu, new ShowSearchHistoryDialogAction(fSearchView)); addActionToMenu(fMenu, new RemoveAllSearchesAction()); } diff --git a/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchView.java b/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchView.java index 1294c94239e..7c1f2124d1a 100644 --- a/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchView.java +++ b/bundles/org.eclipse.search/newsearch/org/eclipse/search2/internal/ui/SearchView.java @@ -526,7 +526,8 @@ public void createPartControl(Composite parent) { } private void initializePageSwitcher() { - new PageSwitcher(this) { + @SuppressWarnings("unused") + PageSwitcher pageSwitcher = new PageSwitcher(this) { @Override public void activatePage(Object page) { ISearchResult searchResult= ((ISearchQuery) page).getSearchResult(); diff --git a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileLabelProvider.java b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileLabelProvider.java index 17157cd3b26..4434698c976 100644 --- a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileLabelProvider.java +++ b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileLabelProvider.java @@ -87,8 +87,10 @@ public StyledString getStyledText(Object element) { return new StyledString(); IResource resource= (IResource) element; - if (!resource.exists()) - new StyledString(SearchMessages.FileLabelProvider_removed_resource_label); + if (!resource.exists()) { + StyledString str = new StyledString(SearchMessages.FileLabelProvider_removed_resource_label); + return getColoredLabelWithCounts(resource, str); + } String name= BasicElementLabels.getResourceName(resource); if (fOrder == SHOW_LABEL) { diff --git a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileSearchQuery.java b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileSearchQuery.java index a3fe73456dd..27c49d413b1 100644 --- a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileSearchQuery.java +++ b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/FileSearchQuery.java @@ -40,6 +40,7 @@ import org.eclipse.search.internal.ui.SearchMessages; import org.eclipse.search.ui.ISearchQuery; import org.eclipse.search.ui.ISearchResult; +import org.eclipse.search.ui.NewSearchUI; import org.eclipse.search.ui.text.AbstractTextSearchResult; import org.eclipse.search.ui.text.FileTextSearchScope; import org.eclipse.search.ui.text.Match; @@ -334,7 +335,9 @@ public boolean canRerun() { public ISearchResult getSearchResult() { if (fResult == null) { fResult= new FileSearchResult(this); - new SearchResultUpdater(fResult); + SearchResultUpdater listener = new SearchResultUpdater(fResult); + NewSearchUI.addQueryListener(listener); + ResourcesPlugin.getWorkspace().addResourceChangeListener(listener); } return fResult; } diff --git a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceConfigurationPage.java b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceConfigurationPage.java index c2f12c2a943..62a2b5a7aa7 100644 --- a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceConfigurationPage.java +++ b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceConfigurationPage.java @@ -127,7 +127,8 @@ public void createControl(Composite parent) { new char[0], true); - new Label(result, SWT.NONE); + @SuppressWarnings("unused") + Label label = new Label(result, SWT.NONE); fReplaceWithRegex= new Button(result, SWT.CHECK); fReplaceWithRegex.setText(SearchMessages.ReplaceConfigurationPage_isRegex_label); fReplaceWithRegex.addSelectionListener(new SelectionAdapter() { diff --git a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/SearchResultUpdater.java b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/SearchResultUpdater.java index c0f1d267c3b..063302948c0 100644 --- a/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/SearchResultUpdater.java +++ b/bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/text/SearchResultUpdater.java @@ -34,8 +34,6 @@ public class SearchResultUpdater implements IResourceChangeListener, IQueryListe public SearchResultUpdater(AbstractTextSearchResult result) { fResult= result; - NewSearchUI.addQueryListener(this); - ResourcesPlugin.getWorkspace().addResourceChangeListener(this); } @Override diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java index daf46f93c79..8fa2d1affa5 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java @@ -316,7 +316,8 @@ public void widgetSelected(SelectionEvent e) { }); setGridData(fSelectAllButton, SWT.FILL, true, SWT.FILL, false); - new Label(panel, SWT.NONE); // filler + @SuppressWarnings("unused") + Label filler = new Label(panel, SWT.NONE); // filler fReplaceFindButton = makeButton(panel, FindReplaceMessages.FindReplace_ReplaceFindButton_label, 103, false, new SelectionAdapter() { diff --git a/tests/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionTest.java b/tests/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionTest.java index 3e69f6fa451..0fa99f0be9e 100644 --- a/tests/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionTest.java +++ b/tests/org.eclipse.text.tests/src/org/eclipse/text/tests/link/LinkedPositionTest.java @@ -39,13 +39,16 @@ public void setUp() { @Test public void testCreate() { - new LinkedPosition(fDoc, 1, 9); - new LinkedPosition(new Document(), 123, 234); + @SuppressWarnings("unused") + LinkedPosition linkedPosition = new LinkedPosition(fDoc, 1, 9); + @SuppressWarnings("unused") + LinkedPosition linkedPosition2 = new LinkedPosition(new Document(), 123, 234); } @Test(expected=Throwable.class) public void testNullCreate() { - new LinkedPosition(null, 1, 9); + @SuppressWarnings("unused") + LinkedPosition linkedPosition = new LinkedPosition(null, 1, 9); } /* diff --git a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/revisions/ChangeRegionTest.java b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/revisions/ChangeRegionTest.java index 6fbff840027..a74c27a2d7b 100644 --- a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/revisions/ChangeRegionTest.java +++ b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/revisions/ChangeRegionTest.java @@ -13,7 +13,9 @@ *******************************************************************************/ package org.eclipse.ui.workbench.texteditor.tests.revisions; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.util.Date; import java.util.List; @@ -68,19 +70,22 @@ public void setUp() throws Exception { @Test public void testCreation() throws Exception { try { - new ChangeRegion(fRevision, null); + @SuppressWarnings("unused") + ChangeRegion changeRegion= new ChangeRegion(fRevision, null); fail(); } catch (Exception e) { } try { - new ChangeRegion(null, new LineRange(12, 3)); + @SuppressWarnings("unused") + ChangeRegion changeRegion= new ChangeRegion(null, new LineRange(12, 3)); fail(); } catch (Exception e) { } try { - new ChangeRegion(null, null); + @SuppressWarnings("unused") + ChangeRegion changeRegion= new ChangeRegion(null, null); fail(); } catch (Exception e) { }