Skip to content

Commit

Permalink
[builder] big refactor: Fixing CheckerFramework features + all params…
Browse files Browse the repository at this point in the history
… now in an object

These handlers had methods with humongous argument lists, and they needed to grow even more in order
to accommodate some new needs to properly implement checkerframework (where annos can be type-use
based, which means they were being put in the wrong place.

void foo(com.foo.@x Bar paramName) // correct
void foo(@x com.foo.Bar paramName) // wrong

For example, the CalledMethod annotation is a type-use annotation.

This commit covers both that refactor and fixing checkerframework generation.
  • Loading branch information
rzwitserloot committed Oct 3, 2020
1 parent 07e921d commit 14cce76
Show file tree
Hide file tree
Showing 27 changed files with 1,163 additions and 999 deletions.
218 changes: 115 additions & 103 deletions src/core/lombok/eclipse/handlers/HandleBuilder.java

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/core/lombok/eclipse/handlers/HandleConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,12 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[0]);

/* Generate annotations that must be put on the generated method, and attach them. */ {
Annotation[] constructorProperties = null, checkerFramework = null;
Annotation[] constructorProperties = null;
if (addConstructorProperties && !isLocalType(type)) constructorProperties = createConstructorProperties(source, fieldsToParam);
if (getCheckerFrameworkVersion(type).generateUnique()) checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__UNIQUE) };

constructor.annotations = copyAnnotations(source,
onConstructor.toArray(new Annotation[0]),
constructorProperties,
checkerFramework);
constructorProperties);
}

constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);
Expand Down Expand Up @@ -536,6 +534,11 @@ public MethodDeclaration createStaticConstructor(AccessLevel level, String name,
TypeDeclaration typeDecl = (TypeDeclaration) type.get();
constructor.returnType = EclipseHandlerUtil.namePlusTypeParamsToTypeReference(type, typeDecl.typeParameters, p);
constructor.annotations = null;
if (getCheckerFrameworkVersion(type).generateUnique()) {
int len = constructor.returnType.getTypeName().length;
constructor.returnType.annotations = new Annotation[len][];
constructor.returnType.annotations[len - 1] = new Annotation[] {generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__UNIQUE)};
}
constructor.selector = name.toCharArray();
constructor.thrownExceptions = null;
constructor.typeParameters = copyTypeParams(((TypeDeclaration) type.get()).typeParameters, source);
Expand All @@ -556,9 +559,7 @@ public MethodDeclaration createStaticConstructor(AccessLevel level, String name,
assigns.add(nameRef);

Argument parameter = new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL);
Annotation[] checkerFramework = null;
if (getCheckerFrameworkVersion(fieldNode).generateUnique()) checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__UNIQUE) };
parameter.annotations = copyAnnotations(source, findCopyableAnnotations(fieldNode), checkerFramework);
parameter.annotations = copyAnnotations(source, findCopyableAnnotations(fieldNode));
params.add(parameter);
}

Expand Down
517 changes: 270 additions & 247 deletions src/core/lombok/eclipse/handlers/HandleSuperBuilder.java

Large diffs are not rendered by default.

554 changes: 311 additions & 243 deletions src/core/lombok/javac/handlers/HandleBuilder.java

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/core/lombok/javac/handlers/HandleConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ public static void addConstructorProperties(JCModifiers mods, JavacNode node, Li
addConstructorProperties(mods, typeNode, fieldsToParam);
}
if (onConstructor != null) mods.annotations = mods.annotations.appendList(copyAnnotations(onConstructor));
if (getCheckerFrameworkVersion(source).generateUnique()) mods.annotations = mods.annotations.prepend(maker.Annotation(genTypeRef(source, CheckerFrameworkVersion.NAME__UNIQUE), List.<JCExpression>nil()));
return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("<init>"),
null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(),
maker.Block(0L, nullChecks.appendList(assigns).toList()), null), source.get(), typeNode.getContext());
Expand Down Expand Up @@ -456,7 +455,6 @@ public JCMethodDecl createStaticConstructor(String name, AccessLevel level, Java
JCClassDecl type = (JCClassDecl) typeNode.get();

JCModifiers mods = maker.Modifiers(Flags.STATIC | toJavacModifier(level));
if (getCheckerFrameworkVersion(typeNode).generateUnique()) mods.annotations = mods.annotations.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__UNIQUE), List.<JCExpression>nil()));

JCExpression returnType, constructorType;

