diff --git a/org.eclipse.jdt.bcoview/pom.xml b/org.eclipse.jdt.bcoview/pom.xml index acb05a03870..1d8ea8a1408 100644 --- a/org.eclipse.jdt.bcoview/pom.xml +++ b/org.eclipse.jdt.bcoview/pom.xml @@ -14,7 +14,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt org.eclipse.jdt.bcoview diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/RenameUnusedVariableCleanUpCore.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/RenameUnusedVariableCleanUpCore.java index f65c9f59a8a..f2e6a63809d 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/RenameUnusedVariableCleanUpCore.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/RenameUnusedVariableCleanUpCore.java @@ -26,14 +26,11 @@ import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.CompilationUnit; -import org.eclipse.jdt.core.dom.Pattern; import org.eclipse.jdt.core.dom.SimpleName; -import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore; import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; import org.eclipse.jdt.ui.cleanup.ICleanUpFix; @@ -150,9 +147,7 @@ public int computeNumberOfFixes(CompilationUnit compilationUnit) { if (id == IProblem.LocalVariableIsNeverUsed) { ProblemLocation location= new ProblemLocation(problem); SimpleName name= UnusedCodeFixCore.getUnusedName(compilationUnit, location); - if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject()) && - name.getParent() instanceof SingleVariableDeclaration nameParent && - nameParent.getParent() instanceof Pattern) { + if (RenameUnusedVariableFixCore.canRenameToUnnamedVariable(compilationUnit, name)) { result++; } } else if (id == IProblem.LambdaParameterIsNeverUsed) { diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UnusedCodeCleanUpCore.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UnusedCodeCleanUpCore.java index 5dfcc4bfa6f..21b0f6e0545 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UnusedCodeCleanUpCore.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UnusedCodeCleanUpCore.java @@ -26,13 +26,11 @@ import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.CompilationUnit; -import org.eclipse.jdt.core.dom.Pattern; import org.eclipse.jdt.core.dom.SimpleName; -import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; +import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore; import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; import org.eclipse.jdt.ui.cleanup.ICleanUpFix; @@ -277,9 +275,7 @@ public int computeNumberOfFixes(CompilationUnit compilationUnit) { if (id == IProblem.LocalVariableIsNeverUsed) { ProblemLocation location= new ProblemLocation(problem); SimpleName name= UnusedCodeFixCore.getUnusedName(compilationUnit, location); - if (!JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject()) || - !(name.getParent() instanceof SingleVariableDeclaration nameParent) || - !(nameParent.getParent() instanceof Pattern)) { + if (!RenameUnusedVariableFixCore.canRenameToUnnamedVariable(compilationUnit, name)) { result++; } } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/RenameUnusedVariableFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/RenameUnusedVariableFixCore.java index ea84dca4965..b7adc600cb9 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/RenameUnusedVariableFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/RenameUnusedVariableFixCore.java @@ -28,6 +28,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.EnhancedForStatement; +import org.eclipse.jdt.core.dom.ForStatement; import org.eclipse.jdt.core.dom.IBinding; import org.eclipse.jdt.core.dom.IVariableBinding; import org.eclipse.jdt.core.dom.LambdaExpression; @@ -35,10 +36,11 @@ import org.eclipse.jdt.core.dom.Pattern; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.TryStatement; +import org.eclipse.jdt.core.dom.VariableDeclarationExpression; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; -import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; @@ -78,15 +80,10 @@ public static RenameUnusedVariableFixCore createRenameToUnnamedFix(CompilationUn if (name != null) { IBinding binding= name.resolveBinding(); if (binding != null) { - if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject())) { - if (name.getParent() instanceof SingleVariableDeclaration parent && - parent.getParent() instanceof Pattern || - name.getParent() instanceof VariableDeclarationFragment parent2 && - parent2.getParent() instanceof LambdaExpression) { - String label= FixMessages.UnusedCodeFix_RenameToUnnamedVariable_description; - RenameToUnnamedVariableOperation operation= new RenameToUnnamedVariableOperation(name); - return new RenameUnusedVariableFixCore(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, getCleanUpOptions()); - } + if (canRenameToUnnamedVariable(compilationUnit, name)) { + String label= FixMessages.UnusedCodeFix_RenameToUnnamedVariable_description; + RenameToUnnamedVariableOperation operation= new RenameToUnnamedVariableOperation(name); + return new RenameUnusedVariableFixCore(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, getCleanUpOptions()); } } } @@ -131,16 +128,8 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble if (name != null) { IBinding binding= name.resolveBinding(); if (binding instanceof IVariableBinding) { - VariableDeclarationFragment parent= ASTNodes.getParent(name, VariableDeclarationFragment.class); - if (parent == null || id == IProblem.LambdaParameterIsNeverUsed) { - if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject())) { - if (name.getParent() instanceof SingleVariableDeclaration nameParent && - nameParent.getParent() instanceof Pattern || - name.getParent() instanceof VariableDeclarationFragment varFragment && - varFragment.getParent() instanceof LambdaExpression) { - result.add(new RenameToUnnamedVariableOperation(name)); - } - } + if (canRenameToUnnamedVariable(compilationUnit, name)) { + result.add(new RenameToUnnamedVariableOperation(name)); } } } @@ -153,8 +142,23 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble return new RenameUnusedVariableFixCore(FixMessages.UnusedCodeFix_change_name, compilationUnit, result.toArray(new CompilationUnitRewriteOperation[result.size()])); } - public static boolean isFormalParameterInEnhancedForStatement(SimpleName name) { - return name.getParent() instanceof SingleVariableDeclaration && name.getParent().getLocationInParent() == EnhancedForStatement.PARAMETER_PROPERTY; + public static boolean canRenameToUnnamedVariable(CompilationUnit compilationUnit, SimpleName name) { + if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + if (name.getParent() instanceof SingleVariableDeclaration nameParent) { + if (nameParent.getParent() instanceof Pattern || nameParent.getParent() instanceof EnhancedForStatement) { + return true; + } + } else if (name.getParent() instanceof VariableDeclarationFragment varFragment) { + if (varFragment.getParent() instanceof LambdaExpression) { + return true; + } else if (varFragment.getParent() instanceof VariableDeclarationExpression varFragmentParent) { + if (varFragmentParent.getParent() instanceof TryStatement || varFragmentParent.getParent() instanceof ForStatement) { + return true; + } + } + } + } + return false; } public static SimpleName getUnusedName(CompilationUnit compilationUnit, IProblemLocation problem) { diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFixCore.java index 2d87696ef2d..21770ea24fe 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFixCore.java @@ -65,7 +65,6 @@ import org.eclipse.jdt.core.dom.Modifier; import org.eclipse.jdt.core.dom.NodeFinder; import org.eclipse.jdt.core.dom.ParenthesizedExpression; -import org.eclipse.jdt.core.dom.Pattern; import org.eclipse.jdt.core.dom.PostfixExpression; import org.eclipse.jdt.core.dom.PrefixExpression; import org.eclipse.jdt.core.dom.QualifiedName; @@ -90,7 +89,6 @@ import org.eclipse.jdt.internal.corext.dom.ReplaceRewrite; import org.eclipse.jdt.internal.corext.dom.StatementRewrite; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.cleanup.CleanUpOptions; @@ -930,7 +928,7 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble if ((removeUnusedLocalVariables && id == IProblem.LocalVariableIsNeverUsed) || (removeUnusedPrivateFields && id == IProblem.UnusedPrivateField)) { SimpleName name= getUnusedName(compilationUnit, problem); - if (name != null) { + if (name != null && !RenameUnusedVariableFixCore.canRenameToUnnamedVariable(compilationUnit, name)) { IBinding binding= name.resolveBinding(); if (binding instanceof IVariableBinding && !isFormalParameterInEnhancedForStatement(name) && (!((IVariableBinding) binding).isField() || isSideEffectFree(name, compilationUnit))) { VariableDeclarationFragment parent= ASTNodes.getParent(name, VariableDeclarationFragment.class); @@ -941,16 +939,7 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble } variableDeclarations.get(varDecl).add(name); } else { - if (id == IProblem.LocalVariableIsNeverUsed) { - SimpleName nameNode= UnusedCodeFixCore.getUnusedName(compilationUnit, problem); - if (!JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject()) || - !(nameNode.getParent() instanceof SingleVariableDeclaration nameParent) || - !(nameParent.getParent() instanceof Pattern)) { - result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false)); - } - } else { - result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false)); - } + result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false)); } } } diff --git a/org.eclipse.jdt.junit.runtime/pom.xml b/org.eclipse.jdt.junit.runtime/pom.xml index 2ed1065d40a..d1add87a0e4 100644 --- a/org.eclipse.jdt.junit.runtime/pom.xml +++ b/org.eclipse.jdt.junit.runtime/pom.xml @@ -14,7 +14,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt org.eclipse.jdt.junit.runtime diff --git a/org.eclipse.jdt.junit/META-INF/MANIFEST.MF b/org.eclipse.jdt.junit/META-INF/MANIFEST.MF index 03b2f8832c5..6d7694d24c7 100644 --- a/org.eclipse.jdt.junit/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.junit/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.jdt.junit Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.jdt.junit;singleton:=true -Bundle-Version: 3.16.600.qualifier +Bundle-Version: 3.16.700.qualifier Bundle-Activator: org.eclipse.jdt.internal.junit.ui.JUnitPlugin Bundle-ActivationPolicy: lazy Bundle-Vendor: %providerName diff --git a/org.eclipse.jdt.junit/icons/full/etool16/run_exc.png b/org.eclipse.jdt.junit/icons/full/etool16/run_exc.png index 08571c1f22e..6c3191d6965 100644 Binary files a/org.eclipse.jdt.junit/icons/full/etool16/run_exc.png and b/org.eclipse.jdt.junit/icons/full/etool16/run_exc.png differ diff --git a/org.eclipse.jdt.junit/icons/full/etool16/run_exc@2x.png b/org.eclipse.jdt.junit/icons/full/etool16/run_exc@2x.png index 156df21c3f4..2fa5cf5c6c6 100644 Binary files a/org.eclipse.jdt.junit/icons/full/etool16/run_exc@2x.png and b/org.eclipse.jdt.junit/icons/full/etool16/run_exc@2x.png differ diff --git a/org.eclipse.jdt.junit4.runtime/pom.xml b/org.eclipse.jdt.junit4.runtime/pom.xml index 82b597ed832..635a3a084ce 100644 --- a/org.eclipse.jdt.junit4.runtime/pom.xml +++ b/org.eclipse.jdt.junit4.runtime/pom.xml @@ -14,7 +14,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt org.eclipse.jdt.junit4.runtime diff --git a/org.eclipse.jdt.junit5.runtime/pom.xml b/org.eclipse.jdt.junit5.runtime/pom.xml index 9dc9f8a02dc..a1005dfe022 100644 --- a/org.eclipse.jdt.junit5.runtime/pom.xml +++ b/org.eclipse.jdt.junit5.runtime/pom.xml @@ -14,7 +14,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt org.eclipse.jdt.junit5.runtime diff --git a/org.eclipse.jdt.text.tests/pom.xml b/org.eclipse.jdt.text.tests/pom.xml index b0fb8632da0..67c19fdd529 100644 --- a/org.eclipse.jdt.text.tests/pom.xml +++ b/org.eclipse.jdt.text.tests/pom.xml @@ -15,7 +15,7 @@ tests-pom eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT ../tests-pom/ org.eclipse.jdt diff --git a/org.eclipse.jdt.ui.examples.projects/pom.xml b/org.eclipse.jdt.ui.examples.projects/pom.xml index ee337f075e6..c7a04b10965 100644 --- a/org.eclipse.jdt.ui.examples.projects/pom.xml +++ b/org.eclipse.jdt.ui.examples.projects/pom.xml @@ -11,7 +11,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt org.eclipse.jdt.ui.examples.projects diff --git a/org.eclipse.jdt.ui.junit.sampleproject/pom.xml b/org.eclipse.jdt.ui.junit.sampleproject/pom.xml index 67a70d1c649..4f6b87738cc 100644 --- a/org.eclipse.jdt.ui.junit.sampleproject/pom.xml +++ b/org.eclipse.jdt.ui.junit.sampleproject/pom.xml @@ -19,7 +19,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt.ui org.eclipse.jdt.ui.junit.sampleproject diff --git a/org.eclipse.jdt.ui.tests.refactoring/pom.xml b/org.eclipse.jdt.ui.tests.refactoring/pom.xml index f64248519cd..ea744d4d55d 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/pom.xml +++ b/org.eclipse.jdt.ui.tests.refactoring/pom.xml @@ -14,7 +14,7 @@ tests-pom eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT ../tests-pom/ org.eclipse.jdt diff --git a/org.eclipse.jdt.ui.tests/leaks/org/eclipse/jdt/ui/leaktest/reftracker/ReferenceTracker.java b/org.eclipse.jdt.ui.tests/leaks/org/eclipse/jdt/ui/leaktest/reftracker/ReferenceTracker.java index cf8ac221d6c..49daa9fd2b3 100644 --- a/org.eclipse.jdt.ui.tests/leaks/org/eclipse/jdt/ui/leaktest/reftracker/ReferenceTracker.java +++ b/org.eclipse.jdt.ui.tests/leaks/org/eclipse/jdt/ui/leaktest/reftracker/ReferenceTracker.java @@ -81,6 +81,11 @@ private void followFieldReference(ReferencedObject ref, Object curr, Field fld) if (fieldVal != null) { fQueue.add(new ReferencedFieldElement(ref, fld, fieldVal)); } + } catch (IllegalArgumentException e) { + // XXX workaround for error in JDK: https://bugs.openjdk.org/browse/JDK-8337622 + if (!"Can not get final java.lang.Class field java.lang.Class.componentType on java.lang.Class".equals(e.getMessage())) { + throw e; + } } catch (IllegalAccessException e) { handleError(e, fld); } diff --git a/org.eclipse.jdt.ui.tests/pom.xml b/org.eclipse.jdt.ui.tests/pom.xml index 5cb5abdc32a..a08ead2af49 100644 --- a/org.eclipse.jdt.ui.tests/pom.xml +++ b/org.eclipse.jdt.ui.tests/pom.xml @@ -15,7 +15,7 @@ tests-pom eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT ../tests-pom/ org.eclipse.jdt diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java index 9a634ca7551..a6b1707192c 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 IBM Corporation and others. + * Copyright (c) 2019, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -126,7 +126,7 @@ enum Day { public void testSplitSwitchCaseLabelRuleStatement() throws Exception { fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); fJProject1.setRawClasspath(projectSetup.getDefaultClasspath(), null); - JavaProjectHelper.set12CompilerOptions(fJProject1, true); + JavaProjectHelper.set14CompilerOptions(fJProject1, true); fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); String str= """ diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java index 2eb15307d38..c9f54966ee4 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java @@ -173,4 +173,161 @@ case R(_, long _) -> {} assertEqualString(preview1, expected1); } + @Test + public void testRenameToUnnamedProposal3() throws Exception { + fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); + fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); + JavaProjectHelper.set22CompilerOptions(fJProject1); + + fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); + IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); + + String test= """ + package test; + + public class Unused { + + public static void main(String[] args) { + for (String arg : args) { + System.out.println("abc"); + } + } + } + """; + + ICompilationUnit cu= pack1.createCompilationUnit("Unused.java", test, false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot, 1); + assertCorrectLabels(proposals); + + CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0); + String preview1= getPreviewContent(proposal1); + + String expected1= """ + package test; + + public class Unused { + + public static void main(String[] args) { + for (String _ : args) { + System.out.println("abc"); + } + } + } + """; + + assertEqualString(preview1, expected1); + } + + @Test + public void testRenameToUnnamedProposal4() throws Exception { + fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); + fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); + JavaProjectHelper.set22CompilerOptions(fJProject1); + + fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); + IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); + + String test= """ + package test; + import java.io.FileInputStream; + + public class Unused { + + public static void main(String[] args) { + try (FileInputStream x = new FileInputStream("a.b")) { + System.out.println("abc"); + } catch (Exception e) { + } + } + } + """; + + ICompilationUnit cu= pack1.createCompilationUnit("Unused.java", test, false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot, 1); + assertCorrectLabels(proposals); + + CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0); + String preview1= getPreviewContent(proposal1); + + String expected1= """ + package test; + import java.io.FileInputStream; + + public class Unused { + + public static void main(String[] args) { + try (FileInputStream _ = new FileInputStream("a.b")) { + System.out.println("abc"); + } catch (Exception e) { + } + } + } + """; + + assertEqualString(preview1, expected1); + } + + @Test + public void testRenameToUnnamedProposal5() throws Exception { + fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); + fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); + JavaProjectHelper.set22CompilerOptions(fJProject1); + + fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); + IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); + + String test= """ + package test; + + public class Unused { + + private static int x = 1; + + private static int sideEffect() { + return ++x; + } + + public static void main(String[] args) { + for (int i = 0, se = sideEffect(); i < 9; ++i) { + System.out.println("abc"); + } + } + } + """; + + ICompilationUnit cu= pack1.createCompilationUnit("Unused.java", test, false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot, 1); + assertCorrectLabels(proposals); + + CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0); + String preview1= getPreviewContent(proposal1); + + String expected1= """ + package test; + + public class Unused { + + private static int x = 1; + + private static int sideEffect() { + return ++x; + } + + public static void main(String[] args) { + for (int i = 0, _ = sideEffect(); i < 9; ++i) { + System.out.println("abc"); + } + } + } + """; + + assertEqualString(preview1, expected1); + } + } diff --git a/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml b/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml index a45a9707e4b..0c3e3c88625 100644 --- a/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml +++ b/org.eclipse.jdt.ui.unittest.junit.feature/pom.xml @@ -16,7 +16,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt.feature org.eclipse.jdt.ui.unittest.junit.feature diff --git a/org.eclipse.jdt.ui.unittest.junit/pom.xml b/org.eclipse.jdt.ui.unittest.junit/pom.xml index a2b2b17e890..05ed1f45a55 100644 --- a/org.eclipse.jdt.ui.unittest.junit/pom.xml +++ b/org.eclipse.jdt.ui.unittest.junit/pom.xml @@ -14,7 +14,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt org.eclipse.jdt.ui.unittest.junit diff --git a/org.eclipse.jdt.ui/pom.xml b/org.eclipse.jdt.ui/pom.xml index e589abb74e8..fffaedcc7ea 100644 --- a/org.eclipse.jdt.ui/pom.xml +++ b/org.eclipse.jdt.ui/pom.xml @@ -14,7 +14,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT org.eclipse.jdt org.eclipse.jdt.ui diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java index 60f2620d9f8..57fa5ef0d9b 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java @@ -58,6 +58,7 @@ import org.eclipse.jdt.internal.ui.search.SearchMessages; import org.eclipse.jdt.internal.ui.text.JavaWordFinder; +import org.eclipse.jdt.internal.ui.util.SelectionUtil; /** @@ -75,7 +76,6 @@ public class JavaElementHyperlinkDetector extends AbstractHyperlinkDetector { private static IRegion fLastWordRegion; private static IJavaElement[] fLastElements; - /* * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean) */ @@ -99,14 +99,13 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) { if (input == null) return null; + IDocumentProvider documentProvider= textEditor.getDocumentProvider(); + IEditorInput editorInput= textEditor.getEditorInput(); + IDocument document= documentProvider.getDocument(editorInput); + IRegion wordRegion= JavaWordFinder.findWord(document, offset); + if (wordRegion == null || wordRegion.getLength() == 0) + return null; try { - IDocumentProvider documentProvider= textEditor.getDocumentProvider(); - IEditorInput editorInput= textEditor.getEditorInput(); - IDocument document= documentProvider.getDocument(editorInput); - IRegion wordRegion= JavaWordFinder.findWord(document, offset); - if (wordRegion == null || wordRegion.getLength() == 0) - return null; - if (isInheritDoc(document, wordRegion) && getClass() != JavaElementHyperlinkDetector.class) return null; @@ -143,7 +142,9 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) { return null; return CollectionsUtil.toArray(links, IHyperlink.class); - + } catch (RuntimeException e) { + SelectionUtil.logException("computing hyperlink", e, textEditor.getTitle(), document, offset); //$NON-NLS-1$ + return null; } catch (JavaModelException e) { return null; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java index 4c152b2ca60..d954c5cbcbf 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java @@ -17,7 +17,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Objects; import java.util.Set; import org.osgi.framework.Bundle; @@ -27,7 +26,6 @@ import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IContributor; import org.eclipse.core.runtime.IExtension; -import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.InvalidRegistryObjectException; @@ -35,11 +33,12 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; -import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jdt.core.ICompilationUnit; + import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.text.IJavaPartitions; @@ -49,6 +48,7 @@ import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; +import org.eclipse.jdt.internal.ui.util.SelectionUtil; /** * The description of an extension to the @@ -328,7 +328,6 @@ public IJavaCompletionProposalComputer createComputer() throws CoreException, In return (IJavaCompletionProposalComputer) fElement.createExecutableExtension(CLASS); } - private String lastErrorMsg; /** * Safely computes completion proposals through the described extension. If the extension * is disabled, throws an exception or otherwise does not adhere to the contract described in @@ -360,8 +359,8 @@ public List computeCompletionProposals(ContentAssistInvocat } } finally { // If computers are using non-ui thread, don't report delays. - fIsReportingDelay= !(context.getViewer() instanceof JavaSourceViewer) - || !((JavaSourceViewer) context.getViewer()).isAsyncCompletionActive(); + fIsReportingDelay= !(context.getViewer() instanceof JavaSourceViewer viewer) + || !viewer.isAsyncCompletionActive(); } status= createAPIViolationStatus(COMPUTE_COMPLETION_PROPOSALS); } catch (InvalidRegistryObjectException x) { @@ -370,25 +369,8 @@ public List computeCompletionProposals(ContentAssistInvocat status= createExceptionStatus(x); } catch (RuntimeException x) { // log error and keep going with other providers - String where= ""; //$NON-NLS-1$ - if (context instanceof JavaContentAssistInvocationContext ctx) { - if (ctx.getCompilationUnit() != null) { - where+= ctx.getCompilationUnit().getElementName(); - } - } - where+= " at offset " + context.getInvocationOffset(); //$NON-NLS-1$ - try { - IDocument doc= context.getDocument(); - int lineOfOffset= doc.getLineOfOffset(context.getInvocationOffset()); - where+= " line " + (lineOfOffset + 1); //$NON-NLS-1$ - int CONTEXT_LINES= 10; - where+= " :\n" + doc.get(doc.getLineOffset(Math.max(0, lineOfOffset - CONTEXT_LINES)), context.getInvocationOffset()) + '|'; //$NON-NLS-1$ - } catch (BadLocationException e1) { - } - if (!Objects.equals(lastErrorMsg, where)) { // avoid repetitive logging - lastErrorMsg= where; - ILog.get().error("RuntimeException computing completion proposal for " + where, x); //$NON-NLS-1$ - } + String title= (context instanceof JavaContentAssistInvocationContext ctx) && (ctx.getCompilationUnit() instanceof ICompilationUnit cu) ? cu.getElementName() : null; + SelectionUtil.logException("computing completion proposal", x, title, context.getDocument(), context.getInvocationOffset()); //$NON-NLS-1$ status= createExceptionStatus(x); return Collections.emptyList(); } finally { diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java index b259b229dd2..f0911c4e0e7 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java @@ -40,6 +40,7 @@ import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; import org.eclipse.jdt.internal.ui.javaeditor.WorkingCopyManager; import org.eclipse.jdt.internal.ui.text.JavaWordFinder; +import org.eclipse.jdt.internal.ui.util.SelectionUtil; /** @@ -119,6 +120,9 @@ protected IJavaElement[] getJavaElementsAt(ITextViewer textViewer, IRegion hover if (resolve != null) { try { return resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength()); + } catch (RuntimeException e) { + SelectionUtil.logException("computing hover information", e, getEditor().getTitle(), document, hoverRegion.getOffset()); //$NON-NLS-1$ + return null; } catch (JavaModelException x) { return null; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java index c3aaa9a1e25..0c82d9ed6a9 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java @@ -17,6 +17,9 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; +import java.util.Objects; + +import org.eclipse.core.runtime.ILog; import org.eclipse.core.resources.IResource; @@ -24,6 +27,9 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; + import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartReference; @@ -121,4 +127,34 @@ public static void selectAndReveal(IResource[] resources, IWorkbenchWindow windo private SelectionUtil() { } + private static String lastErrorMsg; + + public static void logException(String action, RuntimeException e, String title, IDocument document, int offset) { + // log error and keep going + String errorMsg= e.getClass().getSimpleName() + " " + action; //$NON-NLS-1$ + if (title != null) { + errorMsg+= " in " + title; //$NON-NLS-1$ + } + errorMsg+= " at offset " + offset; //$NON-NLS-1$ + try { + int lineOfOffset= document.getLineOfOffset(offset); + String source= "Source line " + (lineOfOffset + 1); //$NON-NLS-1$ + int CONTEXT_LINES= 10; + int startLineOffset= document.getLineOffset(Math.max(0, lineOfOffset - CONTEXT_LINES)); + source+= " :"+System.lineSeparator(); //$NON-NLS-1$ + source+= "-----" + System.lineSeparator(); //$NON-NLS-1$ + source+= document.get(startLineOffset, offset - startLineOffset); // source until offset + source+= '|'; // cursor + source+= document.get(offset, document.getLineOffset(lineOfOffset) + document.getLineLength(lineOfOffset) - offset); // rest of line + source+= "-----"; //$NON-NLS-1$ + e.addSuppressed(new Throwable(source)); + } catch (BadLocationException ble) { + e.addSuppressed(ble); + } + if (!Objects.equals(lastErrorMsg, errorMsg)) { // avoid repetitive logging + lastErrorMsg= errorMsg; + ILog.get().error(errorMsg, e); + } + } + } diff --git a/pom.xml b/pom.xml index d3d04335132..91c86224321 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ org.eclipse eclipse-platform-parent - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT ../eclipse-platform-parent diff --git a/tests-pom/pom.xml b/tests-pom/pom.xml index 3366b4f9eed..901fcabb93e 100644 --- a/tests-pom/pom.xml +++ b/tests-pom/pom.xml @@ -14,7 +14,7 @@ eclipse.jdt.ui eclipse.jdt.ui - 4.34.0-SNAPSHOT + 4.35.0-SNAPSHOT tests-pom pom