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
*/
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.
String className = getClass().getSimpleName();
if (className.startsWith("AutoValue_")) {
@@ -201,8 +206,9 @@ public abstract class Metadata {
continue;
}
if (method.getName().matches("(lambda\\$)?toStringForLoggingLazy(\\$0)?")) {
// skip toStringForLoggingLazy() and the lambda itself
if (method.getName().equals("toStringForLoggingLazy")
|| method.getName().equals("toStringForLoggingImpl")) {
// Don't call myself in infinite recursion.
continue;
}
@@ -216,9 +222,7 @@ public abstract class Metadata {
Object returnValue;
try {
returnValue = method.invoke(this);
} catch (IllegalArgumentException
| IllegalAccessException
| InvocationTargetException e) {
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
// should never happen
throw new IllegalStateException(e);
}
@@ -239,7 +243,6 @@ public abstract class Metadata {
}
return stringHelper.toString();
});
}
public static Metadata.Builder builder() {