fixed #858 Delombok produces duplicate @NonNull annotations on setters/getters

GitOrigin-RevId: 5dbe6a574c4daa08e3fdf17ad9a7c82374f8c583
This commit is contained in:
Michail Plushnikov
2020-09-13 12:48:26 +02:00
committed by intellij-monorepo-bot
parent cddebefd50
commit f73adab6db
6 changed files with 92 additions and 20 deletions

View File

@@ -3,6 +3,7 @@
<ol>
<li>Fixed #802: [Only for IntelliJ>=2020.2.2] val mis-infers an Optional(T) as Optional(Object) after map.</li>
<li>Fixed #826: Error if using @FieldNameConstants in switch case</li>
<li>Fixed #858: Delombok produces duplicate @NonNull annotations on setters/getters</li>
</ol>
</li>
<li>0.31

View File

@@ -3,6 +3,7 @@ package de.plushnikov.intellij.plugin.action.delombok;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.util.text.Strings;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
@@ -241,31 +242,21 @@ public class DelombokHandler {
resultMethod.getThrowsList().replace(elementFactory.createReferenceList(refs));
}
for (PsiParameter parameter : fromMethod.getParameterList().getParameters()) {
PsiParameter param = elementFactory.createParameter(parameter.getName(), parameter.getType());
final PsiModifierList parameterModifierList = parameter.getModifierList();
if (parameterModifierList != null) {
PsiModifierList modifierList = param.getModifierList();
for (PsiAnnotation originalAnnotation : parameterModifierList.getAnnotations()) {
final PsiAnnotation annotation = modifierList.addAnnotation(originalAnnotation.getQualifiedName());
for (PsiNameValuePair nameValuePair : originalAnnotation.getParameterList().getAttributes()) {
annotation.setDeclaredAttributeValue(nameValuePair.getName(), nameValuePair.getValue());
}
}
modifierList.setModifierProperty(PsiModifier.FINAL, parameterModifierList.hasModifierProperty(PsiModifier.FINAL));
for (PsiParameter fromParameter : fromMethod.getParameterList().getParameters()) {
PsiParameter toParameter = elementFactory.createParameter(fromParameter.getName(), fromParameter.getType());
final PsiModifierList fromParameterModifierList = fromParameter.getModifierList();
if (fromParameterModifierList != null) {
final PsiModifierList toParameterModifierList = toParameter.getModifierList();
copyAnnotations(fromParameterModifierList, toParameterModifierList);
toParameterModifierList.setModifierProperty(PsiModifier.FINAL, fromParameterModifierList.hasModifierProperty(PsiModifier.FINAL));
}
resultMethod.getParameterList().add(param);
resultMethod.getParameterList().add(toParameter);
}
final PsiModifierList fromMethodModifierList = fromMethod.getModifierList();
final PsiModifierList resultMethodModifierList = resultMethod.getModifierList();
copyModifiers(fromMethodModifierList, resultMethodModifierList);
for (PsiAnnotation psiAnnotation : fromMethodModifierList.getAnnotations()) {
final PsiAnnotation annotation = resultMethodModifierList.addAnnotation(psiAnnotation.getQualifiedName());
for (PsiNameValuePair nameValuePair : psiAnnotation.getParameterList().getAttributes()) {
annotation.setDeclaredAttributeValue(nameValuePair.getName(), nameValuePair.getValue());
}
}
copyAnnotations(fromMethodModifierList, resultMethodModifierList);
PsiCodeBlock body = fromMethod.getBody();
if (null != body) {
@@ -277,6 +268,21 @@ public class DelombokHandler {
return (PsiMethod) CodeStyleManager.getInstance(project).reformat(resultMethod);
}
private void copyAnnotations(@NotNull PsiModifierList fromModifierList, @NotNull PsiModifierList toModifierList) {
final Set<String> existedAnnotation = Stream.of(toModifierList.getAnnotations())
.map(PsiAnnotation::getQualifiedName)
.collect(Collectors.toSet());
for (PsiAnnotation originalAnnotation : fromModifierList.getAnnotations()) {
final String qualifiedName = Strings.notNullize(originalAnnotation.getQualifiedName());
if (!existedAnnotation.contains(qualifiedName)) {
final PsiAnnotation annotation = toModifierList.addAnnotation(qualifiedName);
for (PsiNameValuePair nameValuePair : originalAnnotation.getParameterList().getAttributes()) {
annotation.setDeclaredAttributeValue(nameValuePair.getName(), nameValuePair.getValue());
}
}
}
}
private void rebuildTypeParameter(@NotNull PsiTypeParameterListOwner listOwner, @NotNull PsiTypeParameterListOwner resultOwner) {
final PsiTypeParameterList resultOwnerTypeParameterList = resultOwner.getTypeParameterList();
if (null != resultOwnerTypeParameterList) {

View File

@@ -20,8 +20,9 @@ daemon.donate.title=Lombok support plugin updated to v{0}
daemon.donate.content=<br/>\
Helpful? <b><a href="https://www.paypal.me/mplushnikov">Donate with PayPal</a></b><br/><br/>\
Fixes:<br/>\
- Fixed (<a href="https://github.com/mplushnikov/lombok-intellij-plugin/issues/02">#802</a>): val mis-infers an Optional(T) as Optional(Object) after map. Only for IntelliJ>=2020.2.2<br/>\
- Fixed (<a href="https://github.com/mplushnikov/lombok-intellij-plugin/issues/802">#802</a>): val mis-infers an Optional(T) as Optional(Object) after map. Only for IntelliJ>=2020.2.2<br/>\
- Fixed (<a href="https://github.com/mplushnikov/lombok-intellij-plugin/issues/826">#826</a>): Error if using @FieldNameConstants in switch case<br/>\
- Fixed (<a href="https://github.com/mplushnikov/lombok-intellij-plugin/issues/858">#858</a>): Delombok produces duplicate @NonNull annotations on setters/getters<br/>\
<br>\
If you find my plugin helpful, donate me using <br><b>\
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\\&hosted_button_id=3F9HXD7A2SMCN\\&source=url">PayPal</a>\

View File

@@ -18,4 +18,7 @@ public class DelombokDataActionTest extends LombokLightActionTestCase {
doTest();
}
public void testDataWithAnnotations() throws Exception {
doTest();
}
}

View File

@@ -0,0 +1,50 @@
import lombok.NonNull;
public class DataWithAnnotations {
@NonNull
@Deprecated
@SuppressWarnings("any")
private Integer someParentInteger;
public DataWithAnnotations(@NonNull Integer someParentInteger) {
this.someParentInteger = someParentInteger;
}
@Deprecated
public @NonNull Integer getSomeParentInteger() {
return this.someParentInteger;
}
@Deprecated
public void setSomeParentInteger(@NonNull Integer someParentInteger) {
this.someParentInteger = someParentInteger;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof DataWithAnnotations)) return false;
final DataWithAnnotations other = (DataWithAnnotations) o;
if (!other.canEqual((Object) this)) return false;
final Object this$someParentInteger = this.getSomeParentInteger();
final Object other$someParentInteger = other.getSomeParentInteger();
if (this$someParentInteger == null ? other$someParentInteger != null : !this$someParentInteger.equals(other$someParentInteger))
return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof DataWithAnnotations;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $someParentInteger = this.getSomeParentInteger();
result = result * PRIME + ($someParentInteger == null ? 43 : $someParentInteger.hashCode());
return result;
}
public String toString() {
return "DataWithAnnotations(someParentInteger=" + this.getSomeParentInteger() + ")";
}
}

View File

@@ -0,0 +1,11 @@
import lombok.Data;
import lombok.NonNull;
@Data
public class DataWithAnnotations {
<caret>
@NonNull
@Deprecated
@SuppressWarnings("any")
private Integer someParentInteger;
}