Expand All @@ -469,7 +467,9 @@ public JCMethodDecl createStaticConstructor(String name, AccessLevel level, Java
typeParams.append(maker.TypeParameter(param.name, param.bounds));
}
}
returnType = namePlusTypeParamsToTypeReference(maker, typeNode, type.typarams);
List<JCAnnotation> annsOnReturnType = List.nil();
if (getCheckerFrameworkVersion(typeNode).generateUnique()) annsOnReturnType = List.of(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__UNIQUE), List.<JCExpression>nil()));
returnType = namePlusTypeParamsToTypeReference(maker, typeNode, type.typarams, annsOnReturnType);
constructorType = namePlusTypeParamsToTypeReference(maker, typeNode, type.typarams);

for (JavacNode fieldNode : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public JCMethodDecl createHashCode(JavacNode typeNode, java.util.List<Included<J
/* ... 1; */
init = maker.Literal(1);
}
statements.append(maker.VarDef(maker.Modifiers(isEmpty ? finalFlag : 0), resultName, maker.TypeIdent(CTC_INT), init));
statements.append(maker.VarDef(maker.Modifiers(isEmpty ? finalFlag : 0L), resultName, maker.TypeIdent(CTC_INT), init));
}

for (Included<JavacNode, EqualsAndHashCode.Include> member : members) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/javac/handlers/HandleGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public List<JCStatement> createLazyGetterBody(JavacTreeMaker maker, JavacNode fi

/* java.lang.Object value = this.fieldName.get();*/ {
JCExpression valueVarType = genJavaLangTypeRef(fieldNode, "Object");
statements.append(maker.VarDef(maker.Modifiers(0), valueName, valueVarType, callGet(fieldNode, createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD))));
statements.append(maker.VarDef(maker.Modifiers(0L), valueName, valueVarType, callGet(fieldNode, createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD))));
}

/* if (value == null) { */ {
Expand Down
33 changes: 31 additions & 2 deletions src/core/lombok/javac/handlers/HandleSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,30 @@ public static JCMethodDecl createSetter(long access, boolean deprecate, JavacNod
return d;
}

public static JCMethodDecl createSetterWithRecv(long access, boolean deprecate, JavacNode field, JavacTreeMaker treeMaker, String setterName, Name paramName, Name booleanFieldToSet, boolean shouldReturnThis, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam, JCVariableDecl recv) {
JCExpression returnType = null;
JCReturn returnStatement = null;
if (shouldReturnThis) {
returnType = cloneSelfType(field);
returnStatement = treeMaker.Return(treeMaker.Ident(field.toName("this")));
}

JCMethodDecl d = createSetterWithRecv(access, deprecate, field, treeMaker, setterName, paramName, booleanFieldToSet, returnType, returnStatement, source, onMethod, onParam, recv);
if (shouldReturnThis && getCheckerFrameworkVersion(source).generateReturnsReceiver()) {
List<JCAnnotation> annotations = d.mods.annotations;
if (annotations == null) annotations = List.nil();
JCAnnotation anno = treeMaker.Annotation(genTypeRef(source, CheckerFrameworkVersion.NAME__RETURNS_RECEIVER), List.<JCExpression>nil());
recursiveSetGeneratedBy(anno, source.get(), field.getContext());
d.mods.annotations = annotations.prepend(anno);
}
return d;
}

public static JCMethodDecl createSetter(long access, boolean deprecate, JavacNode field, JavacTreeMaker treeMaker, String setterName, Name paramName, Name booleanFieldToSet, JCExpression methodType, JCStatement returnStatement, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
return createSetterWithRecv(access, deprecate, field, treeMaker, setterName, paramName, booleanFieldToSet, methodType, returnStatement, source, onMethod, onParam, null);
}

public static JCMethodDecl createSetterWithRecv(long access, boolean deprecate, JavacNode field, JavacTreeMaker treeMaker, String setterName, Name paramName, Name booleanFieldToSet, JCExpression methodType, JCStatement returnStatement, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam, JCVariableDecl recv) {
if (setterName == null) return null;

JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
Expand Down Expand Up @@ -277,8 +300,14 @@ public static JCMethodDecl createSetter(long access, boolean deprecate, JavacNod
annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));
}

JCMethodDecl methodDef = treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
JCMethodDecl methodDef;
if (recv != null && treeMaker.hasMethodDefWithRecvParam()) {
methodDef = treeMaker.MethodDefWithRecvParam(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
methodGenericParams, recv, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
} else {
methodDef = treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
}
if (returnStatement != null) createRelevantNonNullAnnotation(source, methodDef);
JCMethodDecl decl = recursiveSetGeneratedBy(methodDef, source.get(), field.getContext());
copyJavadoc(field, decl, CopyJavadoc.SETTER, returnStatement != null);
Expand Down
Loading

0 comments on commit 14cce76

Please sign in to comment.