Skip to content

Commit 14cce76

Browse files
committed
[builder] big refactor: Fixing CheckerFramework features + all params 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.
1 parent 07e921d commit 14cce76

27 files changed

+1163
-999
lines changed

src/core/lombok/eclipse/handlers/HandleBuilder.java

Lines changed: 115 additions & 103 deletions
Large diffs are not rendered by default.

src/core/lombok/eclipse/handlers/HandleConstructor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,12 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
461461
constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[0]);
462462

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

468467
constructor.annotations = copyAnnotations(source,
469468
onConstructor.toArray(new Annotation[0]),
470-
constructorProperties,
471-
checkerFramework);
469+
constructorProperties);
472470
}
473471

474472
constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);
@@ -536,6 +534,11 @@ public MethodDeclaration createStaticConstructor(AccessLevel level, String name,
536534
TypeDeclaration typeDecl = (TypeDeclaration) type.get();
537535
constructor.returnType = EclipseHandlerUtil.namePlusTypeParamsToTypeReference(type, typeDecl.typeParameters, p);
538536
constructor.annotations = null;
537+
if (getCheckerFrameworkVersion(type).generateUnique()) {
538+
int len = constructor.returnType.getTypeName().length;
539+
constructor.returnType.annotations = new Annotation[len][];
540+
constructor.returnType.annotations[len - 1] = new Annotation[] {generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__UNIQUE)};
541+
}
539542
constructor.selector = name.toCharArray();
540543
constructor.thrownExceptions = null;
541544
constructor.typeParameters = copyTypeParams(((TypeDeclaration) type.get()).typeParameters, source);
@@ -556,9 +559,7 @@ public MethodDeclaration createStaticConstructor(AccessLevel level, String name,
556559
assigns.add(nameRef);
557560

558561
Argument parameter = new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL);
559-
Annotation[] checkerFramework = null;
560-
if (getCheckerFrameworkVersion(fieldNode).generateUnique()) checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__UNIQUE) };
561-
parameter.annotations = copyAnnotations(source, findCopyableAnnotations(fieldNode), checkerFramework);
562+
parameter.annotations = copyAnnotations(source, findCopyableAnnotations(fieldNode));
562563
params.add(parameter);
563564
}
564565

src/core/lombok/eclipse/handlers/HandleSuperBuilder.java

Lines changed: 270 additions & 247 deletions
Large diffs are not rendered by default.

src/core/lombok/javac/handlers/HandleBuilder.java

Lines changed: 311 additions & 243 deletions
Large diffs are not rendered by default.

src/core/lombok/javac/handlers/HandleConstructor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ public static void addConstructorProperties(JCModifiers mods, JavacNode node, Li
377377
addConstructorProperties(mods, typeNode, fieldsToParam);
378378
}
379379
if (onConstructor != null) mods.annotations = mods.annotations.appendList(copyAnnotations(onConstructor));
380-
if (getCheckerFrameworkVersion(source).generateUnique()) mods.annotations = mods.annotations.prepend(maker.Annotation(genTypeRef(source, CheckerFrameworkVersion.NAME__UNIQUE), List.<JCExpression>nil()));
381380
return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("<init>"),
382381
null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(),
383382
maker.Block(0L, nullChecks.appendList(assigns).toList()), null), source.get(), typeNode.getContext());
@@ -456,7 +455,6 @@ public JCMethodDecl createStaticConstructor(String name, AccessLevel level, Java
456455
JCClassDecl type = (JCClassDecl) typeNode.get();
457456

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

461459
JCExpression returnType, constructorType;
462460

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

475475
for (JavacNode fieldNode : fields) {

src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public JCMethodDecl createHashCode(JavacNode typeNode, java.util.List<Included<J
234234
/* ... 1; */
235235
init = maker.Literal(1);
236236
}
237-
statements.append(maker.VarDef(maker.Modifiers(isEmpty ? finalFlag : 0), resultName, maker.TypeIdent(CTC_INT), init));
237+
statements.append(maker.VarDef(maker.Modifiers(isEmpty ? finalFlag : 0L), resultName, maker.TypeIdent(CTC_INT), init));
238238
}
239239

240240
for (Included<JavacNode, EqualsAndHashCode.Include> member : members) {

src/core/lombok/javac/handlers/HandleGetter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public List<JCStatement> createLazyGetterBody(JavacTreeMaker maker, JavacNode fi
380380

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

386386
/* if (value == null) { */ {

src/core/lombok/javac/handlers/HandleSetter.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,30 @@ public static JCMethodDecl createSetter(long access, boolean deprecate, JavacNod
226226
return d;
227227
}
228228

229+
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) {
230+
JCExpression returnType = null;
231+
JCReturn returnStatement = null;
232+
if (shouldReturnThis) {
233+
returnType = cloneSelfType(field);
234+
returnStatement = treeMaker.Return(treeMaker.Ident(field.toName("this")));
235+
}
236+
237+
JCMethodDecl d = createSetterWithRecv(access, deprecate, field, treeMaker, setterName, paramName, booleanFieldToSet, returnType, returnStatement, source, onMethod, onParam, recv);
238+
if (shouldReturnThis && getCheckerFrameworkVersion(source).generateReturnsReceiver()) {
239+
List<JCAnnotation> annotations = d.mods.annotations;
240+
if (annotations == null) annotations = List.nil();
241+
JCAnnotation anno = treeMaker.Annotation(genTypeRef(source, CheckerFrameworkVersion.NAME__RETURNS_RECEIVER), List.<JCExpression>nil());
242+
recursiveSetGeneratedBy(anno, source.get(), field.getContext());
243+
d.mods.annotations = annotations.prepend(anno);
244+
}
245+
return d;
246+
}
247+
229248
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) {
249+
return createSetterWithRecv(access, deprecate, field, treeMaker, setterName, paramName, booleanFieldToSet, methodType, returnStatement, source, onMethod, onParam, null);
250+
}
251+
252+
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) {
230253
if (setterName == null) return null;
231254

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

280-
JCMethodDecl methodDef = treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
281-
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
303+
JCMethodDecl methodDef;
304+
if (recv != null && treeMaker.hasMethodDefWithRecvParam()) {
305+
methodDef = treeMaker.MethodDefWithRecvParam(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
306+
methodGenericParams, recv, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
307+
} else {
308+
methodDef = treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
309+
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
310+
}
282311
if (returnStatement != null) createRelevantNonNullAnnotation(source, methodDef);
283312
JCMethodDecl decl = recursiveSetGeneratedBy(methodDef, source.get(), field.getContext());
284313
copyJavadoc(field, decl, CopyJavadoc.SETTER, returnStatement != null);

0 commit comments

Comments
 (0)