/** * Internal method. * * <p>Used to quickly load {@link LuaObjectMeta} instances by passing the instance. */ static void loadMeta(LuaObjectMeta metaInstance) { if (metaInstance.isPrimaryType()) { Lua.PMETA.put(metaInstance.getTargetObjectClass(), metaInstance); } else { Lua.META.put(metaInstance.getTargetObjectClass(), metaInstance); } }
/** * Internal method. * * <p>Returns a stored {@link LuaObjectMeta} instance. */ static LuaObjectMeta getMeta(Class<? extends LuaObjectMeta> metaClass) { for (LuaObjectMeta meta : Lua.PMETA.values()) { if (meta.getClass() == metaClass) { return meta; } } for (LuaObjectMeta meta : Lua.META.values()) { if (meta.getClass() == metaClass) { return meta; } } return null; }
/** * Returns the {@link LuaObjectMeta} registered with the provided typename. * * @param metaName the typename to match * @return the LuaObjectMeta matching the given typename, or null if doesn't exist */ public static LuaObjectMeta getMeta(String metaName) { for (LuaObjectMeta meta : Lua.PMETA.values()) { if (meta.getTypeName().equals(metaName)) { return meta; } } for (LuaObjectMeta meta : Lua.META.values()) { if (meta.getTypeName().equals(metaName)) { return meta; } } return null; }
/** * Internal method. * * <p>Used to quickly load {@link LuaObjectMeta} instances by passing the class. */ static void loadMeta(Class<? extends LuaObjectMeta> metaClass) { try { LuaObjectMeta meta = metaClass.newInstance(); if (meta.getTargetObjectClass() == null) { log.error( "Failed to load meta for meta class " + metaClass.getSimpleName() + " (invalid target class)"); return; } loadMeta(meta); } catch (Exception e) { log.error("Failed to load meta for meta class " + metaClass.getSimpleName(), e); } }
/** * Returns the given {@link LuaValue} as the appropriate type of {@link LuaObjectValue}, directed * by the provided {@link LuaObjectMeta} type. * * <p>If the provided value is not of the appropriate type, a {@link LuaError} is raised. * * @param value the LuaValue to check * @param clazz the target LuaObjectMeta type to check with * @return the appropriately typed LuaValue as a LuaObjectValue */ public static <T> LuaObjectValue<T> checkType( LuaValue value, Class<? extends LuaObjectMeta> clazz) { LuaObjectMeta meta = Lua.getMeta(clazz); if (meta == null) { throw new IllegalArgumentException( "Meta class " + clazz.getSimpleName() + " isn't registered"); } if (value instanceof LuaObjectValue) { LuaObjectValue<?> objectValue = ((LuaObjectValue<?>) value); if (meta.getTargetObjectClass().isInstance(objectValue.getObject())) { return (LuaObjectValue<T>) objectValue; } } throw new LuaError(String.format(INVALID_TYPE_MSG, meta.getTypeName(), value.typename())); }
/** * Returns the {@link LuaObjectMeta} associated with the provided object. * * <p>If the provided object is an instance of {@link LuaImplementable} and has a primary * identifying class, the meta associated with that class is scanned for first. * * @param obj the Object to query with * @return the LuaObjectMeta associated with the provided object, or null if one was not found */ public static LuaObjectMeta getMeta(Object obj) { if (obj == null) { return null; } Class<?> target = null; if (obj instanceof LuaImplementable && ((LuaImplementable<?>) obj).getPrimaryIdentifyingClass() != null) { target = ((LuaImplementable<?>) obj).getPrimaryIdentifyingClass(); } else { target = obj.getClass(); } do { if (Lua.PMETA.containsKey(target)) { return Lua.PMETA.get(target); } if (Lua.META.containsKey(target)) { return Lua.META.get(target); } for (LuaObjectMeta meta : Lua.PMETA.values()) { if (meta.getTargetObjectClass().isInstance(obj)) { return meta; } } for (LuaObjectMeta meta : Lua.META.values()) { if (meta.getTargetObjectClass().isInstance(obj)) { return meta; } } if (target != obj.getClass()) { target = obj.getClass(); } else { target = null; } } while (target != null); return null; }