Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an efficient way to avoid instantiating a class with syntax errors?

Tags:

abap

As you may know, it is pretty easy to have active code of a class containing syntax errors (someone activated the code ignoring syntax warnings or someone changed the signature of a method the class calls, for instance).

This means that also dynamic instantiation of such a class via

CREATE OBJECT my_object TYPE (class_name).

will fail with an apparently uncatchable SYNTAX_ERROR exception. The goal is to write code that does not terminate when this occurs.

Known solutions:

  1. Wrap the CREATE OBJECT statement inside an RFC function module, call the module with destination NONE, then catch the (classic) exception SYSTEM_FAILURE from the RFC call. If the RFC succeeds, actually create the object (you can't pass the created object out of the RFC because RFC function modules can't pass references, and objects cannot be passed other than by reference as far as I know).

    This solution is not only inelegant, but impacts performance rather harshly since an entirely new LUW is spawned by the RFC call. Additionally, you're not actually preventing the SYNTAX_ERROR dump, just letting it dump in a thread you don't care about. It will still, annoyingly, show up in ST22.

  2. Before attempting to instantiate the class, call

    cl_abap_typedescr=>describe_by_name( class_name )
    

    and catch the class-based exception CX_SY_RTTI_SYNTAX_ERROR it throws when the code it attempts to describe has syntax errors.

    This performs much better than the RFC variant, but still seems to add unnecessary overhead - usually, I don't want the type information that describe_by_name returns, I'm solely calling it to get a catchable exception, and when it succeeds, its result is thrown away.

Is there a way to prevent the SYNTAX_ERROR dump without adding such overhead?

like image 872
ACuriousMind Avatar asked Oct 17 '22 20:10

ACuriousMind


1 Answers

Most efficient way we could come up with:

METHODS has_correct_syntax
  IMPORTING
    class_name    TYPE seoclsname
  RETURNING
    VALUE(result) TYPE abap_bool.

METHOD has_correct_syntax.
  DATA(include_name) = cl_oo_classname_service=>get_cs_name( class_name ).
  READ REPORT include_name INTO DATA(source_code).
  SYNTAX-CHECK FOR source_code MESSAGE DATA(message) LINE DATA(line) WORD DATA(word).
  result = xsdbool( sy-subrc = 0 ).
ENDMETHOD.

Still a lot of overhead for loading the program and syntax-checking it. However, at least none additional for compiling descriptors you are not interested in.

We investigated when we produced a dependency manager that wires classes together upon startup and should exclude syntactically wrong candidates.

CS includes don't always exist, so get_cs_name might come back empty. Seems to depend on the NetWeaver version and the editor the developer used.

If you are certain that the syntax errors are caused by the classes’ own code, you might want to consider buffering the results of the syntax checks and only revalidate when the class changed after it was last checked. This does not work if you expect syntax errors to be caused by something outside those classes.

like image 167
Florian Avatar answered Nov 02 '22 06:11

Florian