Fix: toStringForLoggingLazy() was broken on some compilers, e.g. Eclipse

It seems different compilers generate different method names for
lambdas. This change avoids the use of lambdas.

Change-Id: I4159adfa279cf48a7a6dab2d3ca76e1d3483a053
This commit is contained in:
Joerg Zieren
2019-10-11 14:14:35 +02:00
parent 5ffd069cc4
commit ea277c4397

View File

@@ -183,8 +183,13 @@ public abstract class Metadata {
* @return string representation of this instance that is suitable for logging * @return string representation of this instance that is suitable for logging
*/ */
LazyArg<String> toStringForLoggingLazy() { LazyArg<String> toStringForLoggingLazy() {
return LazyArgs.lazy( // Don't use a lambda because different compilers generate different method names for lambdas,
() -> { // e.g. "lambda$myFunction$0" vs. just "lambda$0" in Eclipse. We need to identify the method
// by name to skip it and avoid infinite recursion.
return LazyArgs.lazy(this::toStringForLoggingImpl);
}
private String toStringForLoggingImpl() {
// Append class name. // Append class name.
String className = getClass().getSimpleName(); String className = getClass().getSimpleName();
if (className.startsWith("AutoValue_")) { if (className.startsWith("AutoValue_")) {
@@ -201,8 +206,9 @@ public abstract class Metadata {
continue; continue;
} }
if (method.getName().matches("(lambda\\$)?toStringForLoggingLazy(\\$0)?")) { if (method.getName().equals("toStringForLoggingLazy")
// skip toStringForLoggingLazy() and the lambda itself || method.getName().equals("toStringForLoggingImpl")) {
// Don't call myself in infinite recursion.
continue; continue;
} }
@@ -216,9 +222,7 @@ public abstract class Metadata {
Object returnValue; Object returnValue;
try { try {
returnValue = method.invoke(this); returnValue = method.invoke(this);
} catch (IllegalArgumentException } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
| IllegalAccessException
| InvocationTargetException e) {
// should never happen // should never happen
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
@@ -239,7 +243,6 @@ public abstract class Metadata {
} }
return stringHelper.toString(); return stringHelper.toString();
});
} }
public static Metadata.Builder builder() { public static Metadata.Builder builder() {