DefaultCharset: Update fix suggestion according to JEP 400
- Dominant language
- Java
- Stars
- 7.2k
- Forks
- 820
- Avg merge
- 5h 9m
- Merged PRs (30d)
- 50
Description
Fix suggestion is outdated for the default charset:
https://github.com/google/error-prone/blob/b349e812051a54ec54894dae9579897632759211/core/src/main/java/com/google/errorprone/bugpatterns/DefaultCharset.java#L85-L97
According to the [JEP 400](https://openjdk.java.net/jeps/400), quoting:
We propose to change the specification of `Charset.defaultCharset()` to say that the default charset is [UTF-8](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/charset/Charset.html#standard) unless configured otherwise by an implementation-specific means.
So that `Charset.defaultCharset()` isn't a real alternative to the constant `UTF-8` charset.
For example, JGit projects added `SystemReader#getDefaultCharset()` [method](https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/heads/master/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java#453):
```java
/**
* Retrieves the default {@link Charset} depending on the system locale.
*
* @return the {@link Charset}
* @since 6.0
* @see JEP 400
*/
public Charset getDefaultCharset() {
Charset result = defaultCharset;
if (result == null) {
// JEP 400: Java 18 populates this system property.
String encoding = getProperty("native.encoding"); //$NON-NLS-1$
try {
if (!StringUtils.isEmptyOrNull(encoding)) {
result = Charset.forName(encoding);
}
} catch (IllegalCharsetNameException
| UnsupportedCharsetException e) {
LOG.error(JGitText.get().logInvalidDefaultCharset, encoding);
}
if (result == null) {
// This is always UTF-8 on Java >= 18.
result = Charset.defaultCharset();
}
defaultCharset = result;
}
return result;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.