Skip to content

Commit

Permalink
Delay initializing the CacheBuilder logger until it is needed.
Browse files Browse the repository at this point in the history
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: 542247494
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jun 23, 2023
1 parent bf79253 commit 0bd3193
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions android/guava/src/com/google/common/cache/CacheBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ public long read() {
}
};

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

static final int UNSET_INT = -1;

Expand Down Expand Up @@ -950,7 +953,8 @@ private void checkWeightWithWeigher() {
checkState(maximumWeight != UNSET_INT, "weigher requires maximumWeight");
} else {
if (maximumWeight == UNSET_INT) {
logger.log(Level.WARNING, "ignoring weigher specified without maximumWeight");
LoggerHolder.logger.log(
Level.WARNING, "ignoring weigher specified without maximumWeight");
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions guava/src/com/google/common/cache/CacheBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ public long read() {
}
};

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

static final int UNSET_INT = -1;

Expand Down Expand Up @@ -1056,7 +1059,8 @@ private void checkWeightWithWeigher() {
checkState(maximumWeight != UNSET_INT, "weigher requires maximumWeight");
} else {
if (maximumWeight == UNSET_INT) {
logger.log(Level.WARNING, "ignoring weigher specified without maximumWeight");
LoggerHolder.logger.log(
Level.WARNING, "ignoring weigher specified without maximumWeight");
}
}
}
Expand Down

0 comments on commit 0bd3193

Please sign in to comment.