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

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

Closed
oowekyala opened this issue Jun 10, 2019 · 3 comments · Fixed by #5286
Closed

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

oowekyala opened this issue Jun 10, 2019 · 3 comments · Fixed by #5286
Milestone

Comments

@oowekyala
Copy link
Member

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?

@oowekyala oowekyala mentioned this issue Jun 10, 2019
7 tasks
@adangel
Copy link
Member

adangel commented Jun 10, 2019

@jsotuyod
Copy link
Member

I believe this is no longer an issue, right?
At least all Ant tests are emitting 0 warnings…

@adangel
Copy link
Member

adangel commented Oct 25, 2024

This warning is actually still printed out for Java11. With Java17 (and later), we already use the public API to determine the console encoding, but for older java version, we use reflection. Java 8 doesn't report this, but Java 11 reports this illegal reflective access operation by default.

@adangel adangel reopened this Oct 25, 2024
@adangel adangel added this to the 7.8.0 milestone Oct 25, 2024
@adangel adangel changed the title [core] Reflective access warnings on java > 9 [core] Reflective access warnings on java > 9 and java < 17 Oct 25, 2024
adangel added a commit to adangel/pmd that referenced this issue Oct 25, 2024
- for java 17+, there is public API to get the console encoding
  -> no problem
- for older java versions, try to use system property
  sun.jnu.encoding if it exists
- only then use the fall-backs with illegal reflective access
  to private fields/methods on java.io.Console
- Also avoid using reflection utils from apache commons, instead
  use reflection directly. The illegal access warnings are then
  properly reported against our class
  net.sourceforge.pmd.ant.Formatter.

Fixes pmd#1860
@adangel adangel changed the title [core] Reflective access warnings on java > 9 and java < 17 [ant Reflective access warnings on java > 9 and java < 17 Oct 27, 2024
@adangel adangel changed the title [ant Reflective access warnings on java > 9 and java < 17 [ant] Reflective access warnings on java > 9 and java < 17 Oct 27, 2024
adangel added a commit that referenced this issue Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants