Skip to content

Commit de62703

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Delay initializing the CacheBuilder logger until it is needed.
This is progress toward addressing the Java agent / `premain` problem discussed in #6566. (And we're careful to avoid lambdas and method references so as to avoid #6565.) RELNOTES=Fixed some problems with [using Guava from a Java Agent](#6566). (But we don't test that configuration, and we don't know how well we'll be able to keep it working.) PiperOrigin-RevId: 542887194
1 parent bf79253 commit de62703

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

android/guava/src/com/google/common/cache/CacheBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ public long read() {
271271
}
272272
};
273273

274-
private static final Logger logger = Logger.getLogger(CacheBuilder.class.getName());
274+
// We use a holder class to delay initialization: https://github.com/google/guava/issues/6566
275+
private static final class LoggerHolder {
276+
static final Logger logger = Logger.getLogger(CacheBuilder.class.getName());
277+
}
275278

276279
static final int UNSET_INT = -1;
277280

@@ -950,7 +953,8 @@ private void checkWeightWithWeigher() {
950953
checkState(maximumWeight != UNSET_INT, "weigher requires maximumWeight");
951954
} else {
952955
if (maximumWeight == UNSET_INT) {
953-
logger.log(Level.WARNING, "ignoring weigher specified without maximumWeight");
956+
LoggerHolder.logger.log(
957+
Level.WARNING, "ignoring weigher specified without maximumWeight");
954958
}
955959
}
956960
}

guava/src/com/google/common/cache/CacheBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ public long read() {
272272
}
273273
};
274274

275-
private static final Logger logger = Logger.getLogger(CacheBuilder.class.getName());
275+
// We use a holder class to delay initialization: https://github.com/google/guava/issues/6566
276+
private static final class LoggerHolder {
277+
static final Logger logger = Logger.getLogger(CacheBuilder.class.getName());
278+
}
276279

277280
static final int UNSET_INT = -1;
278281

@@ -1056,7 +1059,8 @@ private void checkWeightWithWeigher() {
10561059
checkState(maximumWeight != UNSET_INT, "weigher requires maximumWeight");
10571060
} else {
10581061
if (maximumWeight == UNSET_INT) {
1059-
logger.log(Level.WARNING, "ignoring weigher specified without maximumWeight");
1062+
LoggerHolder.logger.log(
1063+
Level.WARNING, "ignoring weigher specified without maximumWeight");
10601064
}
10611065
}
10621066
}

0 commit comments

Comments
 (0)