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 1 commit 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
2 changes: 1 addition & 1 deletion src/core/lombok/javac/handlers/HandleSuperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ private boolean constructorExists(JavacNode type, String builderClassName) {
if (lastIndexOfDot >= 0) {
typeName = typeName.substring(lastIndexOfDot+1);
}
if ((builderClassName+"<?, ?>").equals(typeName))
if (typeName.startsWith(builderClassName) && typeName.endsWith("?, ?>"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code before was not optimal already, as String comparisons tend to break quickly. Imagine something like Java extending the diamond operator also for ?, so that you can write:
protected Parent(ParentBuilder<> b) {...}
Thus, we should use the opportunity to improve it. I suggest doing an instanceof check and only use the generic toString as a fallback:

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))
	return true;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll work on that. I won't like have a chance until a couple weeks from now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, using toString() to compare generics is a nono, though I find the idea of falling back on that in case a series of instanceof fails a great idea. @ntroutman still working on it, or?

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")
private int field1;
public B resetToDefault() {
Expand All @@ -23,31 +23,31 @@ public java.lang.String toString() {
}
}
int field1;
protected Parent(ParentBuilder<?, ?> b) {
protected Parent(ParentBuilder<?, ?, ?> 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")
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")
private ParentBuilderImpl() {
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected SuperBuilderCustomized.Parent.ParentBuilderImpl self() {
protected SuperBuilderCustomized.Parent.ParentBuilderImpl<T> self() {
return this;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
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 @@ -68,7 +68,7 @@ protected SuperBuilderCustomized.Child.ChildBuilderImpl self() {
return new ChildBuilderImpl().field2(10.0);
}
@java.lang.SuppressWarnings("all")
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")
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") 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") class ParentBuilderImpl extends SuperBuilderCustomized.Parent.ParentBuilder<SuperBuilderCustomized.Parent, SuperBuilderCustomized.Parent.ParentBuilderImpl> {
private static final @java.lang.SuppressWarnings("all") 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") SuperBuilderCustomized.Parent.ParentBuilderImpl self() {
protected @java.lang.Override @java.lang.SuppressWarnings("all") SuperBuilderCustomized.Parent.ParentBuilderImpl<T> self() {
return this;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") SuperBuilderCustomized.Parent build() {
return new SuperBuilderCustomized.Parent(this);
public @java.lang.Override @java.lang.SuppressWarnings("all") SuperBuilderCustomized.Parent<T> build() {
return new SuperBuilderCustomized.Parent<T>(this);
}
}
int field1;
protected Parent(ParentBuilder<?, ?> b) {
protected Parent(ParentBuilder<?, ?, ?> 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") 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") class ChildBuilder<C extends SuperBuilderCustomized.Child, B extends SuperBuilderCustomized.Child.ChildBuilder<C, B>> extends Parent.ParentBuilder<String, C, B> {
private @java.lang.SuppressWarnings("all") 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<?, ?, ?> 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