[javadoc] do not add @param tag if no param tags were present before (IDEA-233885)

GitOrigin-RevId: 78226bdcf86b24dfa92358ae5240139b4dd199d7
This commit is contained in:
Anna Kozlova
2021-12-08 14:52:30 +01:00
committed by intellij-monorepo-bot
parent 775f470ab2
commit 9aa8d7ff80
5 changed files with 65 additions and 110 deletions

View File

@@ -1031,48 +1031,51 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
private static void fixJavadocsForChangedMethod(final PsiMethod method, final JavaChangeInfo changeInfo, int newParamsLength) throws IncorrectOperationException {
final PsiParameter[] parameters = method.getParameterList().getParameters();
final JavaParameterInfo[] newParms = changeInfo.getNewParameters();
LOG.assertTrue(parameters.length <= newParamsLength);
final Set<PsiParameter> newParameters = new HashSet<>();
final String[] oldParameterNames = changeInfo.getOldParameterNames();
for (int i = 0; i < newParamsLength; i++) {
JavaParameterInfo newParm = newParms[i];
if (newParm.getOldIndex() < 0 ||
newParm.getOldIndex() == i && !(newParm.getName().equals(oldParameterNames[newParm.getOldIndex()]) && newParm.getTypeText().equals(changeInfo.getOldParameterTypes()[newParm.getOldIndex()]))) {
newParameters.add(parameters[i]);
PsiClass aClass = method.getContainingClass();
if (aClass == null) return;
PsiDocComment methodDocComment = method.getDocComment();
PsiDocComment classDocComment = aClass.getDocComment();
if (changeInfo.isParameterSetOrOrderChanged() || changeInfo.isParameterNamesChanged()) {
final JavaParameterInfo[] newParms = changeInfo.getNewParameters();
LOG.assertTrue(parameters.length <= newParamsLength);
final Set<PsiParameter> newParameters = new HashSet<>();
final String[] oldParameterNames = changeInfo.getOldParameterNames();
for (int i = 0; i < newParamsLength; i++) {
JavaParameterInfo newParm = newParms[i];
if (newParm.getOldIndex() < 0 ||
newParm.getOldIndex() == i && !(newParm.getName().equals(oldParameterNames[newParm.getOldIndex()]) && newParm.getTypeText().equals(changeInfo.getOldParameterTypes()[newParm.getOldIndex()]))) {
newParameters.add(parameters[i]);
}
}
Condition<Pair<PsiParameter, String>> eqCondition = pair -> {
final PsiParameter parameter = pair.first;
final String oldParamName = pair.second;
final int oldIdx = ArrayUtil.find(oldParameterNames, oldParamName);
int newIndex = method.getParameterList().getParameterIndex(parameter);
return oldIdx >= 0 && newIndex >= 0 && changeInfo.getNewParameters()[newIndex].getOldIndex() == oldIdx;
};
Condition<String> matchedToOldParam = paramName -> ArrayUtil.find(oldParameterNames, paramName) >= 0;
if (!(method instanceof SyntheticElement) && methodDocComment != null && methodDocComment.findTagByName("param") != null) {
RefactoringUtil.fixJavadocsForParams(method, methodDocComment, newParameters, eqCondition, matchedToOldParam);
}
if (JavaPsiRecordUtil.isCanonicalConstructor(method) && classDocComment != null && classDocComment.findTagByName("param") != null) {
RefactoringUtil.fixJavadocsForParams(method, classDocComment, newParameters, eqCondition, matchedToOldParam);
}
}
Condition<Pair<PsiParameter, String>> eqCondition = pair -> {
final PsiParameter parameter = pair.first;
final String oldParamName = pair.second;
final int oldIdx = ArrayUtil.find(oldParameterNames, oldParamName);
int newIndex = method.getParameterList().getParameterIndex(parameter);
return oldIdx >= 0 && newIndex >= 0 && changeInfo.getNewParameters()[newIndex].getOldIndex() == oldIdx;
};
Condition<String> matchedToOldParam = paramName -> ArrayUtil.find(oldParameterNames, paramName) >= 0;
if (!(method instanceof SyntheticElement)) {
RefactoringUtil.fixJavadocsForParams(method, newParameters, eqCondition, matchedToOldParam);
}
PsiClass aClass = method.getContainingClass();
if (aClass != null && JavaPsiRecordUtil.isCanonicalConstructor(method)) {
RefactoringUtil.fixJavadocsForParams(method, aClass.getDocComment(), newParameters, eqCondition, matchedToOldParam);
}
if (changeInfo.isReturnTypeChanged()) {
PsiDocComment docComment = method.getDocComment();
if (docComment != null) {
CanonicalTypes.Type type = changeInfo.getNewReturnType();
PsiDocTag aReturn = docComment.findTagByName("return");
if (PsiType.VOID.equalsToText(type.getTypeText())) {
if (aReturn != null) {
aReturn.delete();
}
if (changeInfo.isReturnTypeChanged() && methodDocComment != null) {
CanonicalTypes.Type type = changeInfo.getNewReturnType();
PsiDocTag aReturn = methodDocComment.findTagByName("return");
if (PsiType.VOID.equalsToText(type.getTypeText())) {
if (aReturn != null) {
aReturn.delete();
}
else {
String oldReturnType = changeInfo.getOldReturnType();
if (aReturn == null && oldReturnType != null && PsiType.VOID.equalsToText(oldReturnType)) {
docComment.add(JavaPsiFacade.getElementFactory(method.getProject()).createDocTagFromText("@return"));
}
}
else {
String oldReturnType = changeInfo.getOldReturnType();
if (aReturn == null && oldReturnType != null && PsiType.VOID.equalsToText(oldReturnType)) {
methodDocComment.add(JavaPsiFacade.getElementFactory(method.getProject()).createDocTagFromText("@return"));
}
}
}

View File

@@ -0,0 +1,9 @@
class X {
public class TestRefactorLink {
/**
* Description
*/
public void <caret>mymethod() { }
}
}

View File

@@ -0,0 +1,9 @@
class X {
public class TestRefactorLink {
/**
* Description
*/
public void mymethod(boolean a) { }
}
}

View File

@@ -216,6 +216,12 @@ public class ChangeSignatureTest extends ChangeSignatureBaseTest {
}, false);
}
public void testParamJavadoc4() {
doTest(null, new ParameterInfoImpl[]{
ParameterInfoImpl.createNew().withName("a").withType(PsiType.BOOLEAN),
}, false);
}
public void testParamJavadocRenamedReordered() {
doTest(null, new ParameterInfoImpl[]{
ParameterInfoImpl.create(0).withName("a").withType(PsiType.BOOLEAN),

View File

@@ -1,72 +0,0 @@
/*
* Copyright 2000-2017 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.
*/
package com.intellij.java.refactoring;
import com.intellij.FileSetTestCase;
import com.intellij.psi.*;
import com.intellij.refactoring.util.RefactoringUtil;
import junit.framework.Test;
import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* @author dsl
*/
public abstract class FixMethodJavadocTest extends FileSetTestCase {
FixMethodJavadocTest() {
super(findPath());
}
private static String findPath() {
final URL res = FixMethodJavadocTest.class.getResource("/" + FixMethodJavadocTest.class.getName().replace('.', '/') + ".class");
File f = new File(res.getFile());
String result = f.getParent() + File.separatorChar + "methodJavaDocData";
result = result.replaceAll("classes", "");
return result;
}
@Override
public String transform(String testName, String[] data) {
final PsiManager manager = PsiManager.getInstance(myProject);
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(manager.getProject());
final PsiMethod method = factory.createMethodFromText(data[0], null);
final HashSet<PsiParameter> newParameters = new HashSet<>();
if (data.length == 2) {
final String[] strings = data[1].split("\\s+");
collectNewParameters(method, strings, newParameters);
}
RefactoringUtil.fixJavadocsForParams(method, newParameters);
return method.getText();
}
private void collectNewParameters(PsiMethod method, String[] names, Set<PsiParameter> newParameters) {
Set<String> newNames = new HashSet<>(Arrays.asList(names));
final PsiParameter[] parameters = method.getParameterList().getParameters();
for (PsiParameter parameter : parameters) {
if (newNames.contains(parameter.getName())) {
newParameters.add(parameter);
}
}
}
public static Test suite() {
return new FixMethodJavadocTest(){};
}
}