vendredi 16 avril 2021

Java 9: 'opens' module for access by reflection not required?

The 'opens' keyword in the logic of Java modules allows reflective access (here). I've recently ported a complex JavaFX app. to Java 15 with success, but it was a nightmare because JavaFX is rightly based on reflective access. My question is about a more simple Java 15 application THAT PERFECTLY WORKS, but I expected a runtime error that DID NOT OCCUR! Why? Any highlight welcome... So, I have a very common client/server relationship between 2 modules as follows:

module PauWare2 {
exports com.pauware.pauware_engine.Core;
}
module My_device_module {
requires PauWare2;
}

One may observe that 'opens' IS NOT used in 'My_device_module'. In practice, PauWare2 accesses public methods by reflection:

    package My_device_package;
    public class My_device { // In 'My_device_module'
    public void a() {}
    com.pauware.pauware_engine.Core.AbstractStateMachine _My_device_state_machine; // PauWare2 reuse
    // Reflective access of 'a' method by '_My_device_state_machine'
    }

I expected a runtime error, which involves 'opens My_device_package to PauWare2;' in 'My_device_module'? However, as said, no problem at all... Any idea on that? THANKS in advance..





Aucun commentaire:

Enregistrer un commentaire