Getting object inheritance

Last edited on

Sometimes it is useful to find out which classes a specific object extends. Here is an example for doing this for Qt and Java applications:

Python:

def main():
    # ...

    widget = waitForObject(names.some_object_name)
    test.log(get_inheritance(widget))

def get_inheritance(widget, join_str=" -> "):
    if isNull(widget):
        test.log("get_inheritance(): Passed object reference is null according to isNull()")
        return "n/a (passed object reference is null according to isNull())"

    tree = []

    if className(widget) == "QVariant":
        tree.append("QVariant (castToQObject())")
        widget = castToQObject(widget)

    if className(widget) == "QString":
        # No metaObject on QString's in Squish
        return "QString"
    elif hasattr(widget, "metaObject"): # Qt
        meta = widget.metaObject()
        while True:
            if isNull(meta):
                if not tree:
                    return "null"
                return join_str.join(tree)
            tree.append(str(meta.className()))
            meta = meta.superClass()
    elif hasattr(widget, "getClass"): # Java
        klass = widget.getClass()
        while True:
            if isNull(klass):
                if not tree:
                    return "null"
                return join_str.join(tree)
            tree.append(str(klass.getName()))
            klass = klass.getSuperclass()
    elif hasattr(widget, "nativeObject") and not isNull(widget.nativeObject): # Windows Forms or WPF
        typeObj = widget.nativeObject.GetType()
        while True:
            if isNull(typeObj):
                if not tree:
                    return "null"
                return join_str.join(tree)
            tree.append(str(typeObj.FullName))
            typeObj = typeObj.BaseType
    return "[no metaObject or getClass() or nativeObject to query]"

JavaScript:

function main()
{
    // ...

    var widget = waitForObject(names.some_object_name);
    test.log(get_inheritance(widget));
}

function get_inheritance(widget, joinStr)
{
    if (isNull(widget)) {
        test.log("get_inheritance(): Passed object reference is null according to isNull()");
        return "n/a (passed object reference is null according to isNull())";
    }

    if (joinStr == null) {
        joinStr = " -> ";
    }

    var tree = [];

    if (typeName(widget) == "QVariant") {
        tree.push("QVariant (castToQObject())");
        widget = castToQObject(widget);
    }

    if (typeName(widget) == "QString") { // Qt, special
        // No metaObject on QString's in Squish
        return "QString";
    } else if ("metaObject" in widget) { // Qt
        var meta = widget.metaObject();
        while (true) {
            if (isNull(meta)) {
                if (tree.length == 0) {
                    return "null";
                }
                return tree.join(" -> ");
            }
            tree.push(meta.className());
            meta = meta.superClass();
        }
    } else if ("getClass" in widget) { // Java
        var klass = widget.getClass();
        while (true) {
            if (isNull(klass)) {
                if (tree.length == 0) {
                    return "null";
                }
                return tree.join(" -> ");
            }
            tree.push(klass.getName());
            klass = klass.getSuperclass();
        }
    } else if (("nativeObject" in widget) && !isNull(widget.nativeObject)) { // Windows Forms or WPF
        var typeObj = widget.nativeObject.GetType();
        while (true) {
            if (isNull(typeObj)) {
                if (tree.length == 0) {
                    return "null";
                }
                return tree.join(" -> ")
            }
            tree.push(typeObj.FullName);
            typeObj = typeObj.BaseType;
        }
    }
    return "[no metaObject or getClass() or nativeObject to query]";
}