Skip to content

Conversation

@Nadahar
Copy link

@Nadahar Nadahar commented Oct 28, 2025

When I work with OH code, I see reliance on default locale, charset or time zone from time to time. I can't pursue that every time, because I would never get to do what I originally intended to do. But, it is my impression, both from OH and from other projects, that there are many that aren't aware of the pitfalls of relying on these defaults.

They are all JVM wide. The default Locale and TimeZone can be set by any code running on the JVM at any time. It wasn't long ago where we found a case where GraalPy seems to set the JVM-wide time zone if you invoke certain Python commands. Any add-on, official or not, can do the same. So, in essence, these defaults can change at any time, and you have no reason to expect them to be what you want. They are also affected, by default, by an incorrectly configured system or java command-line arguments. Charset has no method to set the default, but it can still be set by setting a system property or the COMPAT flag. Prior to Java 18, it was also OS dependent, since Java 18 it is UTF-8 by default, but that doesn't mean that it's safe to assume that it is UTF-8.

It's thus almost never "safe" or desirable to use these defaults. There are other properties one can use if one wants to get the "native"/OS defaults, which should be used when that is the need. Except for that, it's almost always better to use an explicit value.

I discovered that PMD 7.17.0 includes a new standard rule, RelianceOnDefaultCharset, which detects use of the default Charset, but I could find no solution for the other two.

I've therefore downloaded the OpenJDK source code and grep'ed my way through it in search of methods that implicitly use the default locale or time zone. This isn't an easy task, some calls are convoluted, and it's not always easy to predict when they come into play. I've also excluded some things that seem completely irrelevant, like AWT or Swing methods. But, I believe that I've been able to catch the most common pitfalls.

Use of the default values will only generate warnings. Trying to set the defaults will generate errors. I don't know if there's any place in the code where these should be set, but if there is, I think they should rather be handled with suppressions, because that's normally a really bad thing to do (given that many things rely on them even though they shouldn't).

Fixes #496.

Note: Writing custom PMD rules is new to me, so I might have made some rookie mistakes.

Ravi Nadahar added 2 commits October 28, 2025 01:15
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
…ime zone

Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
@Nadahar Nadahar requested a review from a team as a code owner October 28, 2025 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default locale detection

1 participant