Class TypeCache

java.lang.Object
org.javagi.gobject.types.TypeCache

@NullMarked public class TypeCache extends Object
A register of GTypes with a Java constructor for each GType. Using this register, the correct Java class is always instantiated, based on the GType of the native object instance.
  • Constructor Details

    • TypeCache

      public TypeCache()
  • Method Details

    • setTypeRegisterFunction

      public static void setTypeRegisterFunction(Class<? extends Proxy> cls, Function<Class<? extends Proxy>, Type> function)
    • getConstructor

      public static @Nullable Function<MemorySegment, ? extends Proxy> getConstructor(MemorySegment address, Function<MemorySegment, ? extends Proxy> fallback)
      Get the constructor from the type registry for the native object instance at the given memory address. The applicable constructor is determined based on the GType of the native object (as it was registered using register(Class, Type, Function, Function)).
      Parameters:
      address - address of TypeInstance object to obtain the type from
      fallback - if none was found, this constructor will be registered for the type, and returned
      Returns:
      the constructor, or null if address is null or a null-pointer
    • getConstructor

      public static @Nullable Function<MemorySegment, ? extends Proxy> getConstructor(Type type, @Nullable Function<MemorySegment, ? extends Proxy> fallback)
      Get the constructor from the type registry for the provided GType. If it isn't found, we are probably dealing with an anonymous subclass or interface implementation. So we try to find a constructor for the parent type and the implemented interfaces. If that works, it is registered as the constructor for the GType and returned. Otherwise, the provided fallback constructor is registered and returned.
      Parameters:
      type - the GType for which the constructor was registered
      fallback - if none was found, this constructor will be registered for the type, and returned
      Returns:
      the constructor, or null if address is null or a null-pointer
    • getEnumConstructor

      public static @Nullable Function<Integer,?> getEnumConstructor(Type type)
      Get a function that will create a Enumeration instance for a given int value for the provided GType. For flags types, the function will create Set<Enumeration> instances.
      Parameters:
      type - the GType of the enum/flags type
      Returns:
      the contructor function
    • getType

      public static Type getType(Class<?> cls)
      Return the GType that was registered for this class. If no type was registered yet, this method will try to register it, and then return the GType.
      Parameters:
      cls - a Java class
      Returns:
      the cached GType
    • getTypeClassConstructor

      public static @Nullable Function<MemorySegment, ? extends Proxy> getTypeClassConstructor(Type type)
      Get the Java constructor for a typeclass
      Parameters:
      type - a typeclass
      Returns:
      a constructor for a wrapper class in Java for the typeclass
    • register

      public static void register(Class<?> cls, @Nullable Type type, @Nullable Function<MemorySegment, ? extends Proxy> ctor, @Nullable Function<MemorySegment, ? extends Proxy> typeClassCtor)
      Register the type and constructor function for the provided class
      Parameters:
      cls - Class in Java
      type - The registered GType
      ctor - Constructor function for this type
      typeClassCtor - Constructor function for the typeclass of this type
    • registerEnum

      public static <T extends Enum<T>> void registerEnum(Class<T> cls, @Nullable Type type, @Nullable Function<Integer,?> ctor)
      Register the type and constructor function for an enum/flags type
      Parameters:
      cls - Class in Java
      type - The registered GType
      ctor - Constructor function for this type
    • contains

      public static boolean contains(Class<?> cls)
      Check if this class is already cached.
      Parameters:
      cls - the class to check
      Returns:
      true when the class is cached in the TypeCache