Skip to content

Commit

Permalink
Merge branch 'superToBuilder' of git://github.com/janrieke/lombok int…
Browse files Browse the repository at this point in the history
…o janrieke-superToBuilder
  • Loading branch information
rzwitserloot committed Oct 8, 2018
2 parents 3bb6f69 + bd494bb commit 06455de
Show file tree
Hide file tree
Showing 15 changed files with 1,474 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/core/lombok/eclipse/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static final boolean toBoolean(Object expr, boolean defaultValue) {
return ((Boolean) expr).booleanValue();
}

private static class BuilderFieldData {
static class BuilderFieldData {
Annotation[] annotations;
TypeReference type;
char[] rawName;
Expand Down
236 changes: 209 additions & 27 deletions src/core/lombok/eclipse/handlers/HandleSuperBuilder.java

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions src/core/lombok/experimental/SuperBuilder.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* Copyright (C) 2018 The Project Lombok Authors.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -43,24 +43,23 @@
* The builder also has a <code>build()</code> method which returns a completed instance of the original type.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/experimental/SuperBuilder">the project lombok features page for &#64;SuperBuilder</a>.
*
*
* @see Singular
*/
@Target(TYPE)
@Retention(SOURCE)
public @interface SuperBuilder {
/** @return Name of the method that creates a new builder instance. Default: {@code builder}. */
String builderMethodName() default "builder";

/** @return Name of the method in the builder class that creates an instance of your {@code @Builder}-annotated class. */
String buildMethodName() default "build";

// toBuilder also requires a two-stage system where each class gets its own toBuilder but calls on a second method (and also calls parentclass's method)
// to fill the builder, as this class does not know what fields to pass on to the builder. Let's consider this, but only for milestone 2.
/*
* If true, generate an instance method to obtain a builder that is initialized with the values of this instance.
*

/**
* If <code>true</code>, generate an instance method to obtain a builder that is initialized with the values of this instance.
* In this case, all superclasses must also have <code>toBuilder=true</code>.
*
* @return Whether to generate a {@code toBuilder()} method.
*/
// boolean toBuilder() default false;
boolean toBuilder() default false;
}
2 changes: 1 addition & 1 deletion src/core/lombok/javac/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static final boolean toBoolean(Object expr, boolean defaultValue) {
return ((Boolean) expr).booleanValue();
}

private static class BuilderFieldData {
static class BuilderFieldData {
List<JCAnnotation> annotations;
JCExpression type;
Name rawName;
Expand Down
261 changes: 220 additions & 41 deletions src/core/lombok/javac/handlers/HandleSuperBuilder.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
public class SuperBuilderAbstractToBuilder {
public static class Parent {
int parentField;
@java.lang.SuppressWarnings("all")
public static abstract class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
@java.lang.SuppressWarnings("all")
private int parentField;
@java.lang.SuppressWarnings("all")
protected B $fillValuesFrom(final C instance) {
ParentBuilder.$fillValuesFromInstanceIntoBuilder(instance, this);
return self();
}
@java.lang.SuppressWarnings("all")
private static void $fillValuesFromInstanceIntoBuilder(final Parent instance, final ParentBuilder<?, ?> b) {
b.parentField(instance.parentField);
}
@java.lang.SuppressWarnings("all")
protected abstract B self();
@java.lang.SuppressWarnings("all")
public abstract C build();
@java.lang.SuppressWarnings("all")
public B parentField(final int parentField) {
this.parentField = parentField;
return self();
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "SuperBuilderAbstractToBuilder.Parent.ParentBuilder(parentField=" + this.parentField + ")";
}
}
@java.lang.SuppressWarnings("all")
private static final class ParentBuilderImpl extends ParentBuilder<Parent, ParentBuilderImpl> {
@java.lang.SuppressWarnings("all")
private ParentBuilderImpl() {
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected ParentBuilderImpl self() {
return this;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public Parent build() {
return new Parent(this);
}
}
@java.lang.SuppressWarnings("all")
protected Parent(final ParentBuilder<?, ?> b) {
this.parentField = b.parentField;
}
@java.lang.SuppressWarnings("all")
public static ParentBuilder<?, ?> builder() {
return new ParentBuilderImpl();
}
@java.lang.SuppressWarnings("all")
public ParentBuilder<?, ?> toBuilder() {
return new ParentBuilderImpl().$fillValuesFrom(this);
}
}
public static abstract class Child extends Parent {
double childField;
@java.lang.SuppressWarnings("all")
public static abstract class ChildBuilder<C extends Child, B extends ChildBuilder<C, B>> extends Parent.ParentBuilder<C, B> {
@java.lang.SuppressWarnings("all")
private double childField;
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected B $fillValuesFrom(final C instance) {
super.$fillValuesFrom(instance);
ChildBuilder.$fillValuesFromInstanceIntoBuilder(instance, this);
return self();
}
@java.lang.SuppressWarnings("all")
private static void $fillValuesFromInstanceIntoBuilder(final Child instance, final ChildBuilder<?, ?> b) {
b.childField(instance.childField);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected abstract B self();
@java.lang.Override
@java.lang.SuppressWarnings("all")
public abstract C build();
@java.lang.SuppressWarnings("all")
public B childField(final double childField) {
this.childField = childField;
return self();
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "SuperBuilderAbstractToBuilder.Child.ChildBuilder(super=" + super.toString() + ", childField=" + this.childField + ")";
}
}
@java.lang.SuppressWarnings("all")
protected Child(final ChildBuilder<?, ?> b) {
super(b);
this.childField = b.childField;
}
}
public static class GrandChild extends Child {
String grandChildField;
@java.lang.SuppressWarnings("all")
public static abstract class GrandChildBuilder<C extends GrandChild, B extends GrandChildBuilder<C, B>> extends Child.ChildBuilder<C, B> {
@java.lang.SuppressWarnings("all")
private String grandChildField;
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected B $fillValuesFrom(final C instance) {
super.$fillValuesFrom(instance);
GrandChildBuilder.$fillValuesFromInstanceIntoBuilder(instance, this);
return self();
}
@java.lang.SuppressWarnings("all")
private static void $fillValuesFromInstanceIntoBuilder(final GrandChild instance, final GrandChildBuilder<?, ?> b) {
b.grandChildField(instance.grandChildField);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected abstract B self();
@java.lang.Override
@java.lang.SuppressWarnings("all")
public abstract C build();
@java.lang.SuppressWarnings("all")
public B grandChildField(final String grandChildField) {
this.grandChildField = grandChildField;
return self();
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "SuperBuilderAbstractToBuilder.GrandChild.GrandChildBuilder(super=" + super.toString() + ", grandChildField=" + this.grandChildField + ")";
}
}
@java.lang.SuppressWarnings("all")
private static final class GrandChildBuilderImpl extends GrandChildBuilder<GrandChild, GrandChildBuilderImpl> {
@java.lang.SuppressWarnings("all")
private GrandChildBuilderImpl() {
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected GrandChildBuilderImpl self() {
return this;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public GrandChild build() {
return new GrandChild(this);
}
}
@java.lang.SuppressWarnings("all")
protected GrandChild(final GrandChildBuilder<?, ?> b) {
super(b);
this.grandChildField = b.grandChildField;
}
@java.lang.SuppressWarnings("all")
public static GrandChildBuilder<?, ?> builder() {
return new GrandChildBuilderImpl();
}
@java.lang.SuppressWarnings("all")
public GrandChildBuilder<?, ?> toBuilder() {
return new GrandChildBuilderImpl().$fillValuesFrom(this);
}
}
public static void test() {
GrandChild x = GrandChild.builder().grandChildField("").parentField(5).childField(2.5).build().toBuilder().build();
}
}
Loading

0 comments on commit 06455de

Please sign in to comment.