diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 457246e772..05550a06b0 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -291,13 +291,6 @@ private ConfigurationKeys() {} */ public static final ConfigurationKey TO_STRING_INCLUDE_FIELD_NAMES = new ConfigurationKey("lombok.toString.includeFieldNames", "Include the field names in the generated toString method (default = true).") {}; - /** - * lombok configuration: {@code lombok.toString.onlyExplicitlyIncluded} = {@code true} | {@code false}. - * - * 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 TO_STRING_ONLY_EXPLICITLY_INCLUDED = new ConfigurationKey("lombok.toString.onlyExplicitlyIncluded", "Include only fields/methods explicitly marked with @ToString.Include. Otherwise, include all non-static, non-dollar-named fields (default = false).") {}; - // ----- Builder ----- /** diff --git a/src/core/lombok/core/AST.java b/src/core/lombok/core/AST.java index 738241e246..07d035c58b 100755 --- a/src/core/lombok/core/AST.java +++ b/src/core/lombok/core/AST.java @@ -454,9 +454,4 @@ public final T readConfigurationOr(ConfigurationKey key, T defaultValue) if (configTracker != null) configTracker.end(start); } } - - public boolean getBooleanAnnotationValue(AnnotationValues annotation, String annoMethod, ConfigurationKey confKey) { - Boolean conf = readConfiguration(confKey); - return annotation.isExplicit(annoMethod) || conf == null ? annotation.getAsBoolean(annoMethod) : conf; - } } diff --git a/src/core/lombok/core/handlers/InclusionExclusionUtils.java b/src/core/lombok/core/handlers/InclusionExclusionUtils.java index 2a519b82da..0aa6c47bba 100644 --- a/src/core/lombok/core/handlers/InclusionExclusionUtils.java +++ b/src/core/lombok/core/handlers/InclusionExclusionUtils.java @@ -113,14 +113,10 @@ private static String innerAnnName(Class type) { } private static , L extends LombokNode, N, I extends Annotation> List> handleIncludeExcludeMarking(Class inclType, String replaceName, Class exclType, LombokNode typeNode, AnnotationValues annotation, LombokNode annotationNode, boolean includeTransient) { - boolean onlyExplicitlyIncluded = annotation != null ? annotation.getAsBoolean("onlyExplicitlyIncluded") : false; - return handleIncludeExcludeMarking(inclType, onlyExplicitlyIncluded, replaceName, exclType, typeNode, annotation, annotationNode, includeTransient); - } - - private static , L extends LombokNode, N, I extends Annotation> List> handleIncludeExcludeMarking(Class inclType, boolean onlyExplicitlyIncluded, String replaceName, Class exclType, LombokNode typeNode, AnnotationValues annotation, LombokNode annotationNode, boolean includeTransient) { List oldExcludes = (annotation != null && annotation.isExplicit("exclude")) ? annotation.getAsStringList("exclude") : null; List oldIncludes = (annotation != null && annotation.isExplicit("of")) ? annotation.getAsStringList("of") : null; + boolean onlyExplicitlyIncluded = annotation != null ? annotation.getAsBoolean("onlyExplicitlyIncluded") : false; boolean memberAnnotationMode = onlyExplicitlyIncluded; List> members = new ArrayList>(); List namesToAutoExclude = new ArrayList(); @@ -207,14 +203,14 @@ private static , L extends LombokNode, N, I exte return members; } - public static , L extends LombokNode, N> List> handleToStringMarking(LombokNode typeNode, boolean onlyExplicitlyIncluded, AnnotationValues annotation, LombokNode annotationNode) { - List> members = handleIncludeExcludeMarking(ToString.Include.class, onlyExplicitlyIncluded, "name", ToString.Exclude.class, typeNode, annotation, annotationNode, true); + public static , L extends LombokNode, N> List> handleToStringMarking(LombokNode typeNode, AnnotationValues annotation, LombokNode annotationNode) { + List> members = handleIncludeExcludeMarking(ToString.Include.class, "name", ToString.Exclude.class, typeNode, annotation, annotationNode, true); Collections.sort(members, new Comparator>() { @Override public int compare(Included a, Included b) { int ra = a.getInc() == null ? 0 : a.getInc().rank(); int rb = b.getInc() == null ? 0 : b.getInc().rank(); - + return compareRankOrPosition(ra, rb, a.getNode(), b.getNode()); } }); @@ -223,28 +219,28 @@ public static , L extends LombokNode, N> List, L extends LombokNode, N> List> handleEqualsAndHashCodeMarking(LombokNode typeNode, AnnotationValues annotation, LombokNode annotationNode) { List> members = handleIncludeExcludeMarking(EqualsAndHashCode.Include.class, "replaces", EqualsAndHashCode.Exclude.class, typeNode, annotation, annotationNode, false); - + Collections.sort(members, new Comparator>() { @Override public int compare(Included a, Included b) { int ra = a.hasExplicitRank() ? a.getInc().rank() : HandlerUtil.defaultEqualsAndHashcodeIncludeRank(a.node.fieldOrMethodBaseType()); int rb = b.hasExplicitRank() ? b.getInc().rank() : HandlerUtil.defaultEqualsAndHashcodeIncludeRank(b.node.fieldOrMethodBaseType()); - + return compareRankOrPosition(ra, rb, a.getNode(), b.getNode()); } }); return members; } - + private static , L extends LombokNode, N> int compareRankOrPosition(int ra, int rb, LombokNode nodeA, LombokNode nodeB) { if (ra < rb) return +1; if (ra > rb) return -1; - + int pa = nodeA.getStartPos(); int pb = nodeB.getStartPos(); - + if (pa < pb) return -1; if (pa > pb) return +1; - + return 0; } } diff --git a/src/core/lombok/eclipse/handlers/HandleToString.java b/src/core/lombok/eclipse/handlers/HandleToString.java index 6beaa84889..05b0e06991 100644 --- a/src/core/lombok/eclipse/handlers/HandleToString.java +++ b/src/core/lombok/eclipse/handlers/HandleToString.java @@ -76,8 +76,7 @@ public void handle(AnnotationValues annotation, Annotation ast, Eclips handleFlagUsage(annotationNode, ConfigurationKeys.TO_STRING_FLAG_USAGE, "@ToString"); ToString ann = annotation.getInstance(); - boolean onlyExplicitlyIncluded = annotationNode.getAst().getBooleanAnnotationValue(annotation, "onlyExplicitlyIncluded", ConfigurationKeys.TO_STRING_ONLY_EXPLICITLY_INCLUDED); - List> members = InclusionExclusionUtils.handleToStringMarking(annotationNode.up(), onlyExplicitlyIncluded, annotation, annotationNode); + List> members = InclusionExclusionUtils.handleToStringMarking(annotationNode.up(), annotation, annotationNode); if (members == null) return; Boolean callSuper = ann.callSuper(); @@ -100,14 +99,16 @@ public void generateToStringForType(EclipseNode typeNode, EclipseNode errorNode) return; } - AnnotationValues anno = AnnotationValues.of(ToString.class); - boolean includeFieldNames = typeNode.getAst().getBooleanAnnotationValue(anno, "includeFieldNames", ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES); - boolean onlyExplicitlyIncluded = typeNode.getAst().getBooleanAnnotationValue(anno, "onlyExplicitlyIncluded", ConfigurationKeys.TO_STRING_ONLY_EXPLICITLY_INCLUDED); + boolean includeFieldNames = true; + try { + Boolean configuration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES); + includeFieldNames = configuration != null ? configuration : ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue(); + } catch (Exception ignore) {} Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS); FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD; - List> members = InclusionExclusionUtils.handleToStringMarking(typeNode, onlyExplicitlyIncluded, null, null); + List> members = InclusionExclusionUtils.handleToStringMarking(typeNode, null, null); generateToString(typeNode, errorNode, members, includeFieldNames, null, false, access); } diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java index 8a0bc686c7..249993ee30 100644 --- a/src/core/lombok/javac/handlers/HandleToString.java +++ b/src/core/lombok/javac/handlers/HandleToString.java @@ -65,8 +65,7 @@ public class HandleToString extends JavacAnnotationHandler { deleteAnnotationIfNeccessary(annotationNode, ToString.class); ToString ann = annotation.getInstance(); - boolean onlyExplicitlyIncluded = annotationNode.getAst().getBooleanAnnotationValue(annotation, "onlyExplicitlyIncluded", ConfigurationKeys.TO_STRING_ONLY_EXPLICITLY_INCLUDED); - java.util.List> members = InclusionExclusionUtils.handleToStringMarking(annotationNode.up(), onlyExplicitlyIncluded, annotation, annotationNode); + java.util.List> members = InclusionExclusionUtils.handleToStringMarking(annotationNode.up(), annotation, annotationNode); if (members == null) return; Boolean callSuper = ann.callSuper(); @@ -77,9 +76,10 @@ public class HandleToString extends JavacAnnotationHandler { 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); + Boolean fieldNamesConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES); + boolean includeNames = annotation.isExplicit("includeFieldNames") || fieldNamesConfiguration == null ? ann.includeFieldNames() : fieldNamesConfiguration; - generateToString(annotationNode.up(), annotationNode, members, includeFieldNames, callSuper, true, fieldAccess); + generateToString(annotationNode.up(), annotationNode, members, includeNames, callSuper, true, fieldAccess); } public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) { @@ -88,14 +88,16 @@ public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) { return; } - AnnotationValues anno = AnnotationValues.of(ToString.class); - boolean includeFieldNames = typeNode.getAst().getBooleanAnnotationValue(anno, "includeFieldNames", ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES); - boolean onlyExplicitlyIncluded = typeNode.getAst().getBooleanAnnotationValue(anno, "onlyExplicitlyIncluded", ConfigurationKeys.TO_STRING_ONLY_EXPLICITLY_INCLUDED); + boolean includeFieldNames = true; + try { + Boolean configuration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES); + includeFieldNames = configuration != null ? configuration : ((Boolean) ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue(); + } catch (Exception ignore) {} Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS); FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD; - java.util.List> members = InclusionExclusionUtils.handleToStringMarking(typeNode, onlyExplicitlyIncluded, null, null); + java.util.List> members = InclusionExclusionUtils.handleToStringMarking(typeNode, null, null); generateToString(typeNode, errorNode, members, includeFieldNames, null, false, access); } diff --git a/test/transform/resource/after-delombok/ToStringExplicitIncludeConf.java b/test/transform/resource/after-delombok/ToStringExplicitIncludeConf.java deleted file mode 100644 index a1bd8ed9ce..0000000000 --- a/test/transform/resource/after-delombok/ToStringExplicitIncludeConf.java +++ /dev/null @@ -1,9 +0,0 @@ -class ToStringExplicitIncludeConf { - int x; - int y; - @java.lang.Override - @java.lang.SuppressWarnings("all") - public java.lang.String toString() { - return "ToStringExplicitIncludeConf(" + this.y + ")"; - } -} diff --git a/test/transform/resource/after-ecj/ToStringExplicitIncludeConf.java b/test/transform/resource/after-ecj/ToStringExplicitIncludeConf.java deleted file mode 100644 index a2b801c6bc..0000000000 --- a/test/transform/resource/after-ecj/ToStringExplicitIncludeConf.java +++ /dev/null @@ -1,10 +0,0 @@ -@lombok.ToString class ToStringExplicitIncludeConf { - int x; - @lombok.ToString.Include int y; - ToStringExplicitIncludeConf() { - super(); - } - public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { - return (("ToStringExplicitIncludeConf(y=" + this.y) + ")"); - } -} diff --git a/test/transform/resource/before/ToStringExplicitIncludeConf.java b/test/transform/resource/before/ToStringExplicitIncludeConf.java deleted file mode 100644 index ee49c754c5..0000000000 --- a/test/transform/resource/before/ToStringExplicitIncludeConf.java +++ /dev/null @@ -1,7 +0,0 @@ -//CONF: lombok.toString.onlyExplicitlyIncluded = true - -@lombok.ToString -class ToStringExplicitIncludeConf { - int x; - @lombok.ToString.Include int y; -} diff --git a/website/templates/features/ToString.html b/website/templates/features/ToString.html index 87e066493a..456092d53c 100644 --- a/website/templates/features/ToString.html +++ b/website/templates/features/ToString.html @@ -32,11 +32,6 @@ lombok.toString.callSuper = [call | skip | warn] (default: skip)
If set to call, lombok will generate calls to the superclass implementation of toString if your class extends something. If set to skip no such call is generated. If set to warn no such call is generated either, but lombok does generate a warning to tell you about it. -
- lombok.toString.onlyExplicitlyIncluded = [true | false] (default: false) -
- If set to false (default), all fields (unless static, name starts with a dollar, or otherwise excluded for obvious reasons) serve as the default set of things to include in the toString, modifiable by using the @ToString.Exclude and @ToString.Include options. - If set to true, nothing is included unless explicitly marked with @ToString.Include.
lombok.toString.flagUsage = [warning | error] (default: not set)