Interface AccessCondition


public interface AccessCondition
A condition that must be satisfied to register elements for dynamic access (i.e., reflection, serialization, JNI access, resource access, and foreign access at run time). AccessCondition is used for programmatic metadata registration in conjunction with: Conditions should be used whenever possible to constrain unnecessary growth of the binary size.

There are currently two types of conditions:

  • typeReached(Class) - satisfied when the type is both reachable by static analysis at build time, and reached at run time.
  • unconditional() - a condition that is always satisfied. This condition should be avoided to prevent unnecessary increases in binary size.

Conditions can only be created via the unconditional() and typeReached(Class) factory methods. These methods are best used with static import methods. For example:

reflection.register(unconditional(), ReflectivelyAccessed.class);
reflection.register(typeReached(ConditionalType.class), ConditionallyAccessed.class)
Since:
25.0.1
  • Method Details

    • unconditional

      static AccessCondition unconditional()
      Returns a condition that is always satisfied. Any element that is predicated with this condition will always be included and accessible.
      Returns:
      instance of the condition
      Since:
      25.0.1
    • typeReached

      static AccessCondition typeReached(Class<?> type)
      Creates the typeReached condition that is satisfied when the type is reached at run time. A type is reached at run time, if the class-initialization is triggered for that type (right before the first step of initialization described in Java Spec - Initialization), or any of the type's subtypes are reached. Elements predicated with this condition will be included in the image only if the type is reachable at build time, but will be accessible when the type is reached at run time.

      Example:

      public class App {
          public static void main(String[] args) {
              // ConditionalType not reached => element access not allowed
              Class<?> clazz = ConditionalType.class;
              // ConditionalType not reached (ConditionalType.class doesn't start class initialization)
              // => element access not allowed
              ConditionalType.singleton();
              // ConditionalType reached (already initialized) => element access allowed
          }
      }
      
      class SuperType {
          static {
              // ConditionalType reached (subtype reached) => element access allowed
          }
      }
      
      class ConditionalType extends SuperType {
          static {
              // ConditionalType reached (before static initializer) => element access allowed
          }
      
          static ConditionalType singleton() {
              // ConditionalType reached (already initialized) => element access allowed
          }
      }
      

      A type is also considered as reached, if it is marked as --initialize-at-build-time or if any of its subtypes on the classpath are marked as --initialize-at-build-time. Array types (e.g., int[]) are never marked as reached and therefore cannot be used as the type in a condition.

      Parameters:
      type - the type that has to be reached for this condition to be satisfied, must not be null
      Returns:
      instance of the condition
      Since:
      25.0.1