Internal API in the Java Module System

Overview

Internal APIs in the Java ecosystem are those APIs that are not exported by modules. These include most com.sun.* and all sun.* packages. They are designed for internal use within the JDK and are subject to change without notice, making them unsuitable for public use. The Java Module System enforces strong encapsulation to restrict access to these APIs, ensuring that only intended modules can access them.

Types of APIs

In the Java Development Kit (JDK), APIs are categorized into three types:

  1. Standardized APIs: These are public classes found in java.* and javax.* packages. They are stable and intended for public use.
  2. Supported APIs: These are public classes within jdk.* modules. They are also intended for public use but may not be as stable as standardized APIs.
  3. Internal APIs: These consist of non-exported classes and packages. They are not intended for public use and are subject to change, which can lead to compatibility issues.

The sun.misc.Unsafe Class

The sun.misc.Unsafe class is a well-known internal API that provides low-level operations, such as direct memory access. It is part of the jdk.unsupported module, which allows its use for a transitional period until standardized alternatives become available. Despite its utility, reliance on sun.misc.Unsafe is discouraged due to its instability and potential for breaking changes.

Changes and Removals

With the introduction of Java 9 and beyond, some internal APIs have been removed or renamed, which can break existing code that depends on them. Notable examples include sun.misc.BASE64Encoder, sun.misc.BASE64Decoder, and com.sun.image.codec.jpeg. Developers are encouraged to use alternatives, and tools like JDeps can assist in identifying dependencies on these internal APIs.

Analyzing Dependencies with JDeps

JDeps is a tool that analyzes Java bytecode to identify dependencies on internal APIs. By using the --jdk-internals option, developers can list all internal APIs that a project depends on. JDeps provides suggestions for replacements, helping developers transition away from internal APIs.

Compiling Against Internal APIs

To compile against internal APIs, developers can use options like --add-exports to make non-exported packages accessible during compilation. This allows code to compile against public classes within those packages, although it is generally discouraged due to the potential for future incompatibilities.

Exporting Internal APIs to Modules

Internal APIs can be exported to specific modules using the --add-exports option. This option allows specified packages to be accessed by designated modules or all unnamed modules, enabling access to public types within those packages.

Executing Against Internal APIs

Executing code that relies on internal APIs involves using JVM options such as --illegal-access to manage access at runtime. Additionally, --add-exports and --add-opens can be used to allow access to specific packages and enable deep reflection, respectively.

Compiler and JVM Options for Accessing JDK-Internal APIs

To access JDK-internal APIs, developers can use the following options:

  • Compiler Options:

    • --add-exports: Exports specific packages to modules, allowing access to public types in non-exported packages during compilation.
  • JVM Options:

    • --illegal-access: Manages illegal access to internal APIs at runtime.
    • --add-exports and --add-opens: Allow access to specific packages and enable deep reflection, respectively.

These options provide mechanisms to work with internal APIs, but developers should be cautious and consider transitioning to supported or standardized APIs to ensure future compatibility.

FAQ (Frequently asked questions)

Can you give examples of internal APIs removed or renamed in Java 9+?

How can developers find alternatives to removed internal APIs?

What is the purpose of using JDeps with the --jdk-internals option?