diff --git a/java/java-analysis-impl/src/com/siyeh/ig/fixes/ConvertToVarargsMethodFix.java b/java/java-analysis-impl/src/com/siyeh/ig/fixes/ConvertToVarargsMethodFix.java index 4ab33fb8f559..33213846e808 100644 --- a/java/java-analysis-impl/src/com/siyeh/ig/fixes/ConvertToVarargsMethodFix.java +++ b/java/java-analysis-impl/src/com/siyeh/ig/fixes/ConvertToVarargsMethodFix.java @@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.search.searches.ReferencesSearch; import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.psiutils.CommentTracker; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -56,7 +57,10 @@ public class ConvertToVarargsMethodFix extends PsiUpdateModCommandQuickFix { final PsiTypeElement newTypeElement = factory.createTypeElement(ellipsisType); final PsiTypeElement typeElement = lastParameter.getTypeElement(); if (typeElement != null) { - typeElement.replace(newTypeElement); + CommentTracker ct = new CommentTracker(); + ct.grabComments(typeElement); + PsiElement result = typeElement.replace(newTypeElement); + ct.insertCommentsBefore(result.getLastChild()); // Swap comments, example: String /* Foo */ [] -> String/* Foo */... } } diff --git a/java/java-tests/testData/ig/com/siyeh/igfixes/migration/convert_to_varargs_method/Comment.after.java b/java/java-tests/testData/ig/com/siyeh/igfixes/migration/convert_to_varargs_method/Comment.after.java new file mode 100644 index 000000000000..c736f5691d76 --- /dev/null +++ b/java/java-tests/testData/ig/com/siyeh/igfixes/migration/convert_to_varargs_method/Comment.after.java @@ -0,0 +1,4 @@ +class Test { + public void foo(/* Before */ String/* Middle */... arg) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/ig/com/siyeh/igfixes/migration/convert_to_varargs_method/Comment.java b/java/java-tests/testData/ig/com/siyeh/igfixes/migration/convert_to_varargs_method/Comment.java new file mode 100644 index 000000000000..fdcf5f0388d2 --- /dev/null +++ b/java/java-tests/testData/ig/com/siyeh/igfixes/migration/convert_to_varargs_method/Comment.java @@ -0,0 +1,4 @@ +class Test { + public void foo(/* Before */ String /* Middle */ [] arg) { + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/siyeh/ig/fixes/migration/ConvertToVarargsMethodFixTest.java b/java/java-tests/testSrc/com/siyeh/ig/fixes/migration/ConvertToVarargsMethodFixTest.java index 3b950be4ac8b..5fa0c23d7c9c 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/fixes/migration/ConvertToVarargsMethodFixTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/fixes/migration/ConvertToVarargsMethodFixTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.siyeh.ig.fixes.migration; import com.siyeh.InspectionGadgetsBundle; @@ -25,6 +11,7 @@ import com.siyeh.ig.migration.MethodCanBeVariableArityMethodInspection; public class ConvertToVarargsMethodFixTest extends IGQuickFixesTestCase { public void testDeepArray() { doTest(); } + public void testComment() { doTest(); } public void testFinal() { doTest(); } public void testCStyle() { doTest(); }