Package org.javagi.gobject.types
Class TypeCache
java.lang.Object
org.javagi.gobject.types.TypeCache
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanCheck if this class is already cached.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.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.getEnumConstructor(Type type) Get a function that will create a Enumeration instance for a given int value for the provided GType.static TypeReturn the GType that was registered for this class.static @Nullable Function<MemorySegment, ? extends Proxy> getTypeClassConstructor(Type type) Get the Java constructor for a typeclassstatic voidregister(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 classstatic <T extends Enum<T> & Enumeration>
voidregisterEnum(Class<T> cls, @Nullable Type type, @Nullable Function<Integer, ?> ctor) Register the type and constructor function for an enum/flags typestatic void
-
Constructor Details
-
TypeCache
public TypeCache()
-
-
Method Details
-
setTypeRegisterFunction
-
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 usingregister(Class, Type, Function, Function)).- Parameters:
address- address of TypeInstance object to obtain the type fromfallback- if none was found, this constructor will be registered for the type, and returned- Returns:
- the constructor, or
nullif address isnullor 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 registeredfallback- if none was found, this constructor will be registered for the type, and returned- Returns:
- the constructor, or
nullif address isnullor a null-pointer
-
getEnumConstructor
Get a function that will create a Enumeration instance for a given int value for the provided GType. For flags types, the function will createSet<Enumeration>instances.- Parameters:
type- the GType of the enum/flags type- Returns:
- the contructor function
-
getType
-
getTypeClassConstructor
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 Javatype- The registered GTypector- Constructor function for this typetypeClassCtor- Constructor function for the typeclass of this type
-
registerEnum
public static <T extends Enum<T> & Enumeration> 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 Javatype- The registered GTypector- Constructor function for this type
-
contains
Check if this class is already cached.- Parameters:
cls- the class to check- Returns:
- true when the class is cached in the TypeCache
-