Skip to content

Commit

Permalink
Make a class initialization deadlock more impossible
Browse files Browse the repository at this point in the history
The following field create a class initialization cycle, since `Base64Encoding` extends `BaseEncoding`:

```
  private static final BaseEncoding BASE64_URL = new Base64Encoding(...);
```

In practice this example is probably harmless, because `Base64Encoding` is already package-private and there are no references outside this file. Making it explicitly private makes it more obviously correct (to Error Prone, and hopefully also to human readers), since it ensures the only way to get an instance of `Base64Encoding` is to first initialize `BaseEncoding`.

See also https://errorprone.info/bugpattern/ClassInitializationDeadlock

RELNOTES=N/A
PiperOrigin-RevId: 616154233
  • Loading branch information
cushon authored and Google Java Core Libraries committed Mar 15, 2024
1 parent d5fbcca commit c99891c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions android/guava/src/com/google/common/io/BaseEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ public int hashCode() {
}
}

static class StandardBaseEncoding extends BaseEncoding {
private static class StandardBaseEncoding extends BaseEncoding {
final Alphabet alphabet;

@CheckForNull final Character paddingChar;
Expand Down Expand Up @@ -948,7 +948,7 @@ public int hashCode() {
}
}

static final class Base16Encoding extends StandardBaseEncoding {
private static final class Base16Encoding extends StandardBaseEncoding {
final char[] encoding = new char[512];

Base16Encoding(String name, String alphabetChars) {
Expand Down Expand Up @@ -995,7 +995,7 @@ BaseEncoding newInstance(Alphabet alphabet, @CheckForNull Character paddingChar)
}
}

static final class Base64Encoding extends StandardBaseEncoding {
private static final class Base64Encoding extends StandardBaseEncoding {
Base64Encoding(String name, String alphabetChars, @CheckForNull Character paddingChar) {
this(new Alphabet(name, alphabetChars.toCharArray()), paddingChar);
}
Expand Down
6 changes: 3 additions & 3 deletions guava/src/com/google/common/io/BaseEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ public int hashCode() {
}
}

static class StandardBaseEncoding extends BaseEncoding {
private static class StandardBaseEncoding extends BaseEncoding {
final Alphabet alphabet;

@CheckForNull final Character paddingChar;
Expand Down Expand Up @@ -948,7 +948,7 @@ public int hashCode() {
}
}

static final class Base16Encoding extends StandardBaseEncoding {
private static final class Base16Encoding extends StandardBaseEncoding {
final char[] encoding = new char[512];

Base16Encoding(String name, String alphabetChars) {
Expand Down Expand Up @@ -995,7 +995,7 @@ BaseEncoding newInstance(Alphabet alphabet, @CheckForNull Character paddingChar)
}
}

static final class Base64Encoding extends StandardBaseEncoding {
private static final class Base64Encoding extends StandardBaseEncoding {
Base64Encoding(String name, String alphabetChars, @CheckForNull Character paddingChar) {
this(new Alphabet(name, alphabetChars.toCharArray()), paddingChar);
}
Expand Down

0 comments on commit c99891c

Please sign in to comment.