Skip to content
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/ConfigurationKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private ConfigurationKeys() {}
* If {@code true}, require a {@code @ToString.Include} annotation on any fields/no-args methods you want to include in lombok's generated `@ToString` method. Otherwise, every (non-static, non-dollar-named) field is included by default (default = false).
*/
public static final ConfigurationKey<Boolean> TO_STRING_ONLY_EXPLICITLY_INCLUDED = new ConfigurationKey<Boolean>("lombok.toString.onlyExplicitlyIncluded", "Include only fields/methods explicitly marked with @ToString.Include. Otherwise, include all non-static, non-dollar-named fields (default = false).") {};

// ----- Builder -----

/**
Expand Down
3 changes: 2 additions & 1 deletion src/core/lombok/ToString.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
* @return If {@code true}, don't include non-static fields automatically (default: {@code false}).
*/
boolean onlyExplicitlyIncluded() default false;

boolean skipNull() default false;

/**
* If present, do not include this field in the generated {@code toString}.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/eclipse/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
fieldNodes.add(new Included<EclipseNode, ToString.Include>(f, null, true, false));
}
}
MethodDeclaration md = HandleToString.createToString(job.builderType, fieldNodes, true, false, ast, FieldAccess.ALWAYS_FIELD);
MethodDeclaration md = HandleToString.createToString(job.builderType, fieldNodes, true, false, ast, FieldAccess.ALWAYS_FIELD, false);
if (md != null) injectMethod(job.builderType, md);
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ void setBuilderToAbstract() {
}
}
// Let toString() call super.toString() if there is a superclass, so that it also shows fields from the superclass' builder.
MethodDeclaration md = HandleToString.createToString(job.builderType, fieldNodes, true, superclassBuilderClass != null, ast, FieldAccess.ALWAYS_FIELD);
MethodDeclaration md = HandleToString.createToString(job.builderType, fieldNodes, true, superclassBuilderClass != null, ast, FieldAccess.ALWAYS_FIELD, false);
if (md != null) injectMethod(job.builderType, md);
}

Expand Down
19 changes: 12 additions & 7 deletions src/core/lombok/eclipse/handlers/HandleToString.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,18 @@ public void handle(AnnotationValues<ToString> annotation, Annotation ast, Eclips
if (members == null) return;

Boolean callSuper = ann.callSuper();

if (!annotation.isExplicit("callSuper")) callSuper = null;

Boolean skipNull = ann.skipNull();
if (!annotation.isExplicit("skipNull")) skipNull = null;

Boolean doNotUseGettersConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS);
boolean doNotUseGetters = annotation.isExplicit("doNotUseGetters") || doNotUseGettersConfiguration == null ? ann.doNotUseGetters() : doNotUseGettersConfiguration;
FieldAccess fieldAccess = doNotUseGetters ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER;

Boolean fieldNamesConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);
boolean includeFieldNames = annotation.isExplicit("includeFieldNames") || fieldNamesConfiguration == null ? ann.includeFieldNames() : fieldNamesConfiguration;

generateToString(annotationNode.up(), annotationNode, members, includeFieldNames, callSuper, true, fieldAccess);
generateToString(annotationNode.up(), annotationNode, members, includeFieldNames, callSuper, true, fieldAccess, skipNull);
}

public void generateToStringForType(EclipseNode typeNode, EclipseNode errorNode) {
Expand All @@ -108,11 +109,11 @@ public void generateToStringForType(EclipseNode typeNode, EclipseNode errorNode)
FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD;

List<Included<EclipseNode, ToString.Include>> members = InclusionExclusionUtils.handleToStringMarking(typeNode, onlyExplicitlyIncluded, null, null);
generateToString(typeNode, errorNode, members, includeFieldNames, null, false, access);
generateToString(typeNode, errorNode, members, includeFieldNames, null, false, access, null);
}

public void generateToString(EclipseNode typeNode, EclipseNode errorNode, List<Included<EclipseNode, ToString.Include>> members,
boolean includeFieldNames, Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess) {
boolean includeFieldNames, Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess, Boolean skipNull) {

if (!isClassOrEnum(typeNode)) {
errorNode.addError("@ToString is only supported on a class or enum.");
Expand Down Expand Up @@ -142,7 +143,10 @@ public void generateToString(EclipseNode typeNode, EclipseNode errorNode, List<I
}
}
}
MethodDeclaration toString = createToString(typeNode, members, includeFieldNames, callSuper, errorNode.get(), fieldAccess);
if(skipNull == null) {
skipNull = false;
}
MethodDeclaration toString = createToString(typeNode, members, includeFieldNames, callSuper, errorNode.get(), fieldAccess, skipNull);
injectMethod(typeNode, toString);
break;
case EXISTS_BY_LOMBOK:
Expand All @@ -156,7 +160,7 @@ public void generateToString(EclipseNode typeNode, EclipseNode errorNode, List<I
}

public static MethodDeclaration createToString(EclipseNode type, Collection<Included<EclipseNode, ToString.Include>> members,
boolean includeNames, boolean callSuper, ASTNode source, FieldAccess fieldAccess) {
boolean includeNames, boolean callSuper, ASTNode source, FieldAccess fieldAccess, boolean skipNull) {

String typeName = getTypeName(type);
boolean isEnum = type.isEnumType();
Expand Down Expand Up @@ -223,6 +227,7 @@ public static MethodDeclaration createToString(EclipseNode type, Collection<Incl
}

for (Included<EclipseNode, ToString.Include> member : members) {
if(skipNull && member.getInc() == null) continue;
EclipseNode memberNode = member.getNode();

TypeReference fieldType = getFieldType(memberNode, fieldAccess);
Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/javac/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ static class BuilderFieldData {
}
}

JCMethodDecl md = HandleToString.createToString(job.builderType, fieldNodes, true, false, FieldAccess.ALWAYS_FIELD, job.sourceNode);
JCMethodDecl md = HandleToString.createToString(job.builderType, fieldNodes, true, false, FieldAccess.ALWAYS_FIELD, job.sourceNode, false);
if (md != null) injectMethod(job.builderType, md);
}

Expand Down
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 @@ -356,7 +356,7 @@ public void handle(AnnotationValues<SuperBuilder> annotation, JCAnnotation ast,
}

// Let toString() call super.toString() if there is a superclass, so that it also shows fields from the superclass' builder.
JCMethodDecl toStringMethod = HandleToString.createToString(job.builderType, fieldNodes, true, superclassBuilderClass != null, FieldAccess.ALWAYS_FIELD, annotationNode);
JCMethodDecl toStringMethod = HandleToString.createToString(job.builderType, fieldNodes, true, superclassBuilderClass != null, FieldAccess.ALWAYS_FIELD, annotationNode, false);
if (toStringMethod != null) injectMethod(job.builderType, toStringMethod);

// If clean methods are requested, add them now.
Expand Down
19 changes: 12 additions & 7 deletions src/core/lombok/javac/handlers/HandleToString.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ public class HandleToString extends JavacAnnotationHandler<ToString> {
if (members == null) return;

Boolean callSuper = ann.callSuper();

if (!annotation.isExplicit("callSuper")) callSuper = null;

Boolean skipNull = ann.skipNull();
if (!annotation.isExplicit("skipNull")) skipNull = null;

Boolean doNotUseGettersConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS);
boolean doNotUseGetters = annotation.isExplicit("doNotUseGetters") || doNotUseGettersConfiguration == null ? ann.doNotUseGetters() : doNotUseGettersConfiguration;
FieldAccess fieldAccess = doNotUseGetters ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER;

boolean includeFieldNames = annotationNode.getAst().getBooleanAnnotationValue(annotation, "includeFieldNames", ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);

generateToString(annotationNode.up(), annotationNode, members, includeFieldNames, callSuper, true, fieldAccess);
generateToString(annotationNode.up(), annotationNode, members, includeFieldNames, callSuper, true, fieldAccess, skipNull);
}

public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) {
Expand All @@ -96,11 +97,11 @@ public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) {
FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD;

java.util.List<Included<JavacNode, ToString.Include>> members = InclusionExclusionUtils.handleToStringMarking(typeNode, onlyExplicitlyIncluded, null, null);
generateToString(typeNode, errorNode, members, includeFieldNames, null, false, access);
generateToString(typeNode, errorNode, members, includeFieldNames, null, false, access, null);
}

public void generateToString(JavacNode typeNode, JavacNode source, java.util.List<Included<JavacNode, ToString.Include>> members,
boolean includeFieldNames, Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess) {
boolean includeFieldNames, Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess, Boolean skipNull) {

if (!isClassOrEnum(typeNode)) {
source.addError("@ToString is only supported on a class or enum.");
Expand Down Expand Up @@ -130,7 +131,10 @@ public void generateToString(JavacNode typeNode, JavacNode source, java.util.Lis
}
}
}
JCMethodDecl method = createToString(typeNode, members, includeFieldNames, callSuper, fieldAccess, source);
if(skipNull == null) {
skipNull = false;
}
JCMethodDecl method = createToString(typeNode, members, includeFieldNames, callSuper, fieldAccess, source, skipNull);
injectMethod(typeNode, method);
break;
case EXISTS_BY_LOMBOK:
Expand All @@ -145,7 +149,7 @@ public void generateToString(JavacNode typeNode, JavacNode source, java.util.Lis
}

static JCMethodDecl createToString(JavacNode typeNode, Collection<Included<JavacNode, ToString.Include>> members,
boolean includeNames, boolean callSuper, FieldAccess fieldAccess, JavacNode source) {
boolean includeNames, boolean callSuper, FieldAccess fieldAccess, JavacNode source, boolean skipNull) {

JavacTreeMaker maker = typeNode.getTreeMaker();

Expand Down Expand Up @@ -196,6 +200,7 @@ static JCMethodDecl createToString(JavacNode typeNode, Collection<Included<Javac
}

for (Included<JavacNode, ToString.Include> member : members) {
if(skipNull && member.getInc() == null) continue;
JCExpression expr;

JCExpression memberAccessor;
Expand Down