mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-11 03:13:38 +00:00
[fixes #1918] toString now supports a configkey to tell it how to deal with super.
This commit is contained in:
1 parent
2c0ec93155
commit
a73965b2b7
9 files changed
+101
-22
No files matched your search
@@ -4,6 +4,8 @@ Lombok Changelog
|
||||
### v1.18.5 "Edgy Guinea Pig"
|
||||
* BUGFIX: Since version 1.18.4, the delombok ant task didn't work and errored with a `NoClassDefFoundError`. [Issue #1932](https://github.com/rzwitserloot/lombok/issues/1932)
|
||||
* FEATURE: The `@FieldNameConstants` feature now allows you to write the inner type by hand and add whatever you like to it; lombok will add the constants to this class. See the updated [FieldNameConstants feature](https://projectlombok.org/features/experimental/FieldNameConstants) page.
|
||||
* FEATURE: There is now a `lombok.config` key to configure `@ToString`'s call super behavior; it's just like `@EqualsAndHashCode` which has had it for a while now. [Issue #1918](https://github.com/rzwitserloot/lombok/issues/1918)
|
||||
|
||||
### v1.18.4 (October 30th, 2018)
|
||||
* PLATFORM: Support for Eclipse Photon. [Issue #1831](https://github.com/rzwitserloot/lombok/issues/1831)
|
||||
* PLATFORM: Angular IDE is now recognized by the installer [Issue #1830](https://github.com/rzwitserloot/lombok/issues/1830)
|
||||
|
||||
@@ -214,7 +214,7 @@ public class ConfigurationKeys {
|
||||
*
|
||||
* For any class with an {@code @EqualsAndHashCode} annotation which extends a class other than {@code java.lang.Object}, should a call to superclass's implementation of {@code equals} and {@code hashCode} be included in the generated methods? (Default = warn)
|
||||
*/
|
||||
public static final ConfigurationKey<CallSuperType> EQUALS_AND_HASH_CODE_CALL_SUPER = new ConfigurationKey<CallSuperType>("lombok.equalsAndHashCode.callSuper", "When generating equals and hashCode for classes that don't extend Object, either automatically take into account superclass implementation (call), or don't (skip), or warn and don't (warn). (default = warn).") {};
|
||||
public static final ConfigurationKey<CallSuperType> EQUALS_AND_HASH_CODE_CALL_SUPER = new ConfigurationKey<CallSuperType>("lombok.equalsAndHashCode.callSuper", "When generating equals and hashCode for classes that extend something (other than Object), either automatically take into account superclass implementation (call), or don't (skip), or warn and don't (warn). (default = warn).") {};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.equalsAndHashCode.flagUsage} = {@code WARNING} | {@code ERROR}.
|
||||
@@ -232,6 +232,13 @@ public class ConfigurationKeys {
|
||||
*/
|
||||
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 (default = false).") {};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.toString.callSuper} = {@code call} | {@code ignore} | {@code warn}.
|
||||
*
|
||||
* For any class with an {@code @ToString} annotation which extends a class other than {@code java.lang.Object}, should a call to superclass's implementation of {@code toString} be included in the generated method? (Default = skip)
|
||||
*/
|
||||
public static final ConfigurationKey<CallSuperType> TO_STRING_CALL_SUPER = new ConfigurationKey<CallSuperType>("lombok.toString.callSuper", "When generating toString for classes that extend something (other than Object), either automatically take into account superclass implementation (call), or don't (skip), or warn and don't (warn). (default = warn).") {};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.toString.flagUsage} = {@code WARNING} | {@code ERROR}.
|
||||
*
|
||||
|
||||
@@ -36,6 +36,7 @@ import lombok.ConfigurationKeys;
|
||||
import lombok.ToString;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.configuration.CallSuperType;
|
||||
import lombok.core.handlers.InclusionExclusionUtils;
|
||||
import lombok.core.handlers.InclusionExclusionUtils.Included;
|
||||
import lombok.eclipse.Eclipse;
|
||||
@@ -119,12 +120,6 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> {
|
||||
boolean notAClass = (modifiers &
|
||||
(ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
|
||||
|
||||
if (callSuper == null) {
|
||||
try {
|
||||
callSuper = ((Boolean)ToString.class.getMethod("callSuper").getDefaultValue()).booleanValue();
|
||||
} catch (Exception ignore) {}
|
||||
}
|
||||
|
||||
if (typeDecl == null || notAClass) {
|
||||
errorNode.addError("@ToString is only supported on a class or enum.");
|
||||
return;
|
||||
@@ -132,6 +127,27 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> {
|
||||
|
||||
switch (methodExists("toString", typeNode, 0)) {
|
||||
case NOT_EXISTS:
|
||||
if (callSuper == null) {
|
||||
if (isDirectDescendantOfObject(typeNode)) {
|
||||
callSuper = false;
|
||||
} else {
|
||||
CallSuperType cst = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_CALL_SUPER);
|
||||
if (cst == null) cst = CallSuperType.SKIP;
|
||||
switch (cst) {
|
||||
default:
|
||||
case SKIP:
|
||||
callSuper = false;
|
||||
break;
|
||||
case WARN:
|
||||
errorNode.addWarning("Generating toString implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this intentional, add '@ToString(callSuper=false)' to your type.");
|
||||
callSuper = false;
|
||||
break;
|
||||
case CALL:
|
||||
callSuper = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
MethodDeclaration toString = createToString(typeNode, members, includeFieldNames, callSuper, errorNode.get(), fieldAccess);
|
||||
injectMethod(typeNode, toString);
|
||||
break;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2011 The Project Lombok Authors.
|
||||
* Copyright (C) 2010-2018 The Project Lombok Authors.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -52,8 +52,7 @@ class EmptyLombokFileObject implements LombokFileObject {
|
||||
@Override public boolean isNameCompatible(String simpleName, Kind kind) {
|
||||
String baseName = simpleName + kind.extension;
|
||||
return kind.equals(getKind())
|
||||
&& (baseName.equals(toUri().getPath())
|
||||
|| toUri().getPath().endsWith("/" + baseName));
|
||||
&& (baseName.equals(toUri().getPath()) || toUri().getPath().endsWith("/" + baseName));
|
||||
}
|
||||
|
||||
@Override public URI toUri() {
|
||||
@@ -112,12 +111,8 @@ class EmptyLombokFileObject implements LombokFileObject {
|
||||
}
|
||||
|
||||
@Override public boolean equals(Object obj) {
|
||||
if (!(obj instanceof EmptyLombokFileObject)) {
|
||||
return false;
|
||||
}
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof EmptyLombokFileObject)) return false;
|
||||
if (obj == this) return true;
|
||||
EmptyLombokFileObject other = (EmptyLombokFileObject) obj;
|
||||
return name.equals(other.name) && kind.equals(other.kind);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.util.Collection;
|
||||
import lombok.ConfigurationKeys;
|
||||
import lombok.ToString;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.configuration.CallSuperType;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.handlers.InclusionExclusionUtils;
|
||||
import lombok.core.handlers.InclusionExclusionUtils.Included;
|
||||
@@ -111,12 +112,6 @@ public class HandleToString extends JavacAnnotationHandler<ToString> {
|
||||
notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
|
||||
}
|
||||
|
||||
if (callSuper == null) {
|
||||
try {
|
||||
callSuper = ((Boolean) ToString.class.getMethod("callSuper").getDefaultValue()).booleanValue();
|
||||
} catch (Exception ignore) {}
|
||||
}
|
||||
|
||||
if (notAClass) {
|
||||
source.addError("@ToString is only supported on a class or enum.");
|
||||
return;
|
||||
@@ -124,6 +119,27 @@ public class HandleToString extends JavacAnnotationHandler<ToString> {
|
||||
|
||||
switch (methodExists("toString", typeNode, 0)) {
|
||||
case NOT_EXISTS:
|
||||
if (callSuper == null) {
|
||||
if (isDirectDescendantOfObject(typeNode)) {
|
||||
callSuper = false;
|
||||
} else {
|
||||
CallSuperType cst = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_CALL_SUPER);
|
||||
if (cst == null) cst = CallSuperType.SKIP;
|
||||
switch (cst) {
|
||||
default:
|
||||
case SKIP:
|
||||
callSuper = false;
|
||||
break;
|
||||
case WARN:
|
||||
source.addWarning("Generating toString implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@ToString(callSuper=false)' to your type.");
|
||||
callSuper = false;
|
||||
break;
|
||||
case CALL:
|
||||
callSuper = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
JCMethodDecl method = createToString(typeNode, members, includeFieldNames, callSuper, fieldAccess, source.get());
|
||||
injectMethod(typeNode, method);
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
class ToStringAutoSuperWithNoParent {
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public java.lang.String toString() {
|
||||
return "ToStringAutoSuperWithNoParent()";
|
||||
}
|
||||
}
|
||||
|
||||
class ToStringAutoSuperWithParent extends ToStringAutoSuperWithNoParent {
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public java.lang.String toString() {
|
||||
return "ToStringAutoSuperWithParent(super=" + super.toString() + ")";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
@lombok.ToString class ToStringAutoSuperWithNoParent {
|
||||
ToStringAutoSuperWithNoParent() {
|
||||
super();
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
|
||||
return "ToStringAutoSuperWithNoParent()";
|
||||
}
|
||||
}
|
||||
@lombok.ToString class ToStringAutoSuperWithParent extends ToStringAutoSuperWithNoParent {
|
||||
ToStringAutoSuperWithParent() {
|
||||
super();
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
|
||||
return (("ToStringAutoSuperWithParent(super=" + super.toString()) + ")");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//CONF: lombok.toString.callSuper = CALL
|
||||
@lombok.ToString
|
||||
class ToStringAutoSuperWithNoParent {
|
||||
}
|
||||
|
||||
@lombok.ToString
|
||||
class ToStringAutoSuperWithParent extends ToStringAutoSuperWithNoParent {
|
||||
}
|
||||
@@ -28,6 +28,10 @@
|
||||
<code>lombok.toString.doNotUseGetters</code> = [<code>true</code> | <code>false</code>] (default: false)
|
||||
</dt><dd>
|
||||
If set to <code>true</code>, lombok will access fields directly instead of using getters (if available) when generating <code>toString</code> methods. The annotation parameter '<code>doNotUseGetters</code>', if explicitly specified, takes precedence over this setting.
|
||||
</dd><dt>
|
||||
<code>lombok.toString.callSuper</code> = [<code>call</code> | <code>skip</code> | <code>warn</code>] (default: skip)
|
||||
</dt><dd>
|
||||
If set to <code>call</code>, lombok will generate calls to the superclass implementation of <code>toString</code> if your class extends something. If set to <code>skip</code> no such call is generated. If set to <code>warn</code> no such call is generated either, but lombok does generate a warning to tell you about it.
|
||||
</dd><dt>
|
||||
<code>lombok.toString.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)
|
||||
</dt><dd>
|
||||
|
||||
Reference in new issue
Block a user