Home >Java >javaTutorial >What is Illegal Reflective Access in Java 9 and How Can I Fix It?

What is Illegal Reflective Access in Java 9 and How Can I Fix It?

DDD
DDDOriginal
2024-12-05 03:07:13219browse

What is Illegal Reflective Access in Java 9 and How Can I Fix It?

Illegal Reflective Access in Java 9

Following the introduction of modularity in Java 9, developers have encountered issues related to illegal reflective access. To fully grasp this concept, it's essential to first understand the Module System's "Relaxed-strong-encapsulation."

Definition of Illegal Reflective Access

Illegal reflective access refers to situations where reflective operations are performed in a manner not intended by the modular design principles. It violates the encapsulation barriers established between modules and their packages.

Circumstances Triggering the Warning

The warning for illegal reflective access is triggered when:

  • A module accesses internal packages of another module without using proper module declarations or VM arguments (e.g., --add-opens).
  • Compiled code accesses internal JDK APIs.

Relaxation of Capsule Principles

Java 9 introduced the --illegal-access launcher option with a default setting of permit to aid in migration. In permit mode:

  • The first illegal reflective access operation to a package generates a warning.
  • Subsequent operations do not trigger warnings.

Configuration Modes

The --illegal-access option can be configured with different modes:

  • debug: Provides warnings and stacktraces for each illegal access.
  • warn: Issues warnings for each illegal access.
  • deny: Disallows illegal access operations.

How to Identify and Fix

To avoid illegal reflective access, developers can:

  • Run applications with --illegal-access=deny to detect unauthorized access.
  • Identify static references to JDK APIs using the jdeps tool (with the --jdk-internals option).

Warning Message

Illegal reflective access operations result in warnings with the following format:

WARNING: Illegal reflective access by $PERPETRATOR to $VICTIM

where:

  • $PERPETRATOR: Type that invoked the reflective operation
  • $VICTIM: Member being accessed

Ensuring Future Compatibility

To ensure future compatibility, it's crucial to eliminate illegal reflective accesses and ensure modules follow proper encapsulation principles.

The above is the detailed content of What is Illegal Reflective Access in Java 9 and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn