mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
[lombok] IDEA-341962 IDEA-301474 improve delombok of SuperBuilder with Builder.Defaults
GitOrigin-RevId: dd3c7ae0bd7ee5168b669228e106c9741218ee24
This commit is contained in:
committed by
intellij-monorepo-bot
parent
01d336719b
commit
4cf257adaa
@@ -83,6 +83,9 @@ public final class SuperBuilderProcessor extends AbstractClassProcessor {
|
|||||||
builderHandler.createBuilderBasedConstructor(psiClass, builderBaseClass, psiAnnotation, psiTypeBaseWithGenerics)
|
builderHandler.createBuilderBasedConstructor(psiClass, builderBaseClass, psiAnnotation, psiTypeBaseWithGenerics)
|
||||||
.ifPresent(target::add);
|
.ifPresent(target::add);
|
||||||
|
|
||||||
|
target.addAll(
|
||||||
|
builderHandler.createBuilderDefaultProviderMethodsIfNecessary(psiClass, null, builderBaseClass, psiAnnotation));
|
||||||
|
|
||||||
// skip generation of builder methods, if class is abstract
|
// skip generation of builder methods, if class is abstract
|
||||||
if (!psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
|
if (!psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
|
||||||
final String builderImplClassName = builderHandler.getBuilderImplClassName(psiClass);
|
final String builderImplClassName = builderHandler.getBuilderImplClassName(psiClass);
|
||||||
|
|||||||
@@ -277,7 +277,7 @@ public class BuilderInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String renderSuperBuilderConstruction() {
|
public String renderSuperBuilderConstruction() {
|
||||||
return builderElementHandler.renderSuperBuilderConstruction(variableInClass, fieldInBuilderName);
|
return builderElementHandler.renderSuperBuilderConstruction(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String renderFieldName() {
|
public String renderFieldName() {
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ public class SuperBuilderHandler extends BuilderHandler {
|
|||||||
.withParameter(BUILDER_VARIABLE_NAME, psiTypeBaseWithGenerics);
|
.withParameter(BUILDER_VARIABLE_NAME, psiTypeBaseWithGenerics);
|
||||||
|
|
||||||
final List<BuilderInfo> builderInfos = createBuilderInfos(psiClass, psiAnnotation, builderClass);
|
final List<BuilderInfo> builderInfos = createBuilderInfos(psiClass, psiAnnotation, builderClass);
|
||||||
//dont need initBuilderInfosBuilderClassType here
|
//don't need initBuilderInfosBuilderClassType here
|
||||||
|
|
||||||
final String buildMethodPrepare = builderInfos.stream()
|
final String buildMethodPrepare = builderInfos.stream()
|
||||||
.map(BuilderInfo::renderSuperBuilderConstruction)
|
.map(BuilderInfo::renderSuperBuilderConstruction)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import de.plushnikov.intellij.plugin.thirdparty.LombokUtils;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -23,8 +24,19 @@ public interface BuilderElementHandler {
|
|||||||
return "this." + info.renderFieldName();
|
return "this." + info.renderFieldName();
|
||||||
}
|
}
|
||||||
|
|
||||||
default String renderSuperBuilderConstruction(@NotNull PsiVariable psiVariable, @NotNull String fieldName) {
|
default String renderSuperBuilderConstruction(@NotNull BuilderInfo info) {
|
||||||
return "this." + psiVariable.getName() + "=b." + fieldName + ";\n";
|
if (info.hasBuilderDefaultAnnotation()) {
|
||||||
|
final String block = """
|
||||||
|
if (b.{0}) '{'
|
||||||
|
this.{1} = b.{2};
|
||||||
|
'}' else '{'
|
||||||
|
this.{1} = {3}();
|
||||||
|
'}'
|
||||||
|
""";
|
||||||
|
return MessageFormat.format(block, info.renderFieldDefaultSetName(), info.getVariable().getName(), info.renderFieldName(),
|
||||||
|
info.renderFieldDefaultProviderName());
|
||||||
|
}
|
||||||
|
return "this." + info.getVariable().getName() + "=b." + info.renderFieldName() + ";\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
default String renderToBuilderCall(@NotNull BuilderInfo info) {
|
default String renderToBuilderCall(@NotNull BuilderInfo info) {
|
||||||
|
|||||||
@@ -103,7 +103,9 @@ class SingularCollectionHandler extends AbstractSingularHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String renderSuperBuilderConstruction(@NotNull PsiVariable psiVariable, @NotNull String fieldName) {
|
public String renderSuperBuilderConstruction(@NotNull BuilderInfo info) {
|
||||||
|
final PsiVariable psiVariable = info.getVariable();
|
||||||
|
final String fieldName = info.renderFieldName();
|
||||||
return renderBuildCode(psiVariable, fieldName, "b") + "this." + psiVariable.getName() + "=" + fieldName + ";\n";
|
return renderBuildCode(psiVariable, fieldName, "b") + "this." + psiVariable.getName() + "=" + fieldName + ";\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -202,7 +202,9 @@ class SingularMapHandler extends AbstractSingularHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String renderSuperBuilderConstruction(@NotNull PsiVariable psiVariable, @NotNull String fieldName) {
|
public String renderSuperBuilderConstruction(@NotNull BuilderInfo info) {
|
||||||
|
final PsiVariable psiVariable = info.getVariable();
|
||||||
|
final String fieldName = info.renderFieldName();
|
||||||
final String basicCode = renderBuildCode(psiVariable, fieldName, "b");
|
final String basicCode = renderBuildCode(psiVariable, fieldName, "b");
|
||||||
final String assignment = "this." + psiVariable.getName() + "=" + fieldName + ";\n";
|
final String assignment = "this." + psiVariable.getName() + "=" + fieldName + ";\n";
|
||||||
return basicCode + assignment;
|
return basicCode + assignment;
|
||||||
|
|||||||
@@ -18,4 +18,8 @@ public class DelombokSuperBuilderActionTest extends LombokLightActionTestCase {
|
|||||||
public void testSuperBuilderJacksonized() throws Exception {
|
public void testSuperBuilderJacksonized() throws Exception {
|
||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSuperBuilderWithBuilderDefault() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SuperBuilderWithBuilderDefault {
|
||||||
|
|
||||||
|
private List<String> listItems = new ArrayList<>();
|
||||||
|
|
||||||
|
protected SuperBuilderWithBuilderDefault(SuperBuilderWithBuilderDefault.SuperBuilderWithBuilderDefaultBuilder<?, ?> b) {
|
||||||
|
if (b.listItems$set) {
|
||||||
|
this.listItems = b.listItems$value;
|
||||||
|
} else {
|
||||||
|
this.listItems = $default$listItems();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> $default$listItems() {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SuperBuilderWithBuilderDefaultBuilder<?, ?> builder() {
|
||||||
|
return new SuperBuilderWithBuilderDefaultBuilderImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static abstract class SuperBuilderWithBuilderDefaultBuilder<C extends SuperBuilderWithBuilderDefault, B extends SuperBuilderWithBuilderDefaultBuilder<C, B>> {
|
||||||
|
private List<String> listItems$value;
|
||||||
|
private boolean listItems$set;
|
||||||
|
|
||||||
|
public B listItems(List<String> listItems) {
|
||||||
|
this.listItems$value = listItems;
|
||||||
|
this.listItems$set = true;
|
||||||
|
return self();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract B self();
|
||||||
|
|
||||||
|
public abstract C build();
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "SuperBuilderWithBuilderDefault.SuperBuilderWithBuilderDefaultBuilder(listItems$value=" + this.listItems$value + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class SuperBuilderWithBuilderDefaultBuilderImpl extends SuperBuilderWithBuilderDefaultBuilder<SuperBuilderWithBuilderDefault, SuperBuilderWithBuilderDefaultBuilderImpl> {
|
||||||
|
private SuperBuilderWithBuilderDefaultBuilderImpl() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected SuperBuilderWithBuilderDefaultBuilderImpl self() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SuperBuilderWithBuilderDefault build() {
|
||||||
|
return new SuperBuilderWithBuilderDefault(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@lombok.experimental.SuperBuilder
|
||||||
|
public class SuperBuilderWithBuilderDefault {
|
||||||
|
<caret>
|
||||||
|
@lombok.Builder.Default
|
||||||
|
private List<String> listItems = new ArrayList<>();
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user