Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe to Suppress Illegal Reflective Access Warnings #20

Open
tkvangorder opened this issue Jun 1, 2021 · 2 comments
Open

Recipe to Suppress Illegal Reflective Access Warnings #20

tkvangorder opened this issue Jun 1, 2021 · 2 comments
Labels
recipe Recipe requested

Comments

@tkvangorder
Copy link
Contributor

The Java module system was introduced in Java 9 and provides a higher-level abstraction for grouping a set of java packages and resources along with additional meta-data. The meta-data is used to identify what services the module offers, what dependencies the module requires, and provides a mechanism for explicitly defining which module classes are “visible” to Java classes that are external to the module.

The module system provides strong encapsulation and the core Java libraries, starting with Java 9, have been designed to use the module specification. The rules of the module system, if strictly enforced, introduce breaking changes to downstream projects that have not yet adopted the module system. In fact, it is very common for a typical Java application to have a mix of module-compliant code along with code that is not aware of modules.

Even as Java has reached Java 15, there are a large number of applications and libraries that are not compliant with the rules defined by the Java module system. Rather than breaking those libraries, the Java runtime has been configured to allow mixed-use applications. If an application makes an illegal, reflective call to a module’s unpublished resource, a warning will be logged.

The default behavior of the Java runtime is to log a warning the first time an illegal access call is made. All subsequent calls will not be logged and the warning looks similar to the following:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file.....)
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illage-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

This warning, while valid, produces noise in an organization's logging infrastructure and can be suppressed when packaging an application into an executable JAR file by adding the following to the JAR's manifest:

<Add-Opens>java.base/java.lang java.base/java.util java.base/java.lang.reflect java.base/java.text java.desktop/java.awt.font</Add-Opens>

This solution will suppress the warning in the deployed artifacts while still surfacing the warning when developers run the application from their development environments.

This recipe can be used to add or modify the maven-jar-plugin to add manifest warnings. It will need to target pom files that build FAT application JARs or WAR files. The recipe can be configured with a list of packages for the manifest entries:

<!-- This is to suppress Java 11 reflective access warnings when running the application from a jar file. -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifestEntries>
                <Add-Opens>java.base/java.lang java.base/java.util java.base/java.lang.reflect java.base/java.text java.desktop/java.awt.font</Add-Opens>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
@traceyyoshima traceyyoshima self-assigned this Jun 1, 2021
@tkvangorder
Copy link
Contributor Author

The task should ONLY add the configuration when the pom is generating a fat/executable jar/war.

@tkvangorder tkvangorder reopened this Jun 1, 2021
@traceyyoshima
Copy link
Contributor

Note: handling this correctly relies on openrewrite/rewrite#585. We currently do have support in the Pom model / Resolver for plugins/pluginManagement.

@traceyyoshima traceyyoshima removed their assignment Jun 3, 2021
@tkvangorder tkvangorder added the enhancement New feature or request label Jun 12, 2021
@pway99 pway99 moved this to Icebox in OpenRewrite Mar 1, 2022
@tkvangorder tkvangorder moved this from Icebox to Backlog in OpenRewrite Mar 1, 2022
@tkvangorder tkvangorder moved this from Backlog to Ice Box in OpenRewrite Apr 8, 2022
@tkvangorder tkvangorder moved this from Request for help to Future in OpenRewrite Apr 18, 2022
@tkvangorder tkvangorder added recipe Recipe requested and removed enhancement New feature or request labels Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants