mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-11 12:19:00 +00:00
[configuration] Added the configuration keys lombok.ToString.doNotUseGetters and lombok.ToString.includeFieldNames
This commit is contained in:
1 parent
143143276d
commit
e557413336
6 files changed
+115
-7
No files matched your search
@@ -112,6 +112,14 @@ public class ConfigurationKeys {
|
||||
public static final ConfigurationKey<FlagUsageType> EQUALS_AND_HASH_CODE_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.EqualsAndHashCode.flagUsage", "Emit a warning or error if @EqualsAndHashCode is used.") {};
|
||||
|
||||
// ----- ToString -----
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.ToString.doNotUseGetters} = {@code true} | {@code false}.
|
||||
*
|
||||
* For any class without an {@code @ToString} that explicitly defines the {@code doNotUseGetters} option, this value is used.
|
||||
*/
|
||||
public static final ConfigurationKey<Boolean> TO_STRING_DO_NOT_USE_GETTERS = new ConfigurationKey<Boolean>("lombok.ToString.doNotUseGetters", "Don't call the getters but use the fields directly in the generated toString method.") {};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.ToString.flagUsage} = {@code WARNING} | {@code ERROR}.
|
||||
*
|
||||
@@ -119,6 +127,13 @@ public class ConfigurationKeys {
|
||||
*/
|
||||
public static final ConfigurationKey<FlagUsageType> TO_STRING_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.ToString.flagUsage", "Emit a warning or error if @ToString is used.") {};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.ToString.includeFieldNames} = {@code true} | {@code false}.
|
||||
*
|
||||
* For any class without an {@code @ToString} that explicitly defines the {@code includeFieldNames} option, this value is used.
|
||||
*/
|
||||
public static final ConfigurationKey<Boolean> TO_STRING_INCLUDE_FIELD_NAMES = new ConfigurationKey<Boolean>("lombok.ToString.includeFieldNames", "Include the field names in the generated toString method.") {};
|
||||
|
||||
// ##### Standalones #####
|
||||
|
||||
// ----- Cleanup -----
|
||||
|
||||
@@ -35,11 +35,12 @@ import java.util.Set;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.ConfigurationKeys;
|
||||
import lombok.ToString;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseNode;
|
||||
import lombok.eclipse.handlers.EclipseHandlerUtil.FieldAccess;
|
||||
|
||||
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
@@ -90,7 +91,8 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> {
|
||||
|
||||
boolean includeFieldNames = true;
|
||||
try {
|
||||
includeFieldNames = ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue();
|
||||
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) {}
|
||||
generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false, FieldAccess.GETTER);
|
||||
}
|
||||
@@ -115,9 +117,14 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> {
|
||||
|
||||
checkForBogusFieldNames(typeNode, annotation);
|
||||
|
||||
FieldAccess fieldAccess = ann.doNotUseGetters() ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER;
|
||||
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;
|
||||
|
||||
generateToString(typeNode, annotationNode, excludes, includes, ann.includeFieldNames(), callSuper, true, fieldAccess);
|
||||
Boolean fieldNamesConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);
|
||||
boolean includeFieldNames = annotation.isExplicit("includeFieldNames") || fieldNamesConfiguration == null ? ann.includeFieldNames() : fieldNamesConfiguration;
|
||||
|
||||
generateToString(typeNode, annotationNode, excludes, includes, includeFieldNames, callSuper, true, fieldAccess);
|
||||
}
|
||||
|
||||
public void generateToString(EclipseNode typeNode, EclipseNode errorNode, List<String> excludes, List<String> includes,
|
||||
|
||||
@@ -95,9 +95,14 @@ public class HandleToString extends JavacAnnotationHandler<ToString> {
|
||||
annotation.setWarning("exclude", "exclude and of are mutually exclusive; the 'exclude' parameter will be ignored.");
|
||||
}
|
||||
|
||||
FieldAccess fieldAccess = ann.doNotUseGetters() ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER;
|
||||
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;
|
||||
|
||||
generateToString(typeNode, annotationNode, excludes, includes, ann.includeFieldNames(), callSuper, true, fieldAccess);
|
||||
Boolean fieldNamesConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);
|
||||
boolean includeFieldNames = annotation.isExplicit("includeFieldNames") || fieldNamesConfiguration == null ? ann.includeFieldNames() : fieldNamesConfiguration;
|
||||
|
||||
generateToString(typeNode, annotationNode, excludes, includes, includeFieldNames, callSuper, true, fieldAccess);
|
||||
}
|
||||
|
||||
public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) {
|
||||
@@ -109,7 +114,8 @@ public class HandleToString extends JavacAnnotationHandler<ToString> {
|
||||
|
||||
boolean includeFieldNames = true;
|
||||
try {
|
||||
includeFieldNames = ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue();
|
||||
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) {}
|
||||
generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false, FieldAccess.GETTER);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
class ToStringConfiguration {
|
||||
int x;
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public java.lang.String toString() {
|
||||
return "ToStringConfiguration(" + this.x + ")";
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public int getX() {
|
||||
return this.x;
|
||||
}
|
||||
}
|
||||
class ToStringConfiguration2 {
|
||||
int x;
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public java.lang.String toString() {
|
||||
return "ToStringConfiguration2(x=" + this.x + ")";
|
||||
}
|
||||
}
|
||||
class ToStringConfiguration3 {
|
||||
int x;
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public java.lang.String toString() {
|
||||
return "ToStringConfiguration3(" + this.getX() + ")";
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public int getX() {
|
||||
return this.x;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import lombok.ToString;
|
||||
import lombok.Getter;
|
||||
@ToString @Getter class ToStringConfiguration {
|
||||
int x;
|
||||
ToStringConfiguration() {
|
||||
super();
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
|
||||
return (("ToStringConfiguration(" + this.x) + ")");
|
||||
}
|
||||
public @java.lang.SuppressWarnings("all") int getX() {
|
||||
return this.x;
|
||||
}
|
||||
}
|
||||
@ToString(includeFieldNames = true) class ToStringConfiguration2 {
|
||||
int x;
|
||||
ToStringConfiguration2() {
|
||||
super();
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
|
||||
return (("ToStringConfiguration2(x=" + this.x) + ")");
|
||||
}
|
||||
}
|
||||
@ToString(doNotUseGetters = false) @Getter class ToStringConfiguration3 {
|
||||
int x;
|
||||
ToStringConfiguration3() {
|
||||
super();
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
|
||||
return (("ToStringConfiguration3(" + this.getX()) + ")");
|
||||
}
|
||||
public @java.lang.SuppressWarnings("all") int getX() {
|
||||
return this.x;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//CONF: lombok.ToString.includeFieldNames = false
|
||||
//CONF: lombok.ToString.doNotUseGetters = true
|
||||
import lombok.ToString;
|
||||
import lombok.Getter;
|
||||
@ToString @Getter class ToStringConfiguration {
|
||||
int x;
|
||||
}
|
||||
@ToString(includeFieldNames=true) class ToStringConfiguration2 {
|
||||
int x;
|
||||
}
|
||||
@ToString(doNotUseGetters=false) @Getter class ToStringConfiguration3 {
|
||||
int x;
|
||||
}
|
||||
Reference in new issue
Block a user