Skip to content

Commit

Permalink
Fixed @wither + @accessors(fluent=true) (projectlombok#414)
Browse files Browse the repository at this point in the history
* projectlombok#412: added failing test for @wither and @accessors(fluent=true)
* projectlombok#412: fixing WitherProcessor and WitherFieldProcessor so that they do not take into account @accessors(fluent=true)
  • Loading branch information
Tomasz Linkowski authored and mplushnikov committed Sep 12, 2017
1 parent 744792a commit d3195ea
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ private boolean validateVisibility(@NotNull PsiAnnotation psiAnnotation) {
return null != methodVisibility;
}

@Override
protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) {
final String methodVisibility = LombokProcessorUtil.getMethodModifier(psiAnnotation);
if (methodVisibility != null) {
final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiClass);
target.addAll(createFieldWithers(psiClass, psiAnnotation, methodVisibility, accessorsInfo));
final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiClass).withFluent(false);
target.addAll(createFieldWithers(psiClass, methodVisibility, accessorsInfo));
}
}

@NotNull
private Collection<PsiMethod> createFieldWithers(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation, @NotNull String methodModifier, @NotNull AccessorsInfo accessors) {
private Collection<PsiMethod> createFieldWithers(@NotNull PsiClass psiClass, @NotNull String methodModifier, @NotNull AccessorsInfo accessors) {
Collection<PsiMethod> result = new ArrayList<PsiMethod>();

final Collection<PsiField> witherFields = getWitherFields(psiClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public boolean isFluent() {
return fluent;
}

public AccessorsInfo withFluent(boolean fluentValue) {
if (fluent == fluentValue) {
return this;
}
return new AccessorsInfo(fluentValue, chain, doNotUseIsPrefix, prefixes);
}

public boolean isChain() {
return chain;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private boolean validateVisibility(@NotNull PsiAnnotation psiAnnotation) {
protected void generatePsiElements(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) {
String methodModifier = LombokProcessorUtil.getMethodModifier(psiAnnotation);
if (methodModifier != null) {
AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
AccessorsInfo accessorsInfo = buildAccessorsInfo(psiField);
PsiMethod method = createWitherMethod(psiField, methodModifier, accessorsInfo);
if (method != null) {
target.add(method);
Expand Down Expand Up @@ -105,7 +105,7 @@ private boolean validIsWitherUnique(@NotNull PsiField psiField, @NotNull final P
if (psiFieldName != null && fieldContainingClass != null) {
final Collection<PsiMethod> classMethods = PsiClassUtil.collectClassMethodsIntern(fieldContainingClass);

final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
final AccessorsInfo accessorsInfo = buildAccessorsInfo(psiField);
final Collection<String> possibleWitherNames = LombokUtils.toAllWitherNames(accessorsInfo, psiFieldName, PsiType.BOOLEAN.equals(psiField.getType()));
for (String witherName : possibleWitherNames) {
if (PsiMethodUtil.hasSimilarMethod(classMethods, witherName, 1)) {
Expand Down Expand Up @@ -215,6 +215,10 @@ private PsiCodeBlock createCodeBlock(@NotNull PsiField psiField, PsiClass psiFie
return PsiMethodUtil.createCodeBlockFromText(blockText, psiFieldContainingClass);
}

private AccessorsInfo buildAccessorsInfo(@NotNull PsiField psiField) {
return AccessorsInfo.build(psiField).withFluent(false);
}

private String getWitherName(@NotNull AccessorsInfo accessorsInfo, String psiFieldName, PsiType psiFieldType) {
return LombokUtils.toWitherName(accessorsInfo, psiFieldName, PsiType.BOOLEAN.equals(psiFieldType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public static String toSetterName(AccessorsInfo accessors, String fieldName, boo
* @return The wither name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
*/
public static String toWitherName(AccessorsInfo accessors, String fieldName, boolean isBoolean) {
if (accessors.isFluent()) {
throw new IllegalArgumentException("@Wither does not support @Accessors(fluent=true)");
}
return toAccessorName(accessors, fieldName, isBoolean, "with", "with");
}

Expand Down Expand Up @@ -158,6 +161,9 @@ public static Collection<String> toAllSetterNames(AccessorsInfo accessorsInfo, S
* @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
*/
public static Collection<String> toAllWitherNames(AccessorsInfo accessorsInfo, String fieldName, boolean isBoolean) {
if (accessorsInfo.isFluent()) {
throw new IllegalArgumentException("@Wither does not support @Accessors(fluent=true)");
}
return toAllAccessorNames(accessorsInfo, fieldName, isBoolean, "with", "with");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ protected boolean shouldCompareCodeBlocks() {
doTest(true);
}

public void testWither$WitherAndAccessors() throws IOException {
doTest(true);
}

public void testWither$WitherAndAllArgsConstructor() throws IOException {
doTest(true);
}
Expand Down Expand Up @@ -49,4 +53,4 @@ protected boolean shouldCompareCodeBlocks() {
public void testWither$WitherWithAbstract() throws IOException {
doTest(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public static String toSetterName(AccessorsInfo accessors, CharSequence fieldNam
* @return The wither name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
*/
public static String toWitherName(AccessorsInfo accessors, CharSequence fieldName, boolean isBoolean) {
if (accessors.isFluent()) {
throw new IllegalArgumentException("@Wither does not support @Accessors(fluent=true)");
}
return toAccessorName(accessors, fieldName, isBoolean, "with", "with", false);
}

Expand Down Expand Up @@ -226,6 +229,9 @@ public static List<String> toAllSetterNames(AccessorsInfo accessors, CharSequenc
* @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
*/
public static List<String> toAllWitherNames(AccessorsInfo accessors, CharSequence fieldName, boolean isBoolean) {
if (accessors.isFluent()) {
throw new IllegalArgumentException("@Wither does not support @Accessors(fluent=true)");
}
return toAllAccessorNames(accessors, fieldName, isBoolean, "with", "with", false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private void makeResults(String fieldName, boolean isBoolean) {
lombokResult.clear();
result.clear();

final AccessorsInfo accessorsInfo = AccessorsInfo.build(false, false, false);
final AccessorsInfo accessorsInfo = AccessorsInfo.EMPTY;
lombokResult.addAll(LombokHandlerUtil.toAllWitherNames(accessorsInfo, fieldName, isBoolean));
result.addAll(LombokUtils.toAllWitherNames(accessorsInfo, fieldName, isBoolean));

Expand Down Expand Up @@ -82,4 +82,4 @@ public void testToAllWitherNames_Boolean_IS() throws Exception {
assertThat(result, is(Arrays.asList("withISmyField")));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import static org.junit.Assert.assertThat;

public class LombokUtilsWitherTest {
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(false, false, false);

private String makeResults(String fieldName, boolean isBoolean) {
String lombokResult = LombokHandlerUtil.toWitherName(DEFAULT_ACCESSORS, fieldName, isBoolean);
String result = LombokUtils.toWitherName(DEFAULT_ACCESSORS, fieldName, isBoolean);
final AccessorsInfo accessorsInfo = AccessorsInfo.EMPTY;
String lombokResult = LombokHandlerUtil.toWitherName(accessorsInfo, fieldName, isBoolean);
String result = LombokUtils.toWitherName(accessorsInfo, fieldName, isBoolean);

assertThat(result, is(lombokResult));
return result;
Expand Down Expand Up @@ -88,4 +88,4 @@ public void testToWitherNames_Boolean_IS() throws Exception {
assertThat(result, equalTo("withISmyField"));
}

}
}
23 changes: 23 additions & 0 deletions testData/after/wither/WitherAndAccessors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class WitherAndAccessors {

final int x = 10;

int y = 20;

@lombok.experimental.Accessors(fluent=true)
final int z;

@java.beans.ConstructorProperties({"y", "z"})
@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public WitherAndAccessors(final int y, final int z) {
this.y = y;
this.z = z;
}

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public WitherAndAccessors withZ(final int z) {
return this.z == z ? this : new WitherAndAccessors(this.y, z);
}
}
10 changes: 10 additions & 0 deletions testData/before/wither/WitherAndAccessors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@lombok.AllArgsConstructor
class WitherAndAccessors {

final int x = 10;

int y = 20;

@lombok.experimental.Accessors(fluent=true)
@lombok.experimental.Wither final int z;
}

0 comments on commit d3195ea

Please sign in to comment.