ecj: type params for constructor, abstract builder class, self method

This commit is contained in:
Jan Rieke committed 2018-05-11 14:41:38 +02:00
1 parent e12a956c25
commit ee51d77dd6
2 files changed
+67 -130

No files matched your search

@@ -58,6 +58,7 @@ import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
@@ -393,7 +394,10 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
constructor.arguments = null;
TypeReference[] wildcards = new TypeReference[] {new Wildcard(Wildcard.UNBOUND), new Wildcard(Wildcard.UNBOUND)};
TypeReference builderType = new ParameterizedSingleTypeReference(builderClassName.toCharArray(), wildcards, 0, p);
constructor.arguments = new Argument[] {new Argument("b".toCharArray(), p, builderType, Modifier.FINAL)};
List<Statement> statements = new ArrayList<Statement>();
List<Statement> nullChecks = new ArrayList<Statement>();
@@ -428,7 +432,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
nullChecks.addAll(statements);
constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]);
constructor.arguments = new Argument[] {new Argument("b".toCharArray(), p, new SingleTypeReference(builderClassName.toCharArray(), p), Modifier.FINAL)};
constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);
@@ -448,21 +451,11 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
// }
private MethodDeclaration generateAbstractSelfMethod(EclipseNode tdParent, boolean override, String builderGenericName) {
// JavacTreeMaker maker = tdParent.getTreeMaker();
// List<JCAnnotation> annotations = List.nil();
// if (override) {
// JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(tdParent, "Override"), List.<JCExpression>nil());
// annotations = List.of(overrideAnnotation);
// }
// JCModifiers modifiers = maker.Modifiers(Flags.PROTECTED | Flags.ABSTRACT, annotations);
// Name name = tdParent.toName(SELF_METHOD);
// JCExpression returnType = maker.Ident(tdParent.toName(builderGenericName));
//
// return maker.MthodDef(modifiers, name, returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
// TODO: @Override annotation if override == true.
MethodDeclaration out = new MethodDeclaration(((CompilationUnitDeclaration) tdParent.top().get()).compilationResult);
out.selector = SELF_METHOD.toCharArray();
out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
out.modifiers = ClassFileConstants.AccAbstract;
out.modifiers = ClassFileConstants.AccAbstract | ClassFileConstants.AccProtected;
out.returnType = new SingleTypeReference(builderGenericName.toCharArray(), 0);
return out;
}
@@ -770,22 +763,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
return null;
}
private TypeParameter[] copyTypeParamsAndAddBuilderParams(TypeParameter[] typeParams, ASTNode source, String classGenericName, String builderGenericName) {
TypeParameter[] result = Arrays.copyOf(copyTypeParams(typeParams, source), typeParams.length + 2);
TypeParameter o = new TypeParameter();
setGeneratedBy(o, source);
o.name = classGenericName.toCharArray();
result[result.length - 2] = o;
o = new TypeParameter();
setGeneratedBy(o, source);
o.name = builderGenericName.toCharArray();
result[result.length - 1] = o;
return result;
}
public EclipseNode makeBuilderAbstractClass(EclipseNode tdParent, String builderClass,
TypeReference superclassBuilderClass, TypeParameter[] typeParams,
TypeParameter[] superclassTypeParams, ASTNode source, String classGenericName, String builderGenericName) {
@@ -795,28 +772,26 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
builder.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
builder.modifiers |= ClassFileConstants.AccPublic | ClassFileConstants.AccStatic | ClassFileConstants.AccAbstract;
builder.name = builderClass.toCharArray();
builder.typeParameters = copyTypeParamsAndAddBuilderParams(typeParams, source, classGenericName, builderGenericName);
builder.superclass = superclassBuilderClass;
// // Keep any type params of the annotated class.
// ListBuffer<JCTypeParameter> allTypeParams = new ListBuffer<JCTypeParameter>();
// allTypeParams.addAll(copyTypeParams(source, typeParams));
// // Add builder-specific type params required for inheritable builders.
// // 1. The return type for the build() method, named "C", which extends the annotated class.
// JCExpression annotatedClass = maker.Ident(tdParent.toName(tdParent.getName()));
// if (typeParams.nonEmpty()) {
// // Add type params of the annotated class.
// annotatedClass = maker.TypeApply(annotatedClass, getTypeParamExpressions(typeParams, maker).toList());
// }
// 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);
// typeParamsForBuilder.add(maker.Ident(tdParent.toName(classGenericName)));
// typeParamsForBuilder.add(maker.Ident(tdParent.toName(builderGenericName)));
// JCTypeApply typeApply = maker.TypeApply(maker.Ident(builderClassName), typeParamsForBuilder.toList());
// allTypeParams.add(maker.TypeParameter(tdParent.toName(builderGenericName), List.<JCExpression>of(typeApply)));
//
// Keep any type params of the annotated class.
builder.typeParameters = Arrays.copyOf(copyTypeParams(typeParams, source), typeParams.length + 2);
// Add builder-specific type params required for inheritable builders.
// 1. The return type for the build() method, named "C", which extends the annotated class.
TypeParameter o = new TypeParameter();
o.name = classGenericName.toCharArray();
o.type = cloneSelfType(tdParent, source);
builder.typeParameters[builder.typeParameters.length - 2] = o;
// 2. The return type for all setter methods, named "B", which extends this builder class.
o = new TypeParameter();
o.name = builderGenericName.toCharArray();
TypeReference[] typerefs = new TypeReference[] {
new SingleTypeReference(classGenericName.toCharArray(), 0),
new SingleTypeReference(builderGenericName.toCharArray(), 0)
};
o.type = new ParameterizedSingleTypeReference(builderClass.toCharArray(), typerefs, 0, 0);
builder.typeParameters[builder.typeParameters.length - 1] = o;
builder.superclass = superclassBuilderClass;
// JCExpression extending = null;
// if (superclassBuilderClassExpression != null) {
// // If the annotated class extends another class, we want this builder to extend the builder of the superclass.
@@ -827,8 +802,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
// typeParamsForBuilder.add(maker.Ident(tdParent.toName(builderGenericName)));
// extending = maker.TypeApply(superclassBuilderClassExpression, typeParamsForBuilder.toList());
// }
//
// JCClassDecl builder = maker.ClassDef(mods, builderClassName, allTypeParams.toList(), extending, List.<JCExpression>nil(), List.<JCTree>nil());
builder.traverse(new SetGeneratedByVisitor(source), (ClassScope) null);
return injectType(tdParent, builder);
@@ -1,99 +1,63 @@
import java.util.List;
public class SuperBuilderBasic {
public static @lombok.experimental.SuperBuilder class Parent {
public static abstract @java.lang.SuppressWarnings("all") class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
private @java.lang.SuppressWarnings("all") int field1;
private @java.lang.SuppressWarnings("all") java.util.ArrayList<String> items;
protected abstract @java.lang.SuppressWarnings("all") B self();
public abstract @java.lang.SuppressWarnings("all") C build();
public @java.lang.SuppressWarnings("all") B field1(final int field1) {
this.field1 = field1;
return self();
}
public @java.lang.SuppressWarnings("all") B item(final String item) {
if (this.items == null) this.items = new java.util.ArrayList<String>();
this.items.add(item);
return self();
}
public @java.lang.SuppressWarnings("all") B items(final java.util.Collection<? extends String> items) {
if (this.items == null) this.items = new java.util.ArrayList<String>();
this.items.addAll(items);
return self();
}
public @java.lang.SuppressWarnings("all") B clearItems() {
if (this.items != null) this.items.clear();
return self();
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
return "SuperBuilderBasic.Parent.ParentBuilder(field1=" + this.field1 + ", items=" + this.items + ")";
}
}
private static final @java.lang.SuppressWarnings("all") class ParentBuilderImpl extends ParentBuilder<Parent, ParentBuilderImpl> {
private @java.lang.SuppressWarnings("all") ParentBuilderImpl() {
}
protected @java.lang.Override @java.lang.SuppressWarnings("all") ParentBuilderImpl self() {
return this;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") Parent build() {
return new Parent(this);
}
}
public @java.lang.SuppressWarnings("all") static ParentBuilder<?, ?> builder() {
return new ParentBuilderImpl();
}
int field1;
@lombok.Singular List<String> items;
protected Parent(final ParentBuilder<?, ?> b) {
protected @java.lang.SuppressWarnings("all") Parent(final ParentBuilder<?, ?> b) {
this.field1 = b.field1;
java.util.List<String> items;
switch (b.items == null ? 0 : b.items.size()) {
case 0:
items = java.util.Collections.emptyList();
break;
case 1:
items = java.util.Collections.singletonList(b.items.get(0));
break;
default:
items = java.util.Collections.unmodifiableList(new java.util.ArrayList<String>(b.items));
}
this.items = items;
}
@java.lang.SuppressWarnings("all")
public static abstract class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
@java.lang.SuppressWarnings("all")
private int field1;
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<String> items;
@java.lang.SuppressWarnings("all")
protected abstract B self();
@java.lang.SuppressWarnings("all")
public abstract C build();
@java.lang.SuppressWarnings("all")
public B field1(final int field1) {
this.field1 = field1;
return self();
}
@java.lang.SuppressWarnings("all")
public B item(final String item) {
if (this.items == null) this.items = new java.util.ArrayList<String>();
this.items.add(item);
return self();
}
@java.lang.SuppressWarnings("all")
public B items(final java.util.Collection<? extends String> items) {
if (this.items == null) this.items = new java.util.ArrayList<String>();
this.items.addAll(items);
return self();
}
@java.lang.SuppressWarnings("all")
public B clearItems() {
if (this.items != null) this.items.clear();
return self();
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "SuperBuilderBasic.Parent.ParentBuilder(field1=" + this.field1 + ", items=" + this.items + ")";
}
}
@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")
public static ParentBuilder<?, ?> builder() {
return new ParentBuilderImpl();
}
}
public static @lombok.experimental.SuperBuilder class Child extends Parent {