Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow generics with SuperBuilder customization #3646

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Mateusz Matela <[email protected]>
Michael Dardis <[email protected]>
Michael Ernst <[email protected]>
Michiel Verheul <[email protected]>
Nathaniel Troutman <[email protected]>
Ole Ludwig <[email protected]>
Pascal Bihler <[email protected]>
Peter Grant <[email protected]>
Expand Down
11 changes: 9 additions & 2 deletions src/core/lombok/javac/handlers/HandleSuperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1159,12 +1159,19 @@ private boolean constructorExists(JavacNode type, String builderClassName) {
if (matches && md.params != null && md.params.length() == 1) {
// Cannot use typeMatches() here, because the parameter could be fully-qualified, partially-qualified, or not qualified.
// A string-compare of the last part should work. If it's a false-positive, users could still @Tolerate it.
String typeName = md.params.get(0).getType().toString();
String typeName;
JCTree paramType = md.params.get(0).getType();
if (paramType instanceof JCTypeApply) {
typeName = ((JCTypeApply)paramType).clazz.toString();
} else {
typeName = paramType.toString();
}

int lastIndexOfDot = typeName.lastIndexOf('.');
if (lastIndexOfDot >= 0) {
typeName = typeName.substring(lastIndexOfDot+1);
}
if ((builderClassName+"<?, ?>").equals(typeName))
if (builderClassName.equals(typeName))
return true;
}
}
Expand Down
22 changes: 11 additions & 11 deletions test/transform/resource/after-delombok/SuperBuilderCustomized.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.util.List;
public class SuperBuilderCustomized {
public static class Parent {
public static abstract class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
public static class Parent<T> {
public static abstract class ParentBuilder<T, C extends Parent<T>, B extends ParentBuilder<T, C, B>> {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private int field1;
Expand All @@ -27,35 +27,35 @@ public java.lang.String toString() {
}
}
int field1;
protected Parent(ParentBuilder<?, ?> b) {
protected Parent(ParentBuilder<T, ?, ?> b) {
if (b.field1 == 0) throw new IllegalArgumentException("field1 must be != 0");
this.field1 = b.field1;
}
public static SuperBuilderCustomized.Parent.ParentBuilder<?, ?> builder(int field1) {
return new SuperBuilderCustomized.Parent.ParentBuilderImpl().field1(field1);
public static <T> SuperBuilderCustomized.Parent.ParentBuilder<T, ?, ?> builder(int field1) {
return new SuperBuilderCustomized.Parent.ParentBuilderImpl<T>().field1(field1);
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
private static final class ParentBuilderImpl extends SuperBuilderCustomized.Parent.ParentBuilder<SuperBuilderCustomized.Parent, SuperBuilderCustomized.Parent.ParentBuilderImpl> {
private static final class ParentBuilderImpl<T> extends SuperBuilderCustomized.Parent.ParentBuilder<T, SuperBuilderCustomized.Parent<T>, SuperBuilderCustomized.Parent.ParentBuilderImpl<T>> {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private ParentBuilderImpl() {
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
protected SuperBuilderCustomized.Parent.ParentBuilderImpl self() {
protected SuperBuilderCustomized.Parent.ParentBuilderImpl<T> self() {
return this;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public SuperBuilderCustomized.Parent build() {
return new SuperBuilderCustomized.Parent(this);
public SuperBuilderCustomized.Parent<T> build() {
return new SuperBuilderCustomized.Parent<T>(this);
}
}
}
public static class Child extends Parent {
public static class Child extends Parent<String> {
private static final class ChildBuilderImpl extends ChildBuilder<Child, ChildBuilderImpl> {
@Override
public Child build() {
Expand All @@ -79,7 +79,7 @@ protected SuperBuilderCustomized.Child.ChildBuilderImpl self() {
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static abstract class ChildBuilder<C extends SuperBuilderCustomized.Child, B extends SuperBuilderCustomized.Child.ChildBuilder<C, B>> extends Parent.ParentBuilder<C, B> {
public static abstract class ChildBuilder<C extends SuperBuilderCustomized.Child, B extends SuperBuilderCustomized.Child.ChildBuilder<C, B>> extends Parent.ParentBuilder<String, C, B> {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private double field2;
Expand Down
22 changes: 11 additions & 11 deletions test/transform/resource/after-ecj/SuperBuilderCustomized.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.util.List;
public class SuperBuilderCustomized {
public static @lombok.experimental.SuperBuilder class Parent {
public static abstract class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
public static @lombok.experimental.SuperBuilder class Parent<T> {
public static abstract class ParentBuilder<T, C extends Parent<T>, B extends ParentBuilder<T, C, B>> {
private @java.lang.SuppressWarnings("all") @lombok.Generated int field1;
public ParentBuilder() {
super();
Expand All @@ -20,29 +20,29 @@ public B field1(int field1) {
return (("SuperBuilderCustomized.Parent.ParentBuilder(field1=" + this.field1) + ")");
}
}
private static final @java.lang.SuppressWarnings("all") @lombok.Generated class ParentBuilderImpl extends SuperBuilderCustomized.Parent.ParentBuilder<SuperBuilderCustomized.Parent, SuperBuilderCustomized.Parent.ParentBuilderImpl> {
private static final @java.lang.SuppressWarnings("all") @lombok.Generated class ParentBuilderImpl<T> extends SuperBuilderCustomized.Parent.ParentBuilder<T, SuperBuilderCustomized.Parent<T>, SuperBuilderCustomized.Parent.ParentBuilderImpl<T>> {
private ParentBuilderImpl() {
super();
}
protected @java.lang.Override @java.lang.SuppressWarnings("all") @lombok.Generated SuperBuilderCustomized.Parent.ParentBuilderImpl self() {
protected @java.lang.Override @java.lang.SuppressWarnings("all") @lombok.Generated SuperBuilderCustomized.Parent.ParentBuilderImpl<T> self() {
return this;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @lombok.Generated SuperBuilderCustomized.Parent build() {
return new SuperBuilderCustomized.Parent(this);
public @java.lang.Override @java.lang.SuppressWarnings("all") @lombok.Generated SuperBuilderCustomized.Parent<T> build() {
return new SuperBuilderCustomized.Parent<T>(this);
}
}
int field1;
protected Parent(ParentBuilder<?, ?> b) {
protected Parent(ParentBuilder<T, ?, ?> b) {
super();
if ((b.field1 == 0))
throw new IllegalArgumentException("field1 must be != 0");
this.field1 = b.field1;
}
public static SuperBuilderCustomized.Parent.ParentBuilder<?, ?> builder(int field1) {
return new SuperBuilderCustomized.Parent.ParentBuilderImpl().field1(field1);
public static <T>SuperBuilderCustomized.Parent.ParentBuilder<T, ?, ?> builder(int field1) {
return new SuperBuilderCustomized.Parent.ParentBuilderImpl<T>().field1(field1);
}
}
public static @lombok.experimental.SuperBuilder class Child extends Parent {
public static @lombok.experimental.SuperBuilder class Child extends Parent<String> {
private static final class ChildBuilderImpl extends ChildBuilder<Child, ChildBuilderImpl> {
private ChildBuilderImpl() {
super();
Expand All @@ -55,7 +55,7 @@ private ChildBuilderImpl() {
return this;
}
}
public static abstract @java.lang.SuppressWarnings("all") @lombok.Generated class ChildBuilder<C extends SuperBuilderCustomized.Child, B extends SuperBuilderCustomized.Child.ChildBuilder<C, B>> extends Parent.ParentBuilder<C, B> {
public static abstract @java.lang.SuppressWarnings("all") @lombok.Generated class ChildBuilder<C extends SuperBuilderCustomized.Child, B extends SuperBuilderCustomized.Child.ChildBuilder<C, B>> extends Parent.ParentBuilder<String, C, B> {
private @java.lang.SuppressWarnings("all") @lombok.Generated double field2;
public ChildBuilder() {
super();
Expand Down
12 changes: 6 additions & 6 deletions test/transform/resource/before/SuperBuilderCustomized.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

public class SuperBuilderCustomized {
@lombok.experimental.SuperBuilder
public static class Parent {
public static abstract class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
public static class Parent<T> {
public static abstract class ParentBuilder<T, C extends Parent<T>, B extends ParentBuilder<T, C, B>> {
public B resetToDefault() {
field1 = 0;
return self();
Expand All @@ -16,19 +16,19 @@ public B field1(int field1) {
}
int field1;

protected Parent(ParentBuilder<?, ?> b) {
protected Parent(ParentBuilder<T, ?, ?> b) {
if (b.field1 == 0)
throw new IllegalArgumentException("field1 must be != 0");
this.field1 = b.field1;
}

public static SuperBuilderCustomized.Parent.ParentBuilder<?, ?> builder(int field1) {
return new SuperBuilderCustomized.Parent.ParentBuilderImpl().field1(field1);
public static <T> SuperBuilderCustomized.Parent.ParentBuilder<T, ?, ?> builder(int field1) {
return new SuperBuilderCustomized.Parent.ParentBuilderImpl<T>().field1(field1);
}
}

@lombok.experimental.SuperBuilder
public static class Child extends Parent {
public static class Child extends Parent<String> {
private static final class ChildBuilderImpl extends ChildBuilder<Child, ChildBuilderImpl> {
@Override
public Child build() {
Expand Down
Loading