Add PMD rules for default locale, charset and time zone #499
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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
LocaleandTimeZonecan 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.Charsethas no method to set the default, but it can still be set by setting a system property or theCOMPATflag. 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 defaultCharset, 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.