Skip to content

Commit

Permalink
Add Builder setterPrefix mplushnikov#719
Browse files Browse the repository at this point in the history
  • Loading branch information
ocadaruma committed Feb 22, 2020
1 parent 856f123 commit f6b8b37
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class BuilderHandler {
private final static String ANNOTATION_BUILDER_CLASS_NAME = "builderClassName";
private static final String ANNOTATION_BUILD_METHOD_NAME = "buildMethodName";
private static final String ANNOTATION_BUILDER_METHOD_NAME = "builderMethodName";
private static final String ANNOTATION_SETTER_PREFIX = "setterPrefix";

private final static String BUILD_METHOD_NAME = "build";
private final static String BUILDER_METHOD_NAME = "builder";
Expand Down Expand Up @@ -231,6 +232,12 @@ String getBuilderMethodName(@NotNull PsiAnnotation psiAnnotation) {
return null == builderMethodName ? BUILDER_METHOD_NAME : builderMethodName;
}

@NotNull
private String getSetterPrefix(@NotNull PsiAnnotation psiAnnotation) {
final String setterPrefix = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, ANNOTATION_SETTER_PREFIX);
return null == setterPrefix ? "" : setterPrefix;
}

@NotNull
@PsiModifier.ModifierConstant
private String getBuilderOuterAccessVisibility(@NotNull PsiAnnotation psiAnnotation) {
Expand Down Expand Up @@ -361,11 +368,13 @@ public List<BuilderInfo> createBuilderInfos(@NotNull PsiAnnotation psiAnnotation
@Nullable PsiMethod psiClassMethod, @NotNull PsiClass builderClass) {
final PsiSubstitutor builderSubstitutor = getBuilderSubstitutor(psiClass, builderClass);
final String accessVisibility = getBuilderInnerAccessVisibility(psiAnnotation);
final String setterPrefix = getSetterPrefix(psiAnnotation);

return createBuilderInfos(psiClass, psiClassMethod)
.map(info -> info.withSubstitutor(builderSubstitutor))
.map(info -> info.withBuilderClass(builderClass))
.map(info -> info.withVisibilityModifier(accessVisibility))
.map(info -> info.withSetterPrefix(setterPrefix))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class BuilderInfo {
private PsiType fieldInBuilderType;
private boolean deprecated;
private String visibilityModifier;
private String setterPrefix;

private String builderChainResult = "this";

Expand Down Expand Up @@ -103,6 +104,11 @@ public BuilderInfo withVisibilityModifier(String visibilityModifier) {
return this;
}

public BuilderInfo withSetterPrefix(String setterPrefix) {
this.setterPrefix = setterPrefix;
return this;
}

public BuilderInfo withBuilderClass(@NotNull PsiClass builderClass) {
this.builderClass = builderClass;
this.builderClassType = PsiClassUtil.getTypeWithGenerics(builderClass);
Expand Down Expand Up @@ -187,6 +193,10 @@ public String getVisibilityModifier() {
return visibilityModifier;
}

public String getSetterPrefix() {
return setterPrefix;
}

public PsiClass getBuilderClass() {
return builderClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.plushnikov.intellij.plugin.processor.handler.BuilderInfo;
import de.plushnikov.intellij.plugin.psi.LombokLightFieldBuilder;
import de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder;
import de.plushnikov.intellij.plugin.thirdparty.LombokUtils;
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
import de.plushnikov.intellij.plugin.util.PsiMethodUtil;
import de.plushnikov.intellij.plugin.util.PsiTypeUtil;
Expand Down Expand Up @@ -60,7 +61,8 @@ public Collection<PsiMethod> renderBuilderMethod(@NotNull BuilderInfo info) {
final String singularName = createSingularName(info.getSingularAnnotation(), fieldName);

final PsiClass builderClass = info.getBuilderClass();
final LombokLightMethodBuilder oneAddMethodBuilder = new LombokLightMethodBuilder(info.getManager(), singularName)
final LombokLightMethodBuilder oneAddMethodBuilder = new LombokLightMethodBuilder(
info.getManager(), LombokUtils.buildAccessorName(info.getSetterPrefix(), singularName))
.withContainingClass(builderClass)
.withMethodReturnType(returnType)
.withNavigationElement(info.getVariable())
Expand All @@ -74,7 +76,8 @@ public Collection<PsiMethod> renderBuilderMethod(@NotNull BuilderInfo info) {

methods.add(oneAddMethodBuilder);

final LombokLightMethodBuilder allAddMethodBuilder = new LombokLightMethodBuilder(info.getManager(), fieldName)
final LombokLightMethodBuilder allAddMethodBuilder = new LombokLightMethodBuilder(
info.getManager(), LombokUtils.buildAccessorName(info.getSetterPrefix(), fieldName))
.withContainingClass(builderClass)
.withMethodReturnType(returnType)
.withNavigationElement(info.getVariable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.plushnikov.intellij.plugin.processor.handler.BuilderInfo;
import de.plushnikov.intellij.plugin.psi.LombokLightFieldBuilder;
import de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder;
import de.plushnikov.intellij.plugin.thirdparty.LombokUtils;
import de.plushnikov.intellij.plugin.util.PsiMethodUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -31,7 +32,8 @@ public Collection<PsiField> renderBuilderFields(@NotNull BuilderInfo info) {
@Override
public Collection<PsiMethod> renderBuilderMethod(@NotNull BuilderInfo info) {
final String blockText = getAllMethodBody(info.getFieldName(), info.getBuilderChainResult());
final LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(info.getManager(), info.getFieldName())
final String methodName = LombokUtils.buildAccessorName(info.getSetterPrefix(), info.getFieldName());
final LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(info.getManager(), methodName)
.withContainingClass(info.getBuilderClass())
.withMethodReturnType(info.getBuilderType())
.withParameter(info.getFieldName(), info.getFieldType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ private static Collection<String> toAllAccessorNames(AccessorsInfo accessorsInfo
return result;
}

public static String buildAccessorName(String prefix, String suffix) {
if (prefix.isEmpty()) {
return suffix;
}
if (suffix.isEmpty()) {
return prefix;
}
return buildName(prefix, suffix);
}

private static String buildName(String prefix, String suffix) {
return prefix + StringUtil.capitalize(suffix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ public void testBuilderAtConstructorSimplePredefined() throws Exception {
public void testBuilderSingularMap() throws Exception {
doTest();
}

public void testBuilderSimpleWithSetterPrefix() throws Exception {
doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public void setUp() throws Exception {
doTest(true);
}

public void testBuilder$BuilderWithAccessorsWithSetterPrefix() {
doTest(true);
}

public void testBuilder$BuilderWithFieldAccessors() {
doTest(true);
}
Expand Down Expand Up @@ -86,6 +90,10 @@ public void setUp() throws Exception {
doTest(true);
}

public void testBuilder$BuilderSingularSetsWithSetterPrefix() {
doTest(true);
}

public void testBuilder$BuilderSingularLists() {
doTest(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class BuilderSimpleWithSetterPrefix {
private int myInt;
private String myString;

BuilderSimpleWithSetterPrefix(int myInt, String myString) {
this.myInt = myInt;
this.myString = myString;
}

public static void main(String[] args) {
BuilderSimpleWithSetterPrefix builderSimple = BuilderSimpleWithSetterPrefix.builder().setMyInt(123).setMyString("string").build();
System.out.println(builderSimple);
}

public static BuilderSimpleWithSetterPrefixBuilder builder() {
return new BuilderSimpleWithSetterPrefixBuilder();
}

public static class BuilderSimpleWithSetterPrefixBuilder {
private int myInt;
private String myString;

BuilderSimpleWithSetterPrefixBuilder() {
}

public BuilderSimpleWithSetterPrefixBuilder setMyInt(int myInt) {
this.myInt = myInt;
return this;
}

public BuilderSimpleWithSetterPrefixBuilder setMyString(String myString) {
this.myString = myString;
return this;
}

public BuilderSimpleWithSetterPrefix build() {
return new BuilderSimpleWithSetterPrefix(myInt, myString);
}

public String toString() {
return "BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder(myInt=" + this.myInt + ", myString=" + this.myString + ")";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@lombok.Builder(setterPrefix = "set")
public class BuilderSimpleWithSetterPrefix {
private int myInt;
private String myString;

public static void main(String[] args) {
BuilderSimpleWithSetterPrefix builderSimple = BuilderSimpleWithSetterPrefix.builder().setMyInt(123).setMyString("string").build();
System.out.println(builderSimple);
}
}
175 changes: 175 additions & 0 deletions testData/after/builder/BuilderSingularSetsWithSetterPrefix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Generated by delombok at Wed Oct 02 19:12:43 GMT 2019

import java.util.Set;
import java.util.SortedSet;

class BuilderSingularSetsWithSetterPrefix<T> {
private Set<T> dangerMice;
private SortedSet<? extends Number> octopodes;
@SuppressWarnings("all")
private Set rawSet;
private Set<String> stringSet;

@java.lang.SuppressWarnings("all")
BuilderSingularSetsWithSetterPrefix(final Set<T> dangerMice, final SortedSet<? extends Number> octopodes, final Set rawSet, final Set<String> stringSet) {
this.dangerMice = dangerMice;
this.octopodes = octopodes;
this.rawSet = rawSet;
this.stringSet = stringSet;
}


@java.lang.SuppressWarnings("all")
public static class BuilderSingularSetsWithSetterPrefixBuilder<T> {
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<T> dangerMice;
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<Number> octopodes;
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<java.lang.Object> rawSet;
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<String> stringSet;

@java.lang.SuppressWarnings("all")
BuilderSingularSetsWithSetterPrefixBuilder() {
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setDangerMouse(final T dangerMouse) {
if (this.dangerMice == null) this.dangerMice = new java.util.ArrayList<T>();
this.dangerMice.add(dangerMouse);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setDangerMice(final java.util.Collection<? extends T> dangerMice) {
if (this.dangerMice == null) this.dangerMice = new java.util.ArrayList<T>();
this.dangerMice.addAll(dangerMice);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> clearDangerMice() {
if (this.dangerMice != null) this.dangerMice.clear();
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setOctopus(final Number octopus) {
if (this.octopodes == null) this.octopodes = new java.util.ArrayList<Number>();
this.octopodes.add(octopus);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setOctopodes(final java.util.Collection<? extends Number> octopodes) {
if (this.octopodes == null) this.octopodes = new java.util.ArrayList<Number>();
this.octopodes.addAll(octopodes);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> clearOctopodes() {
if (this.octopodes != null) this.octopodes.clear();
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setRawSet(final java.lang.Object rawSet) {
if (this.rawSet == null) this.rawSet = new java.util.ArrayList<java.lang.Object>();
this.rawSet.add(rawSet);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setRawSet(final java.util.Collection<?> rawSet) {
if (this.rawSet == null) this.rawSet = new java.util.ArrayList<java.lang.Object>();
this.rawSet.addAll(rawSet);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> clearRawSet() {
if (this.rawSet != null) this.rawSet.clear();
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setStringSet(final String stringSet) {
if (this.stringSet == null) this.stringSet = new java.util.ArrayList<String>();
this.stringSet.add(stringSet);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> setStringSet(final java.util.Collection<? extends String> stringSet) {
if (this.stringSet == null) this.stringSet = new java.util.ArrayList<String>();
this.stringSet.addAll(stringSet);
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefixBuilder<T> clearStringSet() {
if (this.stringSet != null) this.stringSet.clear();
return this;
}

@java.lang.SuppressWarnings("all")
public BuilderSingularSetsWithSetterPrefix<T> build() {
java.util.Set<T> dangerMice;
switch (this.dangerMice == null ? 0 : this.dangerMice.size()) {
case 0:
dangerMice = java.util.Collections.emptySet();
break;
case 1:
dangerMice = java.util.Collections.singleton(this.dangerMice.get(0));
break;
default:
dangerMice = new java.util.LinkedHashSet<T>(this.dangerMice.size() < 1073741824 ? 1 + this.dangerMice.size() + (this.dangerMice.size() - 3) / 3 : java.lang.Integer.MAX_VALUE);
dangerMice.addAll(this.dangerMice);
dangerMice = java.util.Collections.unmodifiableSet(dangerMice);
}
java.util.SortedSet<Number> octopodes = new java.util.TreeSet<Number>();
if (this.octopodes != null) octopodes.addAll(this.octopodes);
octopodes = java.util.Collections.unmodifiableSortedSet(octopodes);
java.util.Set<java.lang.Object> rawSet;
switch (this.rawSet == null ? 0 : this.rawSet.size()) {
case 0:
rawSet = java.util.Collections.emptySet();
break;
case 1:
rawSet = java.util.Collections.singleton(this.rawSet.get(0));
break;
default:
rawSet = new java.util.LinkedHashSet<java.lang.Object>(this.rawSet.size() < 1073741824 ? 1 + this.rawSet.size() + (this.rawSet.size() - 3) / 3 : java.lang.Integer.MAX_VALUE);
rawSet.addAll(this.rawSet);
rawSet = java.util.Collections.unmodifiableSet(rawSet);
}
java.util.Set<String> stringSet;
switch (this.stringSet == null ? 0 : this.stringSet.size()) {
case 0:
stringSet = java.util.Collections.emptySet();
break;
case 1:
stringSet = java.util.Collections.singleton(this.stringSet.get(0));
break;
default:
stringSet = new java.util.LinkedHashSet<String>(this.stringSet.size() < 1073741824 ? 1 + this.stringSet.size() + (this.stringSet.size() - 3) / 3 : java.lang.Integer.MAX_VALUE);
stringSet.addAll(this.stringSet);
stringSet = java.util.Collections.unmodifiableSet(stringSet);
}
return new BuilderSingularSetsWithSetterPrefix<T>(dangerMice, octopodes, rawSet, stringSet);
}

@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "BuilderSingularSetsWithSetterPrefix.BuilderSingularSetsWithSetterPrefixBuilder(dangerMice=" + this.dangerMice + ", octopodes=" + this.octopodes + ", rawSet=" + this.rawSet + ", stringSet=" + this.stringSet + ")";
}
}

@java.lang.SuppressWarnings("all")
public static <T> BuilderSingularSetsWithSetterPrefixBuilder<T> builder() {
return new BuilderSingularSetsWithSetterPrefixBuilder<T>();
}
}

0 comments on commit f6b8b37

Please sign in to comment.