Skip to content

Commit 44356af

Browse files
authored
Code mining: handle Record classes like normal classes (#1822)
also 1 Test was added which tests the bugfix
1 parent 9887b92 commit 44356af

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/codemining/ParameterNamesCodeMiningTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,27 @@ public void testBug549126() throws Exception {
380380
assertEquals(2, fParameterNameCodeMiningProvider.provideCodeMinings(viewer, new NullProgressMonitor()).get().size());
381381
}
382382

383+
@Test
384+
public void testRecordConstructorOneParameter() throws Exception {
385+
String contents= """
386+
public class Ant {
387+
record Ca (int size){
388+
389+
}
390+
391+
Ca c = new Ca(0);
392+
393+
}
394+
""";
395+
ICompilationUnit compilationUnit= fPackage.createCompilationUnit("Ant.java", contents, true, new NullProgressMonitor());
396+
JavaEditor editor= (JavaEditor) EditorUtility.openInEditor(compilationUnit);
397+
fParameterNameCodeMiningProvider.setContext(editor);
398+
JavaSourceViewer viewer= (JavaSourceViewer)editor.getViewer();
399+
waitReconciled(viewer);
400+
401+
assertEquals(0, fParameterNameCodeMiningProvider.provideCodeMinings(viewer, new NullProgressMonitor()).get().size());
402+
}
403+
383404
@Test
384405
public void testRecordConstructorOK() throws Exception {
385406
String contents= "import java.util.Map;\n"

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/CalleeJavaMethodParameterVisitor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected void collectParameterNamesCodeMinings(IMethod method, List<?> argument
129129

130130
protected void collectParameterNamesCodeMinings(IMethodBinding mbinding, List<?> arguments, boolean isVarArgs) {
131131
// synthetic method of a record
132-
if (mbinding.getDeclaringClass().isRecord()) {
132+
if (mbinding.getDeclaringClass().isRecord() && !skipParameterNamesCodeMinings(mbinding)) {
133133
String[] parameterNames= mbinding.getParameterNames();
134134
for (int i= 0; i < Math.min(arguments.size(), parameterNames.length); i++) {
135135
if (!skipParameterNameCodeMining(parameterNames, arguments, i)) {
@@ -168,6 +168,10 @@ private boolean skipParameterNamesCodeMinings(IMethod method) {
168168
return method.getNumberOfParameters() <= 1;
169169
}
170170

171+
private boolean skipParameterNamesCodeMinings(IMethodBinding methodBinding) {
172+
return methodBinding.getParameterNames().length <= 1;
173+
}
174+
171175
private boolean skipParameterNamesCodeMinings(IMethod method, String[] parameterNames) {
172176
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
173177
boolean filter= store.getBoolean(PreferenceConstants.EDITOR_JAVA_CODEMINING_DEFAULT_FILTER_FOR_PARAMETER_NAMES);

0 commit comments

Comments
 (0)