jtableclassloaderjava-10synth

Is it possible to reflect over a JDK 10 private method (SynthLookAndFeel - the method "paintGrid" in SynthTableUI class)?


I'm currently modifying my GUI with SynthLookAndFeel, and I need to repaint the table Grid when I paint my table, but this method is private in JDK and cannot be invoked when the "paintGrid" method is retrieved through reflection (it's too complicated to override other public methods to paint the table grid).


Solution

  • You should look for a solution that does not require the use of internal APIs because that is brittle and can fail on any minor Java update. That said... after retrieving the Method instance, call setAccessible(true) on it - that will allow you to invoke it.

    On JDK 9 and later, this leads to a warning on the console that you are using internal APIs - in future Java versions (maybe as early as 12) that access will be forbidden and your code will fail with an exception. To get around the warning and error, add the command line flag --add-opens java.desktop/javax.swing.plaf.synth=ALL-UNNAMED (replace ALL-UNNAMED with your module's name if you create one) to the java command that launches your app.