Show TOC

Syntax documentationCreating an Object Locate this document in the navigation structure

In an ST program, you can create an instance of a global ABAP objects class as follows:

Syntax Syntax

  1. <tt:create-object var=″oref″ [class=″class″]>
  2.   [<tt:with-parameter name="para1" [ref=″node1″|val=″val1″|var=″var1″] />
  3.    <tt:with-parameter name="para2" [ref=″node2”|val=″val2”|var=″var2″] />
  4.    ...]
  5. </tt:call-method>
End of the code.

You can use var to declare a variable or a parameter of the ST program. The variable or parameter must have been created as a class reference variable or an interface reference variable with the ref-type addition.

The object is created in the internal session of the ABAP program that called the ST program. If the class attribute is not declared, var must be a class reference variable and an instance of the relevant class is created. If the class attribute is declared, an instance of the specified class is created. You can specify a global class from the ABAP class library for class, either more special or the same as the static type of the reference variable var:

  • If var is a class reference variable, class must be the class or subclass of the class of var.

  • If var is an interface reference variable, class must implement the interface of var.

The interface parameters para1, para2, ... of the instance constructor of the class of the object can or must be bound to actual parameters, using the ST command tt:with-parameter. The same rules apply here as to regular method calls. Constructor exceptions are also handled in the same way as exceptions for regular method calls.

After the object has been created, the reference variable var points to the object and can be used to call instance methods.

Example

The following Simple Transformation uses the variable ovar to create an object of the class cls. When doing this, it passes the data root PARA to the parameter repl of its instance constructor. The method convert is then called, whose input/output parameter is assigned the variable result. Note that a data root cannot be assigned to an input/output parameter - for neither serializations nor deserializations.

Syntax Syntax

  1. <tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
  2.   <tt:root name="ROOT"/>
  3.   <tt:root name="PARA"/>
  4.   <tt:variable name="ovar" ref-type="cls"/>
  5.   <tt:variable name="result"/>
  6.   <tt:template>
  7.     <tt:create-object var="ovar">
  8.       <tt:with-parameter name="repl" ref="PARA"/>
  9.     </tt:create-object>
  10.     <tt:assign ref="ROOT" to-var="result"/>
  11.     <tt:call-method s-name="convert" var="ovar">
  12.       <tt:with-parameter name="text" var="result"/>
  13.     </tt:call-method>
  14.     <Result>
  15.       <tt:write var="result"/>
  16.     </Result>
  17.   </tt:template>
  18. </tt:transform>
End of the code.

The following is an example of a calling ABAP program:

Syntax Syntax

  1. DATA xml_string TYPE string.
  2. DATA text       TYPE string VALUE '1 2 3'.
  3. DATA repl       TYPE string value '*'.
  4. DATA exc        TYPE REF TO cx_st_call_method_error.
  5. TRY.
  6.     CALL TRANSFORMATION ...
  7.       SOURCE root = text
  8.              para = repl
  9.       RESULT XML xml_string.
  10.   CATCH cx_st_call_method_error INTO exc.
  11.     ...
  12. ENDTRY.
  13. cl_abap_browser=>show_xml( xml_string = xml_string
  14.                            modal = 'X' ).
End of the code.

The constructor and the method convert of the class cls are defined as follows:

Syntax Syntax

  1. METHOD constructor.
  2.   me->repl = repl.
  3. ENDMETHOD.
  4. METHOD convert.
  5.   REPLACE ALL OCCURRENCES OF ` ` IN text WITH repl.
  6. ENDMETHOD.
End of the code.

The result of the transformation is as follows:

<Result>1*2*3</Result>