Every 6 months sees a release of a new major version of Java. Each major version gives the maintainers of Java the option to release new functionality but also to mark functionality deprecated. A deprecation signals that old ways of working are discouraged and might eventually be removed. When we updated to the most recent Long Term Support version of Java in our projects we ran into a few of these deprecations. Here is how we fixed them.
finalize() is deprecated for removal
Java 9 marked the Object.finalize() method as deprecated. A finalizer is a flawed solution to the problem of doing cleanups when an object is garbage collected. In Java 18 the method was marked as deprecated for removal, signalling that in some future Java release it will actually be removed. We used three different solutions for the finalizers we had in our codebase.
- Remove the finalizer.
In some cases we no longer needed the finalizer, because the object already performs all the cleanup that the finalizer would do as well. - Use try-with-resources.
Some objects have aclose()method that cleans up. Make the objectAutoCloseableand ensure all calls to those objects are wrapped in a try-with-resources. Then you no longer require the finalizer, because theclose()method is called in all situations. - Use the Cleaner API.
Java 9 introduced the Cleaner API which allows you to specify a clean up action to be executed when the object gets garbage collected. The Cleaner API comes in handy if you can’t make the objectAutoCloseable. For example an object that does not keep a reference or state of the resource to clean up.
Note that the cleanup method can not hold a reference to the object to clean, or that object would never be garbage collected.
For some of our finalizers it was not clear which solution we should use. For example if we could not guarantee that the object was always used in a try-with-resources block using static code analysis. In those cases we added some monitoring to the finalizer, so we are notified if the finalizer ever did actual clean up work. This monitoring showed that we can fix some cases by adding a try-with-resources, whereas others actually required the Cleaner API.
javax.security.auth.Subject’s doAs is replaced with callAs
The Subject.doAs method depends on AccessControlContext which, in conjunction with the Security Manager, is deprecated and subject to removal in a future release. However, performing work as a Subject is useful independent of the Security Manager. Thus, a replacement API named callAs has been added which can be used to perform the same work. Instead of a java.security.PrivilegedAction or java.security.PrivilegedExceptionAction it accepts a java.util.concurrent.Callable.
If you were using a PrivilegedAction before, you need to handle the Exception that Callable throws. For the PrivilegedExceptionAction the Callable is almost a drop-in replacement with the same signature, only a different method name call() instead of run().
Deprecations in generated code
We are using the jaxws-maven-plugin to turn WSDL definition files into Java code. The generated code contains new URL("url") which is now deprecated in favor of new URI("url").toUrl(). The maintainers of the jaxws-maven-plugin should fix the code generation. Until that time, we have a deprecation warning in our build log. As a workaround we implemented a step in our build process that adds @SuppressWarnings("deprecation") to the affected generated classes before compilation happens.
The trivial ones
Then finally there are the trivial deprecations, which are well documented in the @Deprecated tag. For example:
- Instead of
new Locale("nl")use the staticLocale.of("nl") - Do not use
Thread.getId()as it is not final and thus can be overwritten by subclasses. UseThread.threadId()instead.