From 4f9e796816fe925d532518432df0a8ac5c08cf7c Mon Sep 17 00:00:00 2001 From: jannisCode Date: Fri, 29 Nov 2024 12:39:34 +0100 Subject: [PATCH] Record classes are now handled the same as normal classes also 2 Tests were added which test the bugfix --- .../ParameterNamesCodeMiningTest.java | 41 +++++++++++++++++++ .../CalleeJavaMethodParameterVisitor.java | 6 ++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/codemining/ParameterNamesCodeMiningTest.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/codemining/ParameterNamesCodeMiningTest.java index 26326152458..9fa54e9ddff 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/codemining/ParameterNamesCodeMiningTest.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/codemining/ParameterNamesCodeMiningTest.java @@ -123,6 +123,47 @@ public class Foo { assertEquals(3, fParameterNameCodeMiningProvider.provideCodeMinings(viewer, new NullProgressMonitor()).get().size()); } +//note that this test will fail if every parameter is shown!!! + @Test + public void testRecordClasses() throws Exception { + String contents= """ + public class Ant { + record Ca (int size){ + + } + + Ca c = new Ca(0); + + } + """; + ICompilationUnit compilationUnit= fPackage.createCompilationUnit("Ant.java", contents, true, new NullProgressMonitor()); + JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(compilationUnit); + fParameterNameCodeMiningProvider.setContext(editor); + JavaSourceViewer viewer= (JavaSourceViewer)editor.getViewer(); + waitReconciled(viewer); + + assertEquals(0, fParameterNameCodeMiningProvider.provideCodeMinings(viewer, new NullProgressMonitor()).get().size()); + } + + @Test + public void testRecordClasses2() throws Exception { + String contents= """ + public class Ant { + record Ca (int size, int color){ + + } + + Ca c = new Ca(0,0); + + } + """; + ICompilationUnit compilationUnit= fPackage.createCompilationUnit("Ant.java", contents, true, new NullProgressMonitor()); + JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(compilationUnit); + fParameterNameCodeMiningProvider.setContext(editor); + JavaSourceViewer viewer= (JavaSourceViewer)editor.getViewer(); + waitReconciled(viewer); + assertEquals(2, fParameterNameCodeMiningProvider.provideCodeMinings(viewer, new NullProgressMonitor()).get().size()); + } @Test public void testVarargs() throws Exception { diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/CalleeJavaMethodParameterVisitor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/CalleeJavaMethodParameterVisitor.java index 11f14a89ef0..7f3adecb504 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/CalleeJavaMethodParameterVisitor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/CalleeJavaMethodParameterVisitor.java @@ -129,7 +129,7 @@ protected void collectParameterNamesCodeMinings(IMethod method, List argument protected void collectParameterNamesCodeMinings(IMethodBinding mbinding, List arguments, boolean isVarArgs) { // synthetic method of a record - if (mbinding.getDeclaringClass().isRecord()) { + if (mbinding.getDeclaringClass().isRecord() && !skipParameterNamesCodeMinings(mbinding)) { String[] parameterNames= mbinding.getParameterNames(); for (int i= 0; i < Math.min(arguments.size(), parameterNames.length); i++) { if (!skipParameterNameCodeMining(parameterNames, arguments, i)) { @@ -168,6 +168,10 @@ private boolean skipParameterNamesCodeMinings(IMethod method) { return method.getNumberOfParameters() <= 1; } + private boolean skipParameterNamesCodeMinings(IMethodBinding methodBinding) { + return methodBinding.getParameterNames().length <= 1; + } + private boolean skipParameterNamesCodeMinings(IMethod method, String[] parameterNames) { IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); boolean filter= store.getBoolean(PreferenceConstants.EDITOR_JAVA_CODEMINING_DEFAULT_FILTER_FOR_PARAMETER_NAMES);