# Monday, 19 December 2011
« New Development Snapshot | Main | IKVM.NET 0.46 Update 1 Release Candidate... »
Accessibility, Visibility and Transparency

There are a couple of subtle differences between the JVM and CLR with respect to member accessibility. For example, the JVM will allow you to access public members in non-public base classes:

package p1;

class Base {
    public int foo;
}

public class Derived extends Base {
}

package p2;

class Test {
    public static void main(String[] args) {
       p1.Derived d = new p1.Derived();
       d.foo = 42;
    }
}

If you compile p1 and p2 seperately with ikvmc and look inside the resulting assemblies, you'll see that ikvmc generated a foo property in Derived. It needs to do this, because the CLR won't allow code from another assembly to access the members of Base.

In the IKVM source code this type of access stubs is known as type 1. As you'd expect there are also type 2 access stubs. These are generated when a public (or protected) member exposes a non-public type:

package p1;

class Foo { }

public class Bar {
  public Foo field = new Foo();
}

package p2;

class Test {
    public static void main(String[] args) {
       p1.Bar b = new p1.Bar();
       System.out.println(b.field);
       b.field = null;
    }
}

Once again, if you compile p1 and p2 into separate assemblies with ikvmc, you'll see that field will be accessed through a property. In this case the property type is erased to the first public base type and a custom modifier is attached to make sure the property signature is unique (and to be able to recover the real type).

These type 2 access stubs were originally generated (in some cases) by ikvmc to support accessing these members from C#, because the CLR, like the JVM, doesn't care about the visibility of the types in a signature when it considers member accessibility.

However, since .NET 4.0 there is a new factor to consider, when level 2 transparent code  tries to access a member, the types in the member signature (except for the custom modifiers) must all be visible to the caller.

I have not seen this last fact documented anywhere yet.

Monday, 19 December 2011 11:57:13 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]