Wednesday 3 February 2021

Printing to ABAP Console

Introduction to the ABAP Console

Perhaps you may be unfamiliar with this, but newer ABAP versions (7.52+) have the interface if_oo_adt_classrun, which allows printing to a console. This is an alternative to using WRITE statements or the cl_demo_output class. And this alternative is, IMO, a very welcome thing since pretty much all non-ABAP developers are using console printing one way or another.

Implementing this interface does more than that. It turns a class into an executable state by forcing you to implement a main method – as you would do in Java, for example. And, thanks to this, it completely bypasses the need for starting an SAP GUI everytime you want to run something – which is another great thing. To execute, you press F9 and the class starts running through its main method.

Demo of the ABAP Console

What better way to demonstrate this than saying Hello, world? Here’s how we do this:

1. Create a new class;

2. Implement the if_oo_adt_classrun_interface;

3. Use the out object to print;

4. Execute with F9 to see the output in the console.

And here’s an example code following all of the steps above:

CLASS z_ptac_main DEFINITION PUBLIC FINAL CREATE PUBLIC.

  PUBLIC SECTION.

    INTERFACES:

      if_oo_adt_classrun.

ENDCLASS.

CLASS z_ptac_main IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.

    out->write( 'Hello, world' ).

  ENDMETHOD.

ENDCLASS.

And the result of executing this code with F9:

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning
Hello, world!

Printing to the ABAP console from another method


Now, here comes the reason for writing this article. I was running some experiments, and it was going to be very useful to see some output printed from a non-main method of the class. How do you this? Well, I couldn’t find any instructions – so I created my own workaround:

1. Create an out static attribute pointer in the class which has the main method (i.e., the one implementing the if_oo_adt_classrun interface);

2. Set the static attribute pointer to point to the out object from the main method;

3. Now, you can print from any other method inside this class!

Here’s the example:

CLASS z_ptac_main DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES:
      if_oo_adt_classrun.

    CLASS-DATA:
      out TYPE REF TO if_oo_adt_classrun_out.

    CLASS-METHODS:
      hello_world.
ENDCLASS.

CLASS z_ptac_main IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    me->out = out.

    hello_world(  ).
  ENDMETHOD.

  METHOD hello_world.
    out->write( 'Hello, world - but from another method!' ).
  ENDMETHOD.
ENDCLASS.

And here’s the console output for the example:

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning
Hello, world – but from another method!

Printing to the ABAP console from another class


If you noticed, we have made the out pointer public. This means that we could also use it from another class – like in this example:

CLASS z_ptac_dog DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    METHODS:
      woof.
ENDCLASS.

CLASS z_ptac_dog IMPLEMENTATION.
  METHOD woof.
    z_ptac_main=>out->write( 'Woof-woof!' ).
  ENDMETHOD.
ENDCLASS.

We will still be running the logic through our class with the main method, however:

CLASS z_ptac_main DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES:
      if_oo_adt_classrun.

    CLASS-DATA:
      out TYPE REF TO if_oo_adt_classrun_out.
ENDCLASS.

CLASS z_ptac_main IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    me->out = out.

    DATA(dog) = NEW z_ptac_dog(  ).
    dog->woof(  ).
  ENDMETHOD.
ENDCLASS.

Executing the z_ptac_main class prints the following output:

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning
Hello, world but in dog language

No comments:

Post a Comment