Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

In SilkTest, how can the exact class of a java.lang.Object returned by a Java method be determined, and how then can that class be used?

In SilkTest, how can the exact class of a java.lang.Object returned by a Java method be determined, and how then can that class be used?

Many Java methods return values of type java.lang.Object. In order to call such methods in SilkTest, you must use invokeMethods() to call a method on the return object. (Eventually, you must call a method that returns a 4Test-compatible value.) However, you need to know the exact class of the java.lang.Object, before you know which methods are available for that object. Otherwise, you can only call java.lang.Object methods, which is a fairly limited list.

  • If your method returns a java.lang.Object value, you can use the following invokeMethods() call to return the name of the class of the return object:

    STRING sClass = invokeMethods ({"", "getClass", "getName"}, {{}, {}, {}})

    The above statement will call 3 Java methods:

    1. () -- This is the method of interest. It returns a value of type java.lang.Object, which is not 4Test-compatible and therefore must be used to call a new method. NOTE: Record Class will only list this method if you enable "Show all methods". The method will appear in the commented list below the declaration of invokeMethods().

    2. getClass() -- This java.lang.Object method returns a value of type java.lang.Class, which is not 4Test-compatible and therefore must be used to call a new method.

    3. getName() -- This java.lang.Class method returns a value of type java.lang.String, which is 4Test-compatible.
  • Once you know the name of the class, you can call methods specific to that class. Preferably those methods will return a 4Test-compatible type. Otherwise, you will need to chain additional methods in the invokeMethods() call.

    Expanding on the previous example:

    [ ] STRING sClass = invokeMethods ({"", "getClass", "getName"}, {{}, {}, {}})
    [ ] ANYTYPE aProp1
    [ ] ANYTYPE aProp2
    [+] switch sClass
        [+] case "ClassA"
            [ ] aProp1 = invokeMethods({"", "getClassAProperty1"}, {{}, {}})
            [ ] aProp2 = invokeMethods({"", "getClassAProperty2"}, {{}, {}})
        [+] case "ClassB"
            [ ] aProp1 = invokeMethods({"", "getClassBProperty1"}, {{}, {}})
            [ ] aProp2=invokeMethods({"", "getClassBProperty2"}, {{}, {}})
        [+] default
            [ ] RaiseError (E_UNSUPPORTED, "java.lang.Object is of an unknown class: {sClass}")
    
    

  • A useful general method is toString(), which is a java.lang.Object method that returns a value of type java.lang.String, which translates to 4Test type STRING. You may be able to use toString() to return a value when you do not really understand what the previous method does. However, toString() may return a blank string, or the value may not make sense.

Old KB# 22080

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:27
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.