Skip to content

[ant] Reflective access warnings on java > 9 and java < 17 #1860

Description

@oowekyala

From #584 :

I also receive the following warning message while running the Java Ant Task:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.sourceforge.pmd.ant.Formatter (file:/data/data/com.termux/files/home/LearnJava/lib/pmd-core-6.15.0.jar) to field java.io.Console.cs
WARNING: Please consider reporting this to the maintainers of net.sourceforge.pmd.ant.Formatter WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release.

I'm running Ant against an Open Jdk 1.10 version on Arch Linux on Termux.

Here's the relevant code:

private static String getConsoleEncoding() {
Console console = System.console();
// in case of pipe or redirect, no interactive console.
if (console != null) {
try {
Field f = Console.class.getDeclaredField("cs");
f.setAccessible(true);
Object res = f.get(console);
if (res instanceof Charset) {
return ((Charset) res).name();
}
} catch (ReflectiveOperationException ignored) {
// fall-through
}
return getNativeConsoleEncoding();
}
return null;
}
private static String getNativeConsoleEncoding() {
try {
Method m = Console.class.getDeclaredMethod("encoding");
m.setAccessible(true);
Object res = m.invoke(null);
if (res instanceof String) {
return (String) res;
}
} catch (ReflectiveOperationException ignored) {
// fall-through
}
return null;
}
}

Maybe there's a better way to fetch that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions