Skip to content

Commit

Permalink
added support for ToString.Include.rank functionality projectlombok#504
Browse files Browse the repository at this point in the history
updated TestData
  • Loading branch information
mplushnikov committed Oct 20, 2018
1 parent a00cb1f commit af7770e
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 51 deletions.
3 changes: 1 addition & 2 deletions parts/pluginChanges.html
Expand Up @@ -2,7 +2,7 @@
<li>0.22
<ol>
<li>Performance improvements #516: Editor UI freezes when editing a particular class</li>
<li>Fixed #504: Added Support for Delombok of @EqualsAndHashcode/ToString.Exclude/Include</li>
<li>Fixed #504: Added Support for Delombok of @EqualsAndHashcode and @ToString Exclude/Include annotations</li>
</ol>
</li>
<li>0.21
Expand All @@ -26,7 +26,6 @@
<ol>
<li>Fixed #475: Added support for @FieldNameConstants</li>
<li>Fixed #488: Added support for @Flogger annotation</li>
LombokConfigSyntaxHighlighterFactory
<li>Fixed #490: Added support for lombok.noArgsConstructor.extraPrivate</li>
<li>Fixed #473: Warning about Wither requiring AllArgsConstructor, although Builder provides one</li>
<li>Fixed #382: Added support for @XXX(onMethod_, onParam_, onConstructor_) for JDK8</li>
Expand Down
Expand Up @@ -23,34 +23,45 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class EqualsAndHashCodeToStringHandler {
public static class MemberInfo {

private static final String TO_STRING_RANK_ANNOTATION_PARAMETER = "rank";

public static class MemberInfo implements Comparable<MemberInfo> {
private final PsiField psiField;
private final PsiMethod psiMethod;
private final String memberName;
private final boolean defaultInclude;
private final int rankValue;

MemberInfo(PsiField psiField) {
this(psiField, psiField.getName(), false);
}

MemberInfo(PsiField psiField, String memberName) {
this(psiField, memberName, false);
MemberInfo(PsiField psiField, String memberName, int rankValue) {
this(psiField, memberName, false, rankValue);
}

MemberInfo(PsiField psiField, String memberName, boolean defaultInclude) {
this(psiField, memberName, defaultInclude, 0);
}

private MemberInfo(PsiField psiField, String memberName, boolean defaultInclude, int rankValue) {
this.psiField = psiField;
this.psiMethod = null;
this.memberName = memberName;
this.defaultInclude = defaultInclude;
this.rankValue = rankValue;
}

MemberInfo(PsiMethod psiMethod, String memberName) {
MemberInfo(PsiMethod psiMethod, String memberName, int rankValue) {
this.psiField = null;
this.psiMethod = psiMethod;
this.memberName = memberName;
this.defaultInclude = false;
this.rankValue = rankValue;
}

public PsiField getField() {
Expand Down Expand Up @@ -78,6 +89,11 @@ private boolean matchDefaultIncludedFieldName(String fieldName) {
}
return false;
}

@Override
public int compareTo(@NotNull MemberInfo other) {
return Integer.compare(other.rankValue, rankValue);
}
}

public Collection<MemberInfo> filterFields(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, boolean filterTransient, String includeAnnotationProperty) {
Expand All @@ -102,7 +118,7 @@ public Collection<MemberInfo> filterFields(@NotNull PsiClass psiClass, @NotNull
final Collection<PsiMember> psiMembers = PsiClassUtil.collectClassMemberIntern(psiClass);

final Collection<String> fieldNames2BeReplaced = new ArrayList<>();
final Collection<MemberInfo> result = new ArrayList<>(psiMembers.size());
final List<MemberInfo> result = new ArrayList<>(psiMembers.size());

for (PsiMember psiMember : psiMembers) {
final PsiAnnotation includeAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiMember, annotationIncludeFQN);
Expand Down Expand Up @@ -144,22 +160,24 @@ public Collection<MemberInfo> filterFields(@NotNull PsiClass psiClass, @NotNull
}
result.add(new MemberInfo(psiField, fieldName, true));
} else {
final String annotationValue = PsiAnnotationUtil.getStringAnnotationValue(includeAnnotation, includeAnnotationProperty);
final String includeNameValue = PsiAnnotationUtil.getStringAnnotationValue(includeAnnotation, includeAnnotationProperty);
final String newMemberName;
if (StringUtil.isEmptyOrSpaces(annotationValue)) {
if (StringUtil.isEmptyOrSpaces(includeNameValue)) {
newMemberName = psiMember.getName();
} else {
newMemberName = annotationValue;
newMemberName = includeNameValue;
}

if ((psiMember instanceof PsiMethod)) {
final PsiMethod psiMethod = (PsiMethod) psiMember;
if (0 == psiMethod.getParameterList().getParametersCount()) {
fieldNames2BeReplaced.add(newMemberName);
result.add(new MemberInfo(psiMethod, psiMethod.getName()));
int memberRank = calcMemberRank(includeAnnotation);
result.add(new MemberInfo(psiMethod, psiMethod.getName(), memberRank));
}
} else {
result.add(new MemberInfo((PsiField) psiMember, newMemberName));
int memberRank = calcMemberRank(includeAnnotation);
result.add(new MemberInfo((PsiField) psiMember, newMemberName, memberRank));
}
}
}
Expand All @@ -169,9 +187,21 @@ public Collection<MemberInfo> filterFields(@NotNull PsiClass psiClass, @NotNull
result.removeIf(memberInfo -> memberInfo.matchDefaultIncludedFieldName(fieldName));
}

result.sort(MemberInfo::compareTo);
return result;
}

private int calcMemberRank(@NotNull PsiAnnotation includeAnnotation) {
final String includeRankValue = PsiAnnotationUtil.getStringAnnotationValue(includeAnnotation, TO_STRING_RANK_ANNOTATION_PARAMETER);
if (!StringUtil.isEmptyOrSpaces(includeRankValue)) {
try {
return Integer.parseInt(includeRankValue);
} catch (NumberFormatException ignore) {
}
}
return 0;
}

public String getMemberAccessorName(@NotNull MemberInfo memberInfo, boolean doNotUseGetters, @NotNull PsiClass psiClass) {
final String memberAccessor;
if (null == memberInfo.getMethod()) {
Expand Down Expand Up @@ -204,7 +234,8 @@ private String buildAttributeNameString(boolean doNotUseGetters, @NotNull PsiFie
private Collection<String> makeSet(@NotNull Collection<String> exclude) {
if (exclude.isEmpty()) {
return Collections.emptySet();
} else {
return new HashSet<>(exclude);
}
return new HashSet<>(exclude);
}
}
@@ -1,23 +1,7 @@
package de.plushnikov.intellij.plugin.util;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiArrayInitializerMemberValue;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassObjectAccessExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementFactory;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypeElement;
import com.intellij.psi.PsiVariable;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -121,6 +105,11 @@ private static <T> T resolveElementValue(@NotNull PsiElement psiElement, @NotNul
if (asClass.isAssignableFrom(PsiAnnotation.class)) {
value = (T) psiElement;
}
} else if (psiElement instanceof PsiPrefixExpression) {
if (asClass.isAssignableFrom(String.class)) {
String expressionText = psiElement.getText();
value = (T) expressionText;
}
}
return value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/lombokBundle.properties
Expand Up @@ -23,7 +23,7 @@ daemon.donate.content=<br/>\
Helpful? <b><a href="https://www.paypal.me/mplushnikov">Donate with PayPal</a></b><br/><br/>\
Fixes:<br/>\
- Performance improvements (<a href="https://github.com/mplushnikov/lombok-intellij-plugin/issues/516">#516</a>): Editor UI freezes when editing a particular class<br>\
- Fixed (<a href="https://github.com/mplushnikov/lombok-intellij-plugin/issues/504">#504</a>): Added Support for Delombok of @EqualsAndHashcode/ToString.Exclude/Include<br>\
- Fixed (<a href="https://github.com/mplushnikov/lombok-intellij-plugin/issues/504">#504</a>): Added Support for Delombok of @EqualsAndHashcode. and @ToString.Exclude/Include annotations<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">PayPal</a>\
Expand Down
Expand Up @@ -23,6 +23,10 @@ public class EqualsAndHashCodeTest extends AbstractLombokParsingTestCase {
doTest(true);
}

public void testEqualsandhashcode$EqualsAndHashCodeExplicitInclude() {
doTest(true);
}

public void testEqualsandhashcode$EqualsAndHashCodeNestedShadow() {
doTest(true);
}
Expand Down
Expand Up @@ -19,6 +19,10 @@ public class ToStringTest extends AbstractLombokParsingTestCase {
doTest(true);
}

public void testTostring$ToStringExplicitInclude() {
doTest(true);
}

public void testTostring$ToStringInner() {
doTest(true);
}
Expand Down
Expand Up @@ -35,7 +35,7 @@ String getA() {
return a;
}

public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCodeSimpleExclude)) return false;
final EqualsAndHashCodeSimpleExclude other = (EqualsAndHashCodeSimpleExclude) o;
Expand All @@ -51,6 +51,10 @@ public boolean equals(Object o) {
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof EqualsAndHashCodeSimpleExclude;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
Expand All @@ -64,8 +68,4 @@ public int hashCode() {
result = result * PRIME + ($a == null ? 43 : $a.hashCode());
return result;
}

protected boolean canEqual(Object other) {
return other instanceof EqualsAndHashCodeSimpleExclude;
}
}
Expand Up @@ -35,7 +35,7 @@ String getA() {
return a;
}

public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCodeNoGetters)) return false;
final EqualsAndHashCodeNoGetters other = (EqualsAndHashCodeNoGetters) o;
Expand All @@ -52,6 +52,10 @@ public boolean equals(Object o) {
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof EqualsAndHashCodeNoGetters;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
Expand All @@ -66,8 +70,4 @@ public int hashCode() {
result = result * PRIME + ($a == null ? 43 : $a.hashCode());
return result;
}

protected boolean canEqual(Object other) {
return other instanceof EqualsAndHashCodeNoGetters;
}
}
Expand Up @@ -35,7 +35,7 @@ String getA() {
return a;
}

public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCodeSimpleOf)) return false;
final EqualsAndHashCodeSimpleOf other = (EqualsAndHashCodeSimpleOf) o;
Expand All @@ -44,14 +44,14 @@ public boolean equals(Object o) {
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof EqualsAndHashCodeSimpleOf;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
return result;
}

protected boolean canEqual(Object other) {
return other instanceof EqualsAndHashCodeSimpleOf;
}
}
Expand Up @@ -35,7 +35,7 @@ String getA() {
return a;
}

public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCodeSimple)) return false;
final EqualsAndHashCodeSimple other = (EqualsAndHashCodeSimple) o;
Expand All @@ -52,6 +52,10 @@ public boolean equals(Object o) {
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof EqualsAndHashCodeSimple;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
Expand All @@ -66,8 +70,4 @@ public int hashCode() {
result = result * PRIME + ($a == null ? 43 : $a.hashCode());
return result;
}

protected boolean canEqual(Object other) {
return other instanceof EqualsAndHashCodeSimple;
}
}
@@ -0,0 +1,22 @@
class EqualsAndHashCodeExplicitInclude {
int x;
@Override
@SuppressWarnings("all")
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCodeExplicitInclude)) return false;
final EqualsAndHashCodeExplicitInclude other = (EqualsAndHashCodeExplicitInclude) o;
if (!other.canEqual((Object) this)) return false;
return true;
}
@SuppressWarnings("all")
protected boolean canEqual(final Object other) {
return other instanceof EqualsAndHashCodeExplicitInclude;
}
@Override
@SuppressWarnings("all")
public int hashCode() {
int result = 1;
return result;
}
}
8 changes: 8 additions & 0 deletions testData/after/tostring/ToStringExplicitInclude.java
@@ -0,0 +1,8 @@
class ToStringExplicitInclude {
int x;
@Override
@SuppressWarnings("all")
public String toString() {
return "ToStringExplicitInclude()";
}
}
@@ -0,0 +1,4 @@
@lombok.EqualsAndHashCode(onlyExplicitlyIncluded = true)
class EqualsAndHashCodeExplicitInclude {
int x;
}
4 changes: 4 additions & 0 deletions testData/before/tostring/ToStringExplicitInclude.java
@@ -0,0 +1,4 @@
@lombok.ToString(onlyExplicitlyIncluded = true)
class ToStringExplicitInclude {
int x;
}

0 comments on commit af7770e

Please sign in to comment.