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: 542887194
cpovirk authored and Google Java Core Libraries committed Jun 23, 2023

Verified

This commit was signed with the committer’s verified signature.
j00bar Joshua "jag" Ginsberg
1 parent bf79253 commit de62703
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
@@ -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;

@@ -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");
}
}
}
8 changes: 6 additions & 2 deletions guava/src/com/google/common/cache/CacheBuilder.java
Original file line number Diff line number Diff line change
@@ -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;

@@ -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");
}
}
}

0 comments on commit de62703

Please sign in to comment.