Skip to content

Commit

Permalink
SuperBuilder javac fix for nested generic types; fixes projectlombok#…
Browse files Browse the repository at this point in the history
  • Loading branch information
janrieke authored and Febell committed Mar 1, 2020
1 parent 1363b48 commit 42d5a36
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/core/lombok/javac/handlers/HandleSuperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.JCTree.JCWildcard;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
Expand Down Expand Up @@ -414,7 +415,7 @@ private JavacNode generateBuilderAbstractClass(JavacNode source, JavacNode tdPar
allTypeParams.add(maker.TypeParameter(tdParent.toName(classGenericName), List.<JCExpression>of(annotatedClass)));
// 2. The return type for all setter methods, named "B", which extends this builder class.
Name builderClassName = tdParent.toName(builderClass);
ListBuffer<JCExpression> typeParamsForBuilder = getTypeParamExpressions(typeParams, maker);
ListBuffer<JCExpression> typeParamsForBuilder = getTypeParamExpressions(typeParams, maker, source.getContext());
typeParamsForBuilder.add(maker.Ident(tdParent.toName(classGenericName)));
typeParamsForBuilder.add(maker.Ident(tdParent.toName(builderGenericName)));
JCTypeApply typeApply = maker.TypeApply(namePlusTypeParamsToTypeReference(maker, tdParent, builderClassName, false, List.<JCTypeParameter>nil()), typeParamsForBuilder.toList());
Expand All @@ -424,7 +425,7 @@ private JavacNode generateBuilderAbstractClass(JavacNode source, JavacNode tdPar
if (superclassBuilderClassExpression != null) {
// If the annotated class extends another class, we want this builder to extend the builder of the superclass.
// 1. Add the type parameters of the superclass.
typeParamsForBuilder = getTypeParamExpressions(superclassTypeParams, maker);
typeParamsForBuilder = getTypeParamExpressions(superclassTypeParams, maker, source.getContext());
// 2. Add the builder type params <C, B>.
typeParamsForBuilder.add(maker.Ident(tdParent.toName(classGenericName)));
typeParamsForBuilder.add(maker.Ident(tdParent.toName(builderGenericName)));
Expand Down Expand Up @@ -453,7 +454,7 @@ private JavacNode generateBuilderImplClass(JavacNode source, JavacNode tdParent,
// 2. The return type for all setter methods (named "B" in the abstract builder), which is this builder class.
JCExpression builderImplClassExpression = namePlusTypeParamsToTypeReference(maker, tdParent, tdParent.toName(builderImplClass), false, typeParams);

ListBuffer<JCExpression> typeParamsForBuilder = getTypeParamExpressions(typeParams, maker);
ListBuffer<JCExpression> typeParamsForBuilder = getTypeParamExpressions(typeParams, maker, source.getContext());
typeParamsForBuilder.add(annotatedClass);
typeParamsForBuilder.add(builderImplClassExpression);
extending = maker.TypeApply(extending, typeParamsForBuilder.toList());
Expand Down Expand Up @@ -522,7 +523,7 @@ private void generateBuilderBasedConstructor(CheckerFrameworkVersion cfv, JavacN
long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
Name builderClassname = typeNode.toName(builderClassName);
// First add all generics that are present on the parent type.
ListBuffer<JCExpression> typeParamsForBuilderParameter = getTypeParamExpressions(typeParams, maker);
ListBuffer<JCExpression> typeParamsForBuilderParameter = getTypeParamExpressions(typeParams, maker, typeNode.getContext());
// Now add the <?, ?>.
JCWildcard wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
typeParamsForBuilderParameter.add(wildcard);
Expand Down Expand Up @@ -685,7 +686,7 @@ private JCMethodDecl generateStaticFillValuesMethod(JavacNode type, String build

// 2nd parameter: "FoobarBuilder<?, ?> b" (plus generics on the annotated type)
// First add all generics that are present on the parent type.
ListBuffer<JCExpression> typeParamsForBuilderParameter = getTypeParamExpressions(typeParams, maker);
ListBuffer<JCExpression> typeParamsForBuilderParameter = getTypeParamExpressions(typeParams, maker, type.getContext());
// Now add the <?, ?>.
JCWildcard wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
typeParamsForBuilderParameter.add(wildcard);
Expand Down Expand Up @@ -1029,7 +1030,7 @@ private JavacNode findInnerClass(JavacNode parent, String name) {
return null;
}

private ListBuffer<JCExpression> getTypeParamExpressions(List<? extends JCTree> typeParams, JavacTreeMaker maker) {
private ListBuffer<JCExpression> getTypeParamExpressions(List<? extends JCTree> typeParams, JavacTreeMaker maker, Context context) {
ListBuffer<JCExpression> typeParamsForBuilderParameter = new ListBuffer<JCExpression>();
for (JCTree typeParam : typeParams) {
if (typeParam instanceof JCTypeParameter) {
Expand All @@ -1038,6 +1039,8 @@ private ListBuffer<JCExpression> getTypeParamExpressions(List<? extends JCTree>
typeParamsForBuilderParameter.add(maker.Ident(((JCIdent)typeParam).getName()));
} else if (typeParam instanceof JCFieldAccess) {
typeParamsForBuilderParameter.add(copySelect(maker, (JCFieldAccess) typeParam));
} else if (typeParam instanceof JCTypeApply) {
typeParamsForBuilderParameter.add(cloneType(maker, (JCTypeApply)typeParam, typeParam, context));
}
}
return typeParamsForBuilderParameter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class SuperBuilderNestedGenericTypes {
public static abstract class Generic<T extends Generic<?>> {
@java.lang.SuppressWarnings("all")
public static abstract class GenericBuilder<T extends Generic<?>, C extends SuperBuilderNestedGenericTypes.Generic<T>, B extends SuperBuilderNestedGenericTypes.Generic.GenericBuilder<T, C, B>> {
@java.lang.SuppressWarnings("all")
protected abstract B self();
@java.lang.SuppressWarnings("all")
public abstract C build();
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "SuperBuilderNestedGenericTypes.Generic.GenericBuilder()";
}
}
@java.lang.SuppressWarnings("all")
protected Generic(final SuperBuilderNestedGenericTypes.Generic.GenericBuilder<T, ?, ?> b) {
}
}
public static abstract class NestedGeneric<T extends OtherGeneric<?>> extends Generic<NestedGeneric<? extends OtherGeneric<?>>> {
@java.lang.SuppressWarnings("all")
public static abstract class NestedGenericBuilder<T extends OtherGeneric<?>, C extends SuperBuilderNestedGenericTypes.NestedGeneric<T>, B extends SuperBuilderNestedGenericTypes.NestedGeneric.NestedGenericBuilder<T, C, B>> extends Generic.GenericBuilder<NestedGeneric<? extends OtherGeneric<?>>, C, B> {
@java.lang.Override
@java.lang.SuppressWarnings("all")
protected abstract B self();
@java.lang.Override
@java.lang.SuppressWarnings("all")
public abstract C build();
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "SuperBuilderNestedGenericTypes.NestedGeneric.NestedGenericBuilder(super=" + super.toString() + ")";
}
}
@java.lang.SuppressWarnings("all")
protected NestedGeneric(final SuperBuilderNestedGenericTypes.NestedGeneric.NestedGenericBuilder<T, ?, ?> b) {
super(b);
}
}
public interface OtherGeneric<T> {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class SuperBuilderNestedGenericTypes {
public static abstract @lombok.experimental.SuperBuilder class Generic<T extends Generic<?>> {
public static abstract @java.lang.SuppressWarnings("all") class GenericBuilder<T extends Generic<?>, C extends SuperBuilderNestedGenericTypes.Generic<T>, B extends SuperBuilderNestedGenericTypes.Generic.GenericBuilder<T, C, B>> {
public GenericBuilder() {
super();
}
protected abstract @java.lang.SuppressWarnings("all") B self();
public abstract @java.lang.SuppressWarnings("all") C build();
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
return "SuperBuilderNestedGenericTypes.Generic.GenericBuilder()";
}
}
protected @java.lang.SuppressWarnings("all") Generic(final SuperBuilderNestedGenericTypes.Generic.GenericBuilder<T, ?, ?> b) {
super();
}
}
public static abstract @lombok.experimental.SuperBuilder class NestedGeneric<T extends OtherGeneric<?>> extends Generic<NestedGeneric<? extends OtherGeneric<?>>> {
public static abstract @java.lang.SuppressWarnings("all") class NestedGenericBuilder<T extends OtherGeneric<?>, C extends SuperBuilderNestedGenericTypes.NestedGeneric<T>, B extends SuperBuilderNestedGenericTypes.NestedGeneric.NestedGenericBuilder<T, C, B>> extends Generic.GenericBuilder<NestedGeneric<? extends OtherGeneric<?>>, C, B> {
public NestedGenericBuilder() {
super();
}
protected abstract @java.lang.Override @java.lang.SuppressWarnings("all") B self();
public abstract @java.lang.Override @java.lang.SuppressWarnings("all") C build();
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
return (("SuperBuilderNestedGenericTypes.NestedGeneric.NestedGenericBuilder(super=" + super.toString()) + ")");
}
}
protected @java.lang.SuppressWarnings("all") NestedGeneric(final SuperBuilderNestedGenericTypes.NestedGeneric.NestedGenericBuilder<T, ?, ?> b) {
super(b);
}
}
public interface OtherGeneric<T> {
}
public SuperBuilderNestedGenericTypes() {
super();
}
}
12 changes: 12 additions & 0 deletions test/transform/resource/before/SuperBuilderNestedGenericTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class SuperBuilderNestedGenericTypes {
@lombok.experimental.SuperBuilder
public static abstract class Generic<T extends Generic<?>> {
}

@lombok.experimental.SuperBuilder
public static abstract class NestedGeneric<T extends OtherGeneric<?>> extends Generic<NestedGeneric<? extends OtherGeneric<?>>> {
}

public interface OtherGeneric<T> {
}
}

0 comments on commit 42d5a36

Please sign in to comment.