Closed baptistemanson closed 6 years ago
No, this proposal does not expose any mechanism for accessing private state or methods from outside the class. The tradeoff between encapsulation and reflection was discussed at length, for example in this thread, and ultimately we came down on the side of encapsulation.
Incidentally, one of the major features of Java 9, modules, was motivated in large part by the goal of allowing authors to prevent consumers from accessing their private state via reflection.
The current “hard private” disallows this.
However, you’ll be able to use decorators to expose private fields publicly:
class Example {
@visibleForTesting
#x = 1
}
Decorators seem to be pretty elegant for this. Thanks!
Closed as it seems like decorators resolve the concern satisfactorily. Thanks for explaining the existing proposals and plans, @bakkot and @jridgewell .
Hi all,
In Java, we can test private methods via Reflection. In PHP, we cannot test private methods. 30 PHP engineers in my company feel like in practice they need to test some private methods.
The usual consequences of a limitation like in PHP are:
The infamous "private" __SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED from React was used in Unit Testing as well if I'm not mistaken.
Is there any possibility to avoid this situation in JS? Like being able to access private methods with proxies/any other mean?