RFR: 8316971: Add Lint warning for restricted method calls

Maurizio Cimadamore mcimadamore at openjdk.org
Thu Sep 28 13:36:27 UTC 2023

On Thu, 28 Sep 2023 13:13:31 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> This patch adds a new lint warning category, namely `-Xlint:restricted` to enable warnings on restricted method calls.
> The patch is relatively straightforward: javac marks methods that are marked with the `@Restricted` annotation with a corresponding internal flag. This is done both in `Annotate` when compiling JDK from source, and in `ClassReader` when JDK classfiles are read. When calls to methods marked with the special flag are found, a new warning is issued.
> While there are some similarities between this new warning and the preview API warnings, the compiler does *not* emit a mandatory note when a compilation unit is found to have one or more restricted method calls. In other words, this is just a plain lint warning.
> The output from javac looks as follows:
> Foo.java:6: warning: [restricted] MemorySegment.reinterpret(long) is a restricted method.
>       Arena.ofAuto().allocate(10).reinterpret(100);
>                                  ^
>   (Restricted methods are unsafe, and, if used incorrectly, they might crash the JVM or result in memory corruption)

test/langtools/tools/javac/RestrictedMethods.java line 5:

> 3:  * @bug 8316971
> 4:  * @summary Smoke test for restricted method call warnings
> 5:  * @compile/fail/ref=RestrictedMethods.out -Xlint:restricted -Werror -XDrawDiagnostics --enable-preview --source ${jdk.version} RestrictedMethods.java

The `--enable-preview` related options will need to be removed when FFM is finalized


PR Review Comment: https://git.openjdk.org/jdk/pull/15964#discussion_r1340165979

More information about the compiler-dev mailing list