What about Array.newInstance for restricted value-types?

Gernot Neppert mcnepp02 at googlemail.com
Tue Jul 26 12:38:05 UTC 2022


There is an ongoing discussion about how to prevent the creation of 
values with non-sensible zero-defaults.

Currently, the consensus seems to be that restricting access to a 
value-type's "companion-type" will achieve this.

However, I found this Anomaly:

The method java.lang.reflect.Array.newInstance(Class<?> 
componentType,int length) currently does not take the accessibility of 
the "componentType" into account!

So, it looks as if one could still create flat, zero-initialized arrays 
of private companion-types from anywhere in the codebase:

package org.secrets;

public value class Bar {

     private value companion Bar.val;

}

package com.external;

import java.lang.reflect.Array;

public class Main {

      Bar[] bars = 
(Bar[])Array.newInstance(Class.forName("org.secrets.Bar.val"), 42);

}









More information about the valhalla-spec-comments mailing list