ikvm icon indicating copy to clipboard operation
ikvm copied to clipboard

Feature Request: Simulate "package-private" scope (for use in Tests)

Open NightOwl888 opened this issue 1 year ago • 1 comments

Java has a package-private scope that is visible to all code within the same "package" (namespace) that .NET is lacking. This scope is often leveraged in Java tests. We currently cannot run tests that rely on package-private scope (signified with no scope identifier, which in .NET just means private) because in .NET these members are private.

As per the discussion on Discord, there are 2 possible ways to do this:

  1. Transform the scope of package-private to internal (Assembly) and mark the assembly with InternalsVisibleTo just for the tests.
  2. Transform the scope of package-private to public and mark every member with attributes to hide it from IDEs.

In general, it is considered a bad practice in Java to use package-private for any other purpose than tests, although there is nothing preventing a consumer from accessing package-private members by declaring the same package name in their project. So, option 1 could run into issues for any Java project that has broken this best practice.

In our discussion, it was argued to use option 2.

In either case, this may need to be more nuanced if there are Java serialization features that can be applied optionally for certain types of serialization of package-private members. Changing the scope of the members may also inadvertently change the scope for serialization, which could break serialization contracts. For this reason, it may be best to make this an optional feature rather than a required feature, at least until we get the kinks worked out of it.

NightOwl888 avatar Aug 24 '22 21:08 NightOwl888

The problem with (1) is except in some narrow cases, at the time of generating the primary assembly, it's not necessarily known what the name of the test assembly should be, whether there even is one, or multiple. It might be possible to do exclusively for Maven stuff, in the cases where the 'tests' signifier is present. But that's not a thing we should rely on.

wasabii avatar Aug 24 '22 23:08 wasabii