5

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?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
ACuriousMind
  • 233
  • 2
  • 14
  • 2
    "someone activated the code ignoring syntax warnings" If someone would do this in my organisation, I would have a serious conversation with him... – József Szikszai Apr 16 '18 at 18:31
  • 1
    I don't understand why you should write some code to work around a short dump. The short dump is there for a critical error which must be solved by a developer. Detecting a syntax error could only be useful for tools which do mass operations on development objects or things like that. – Sandra Rossi Apr 16 '18 at 21:12
  • @SandraRossi Imagine a transaction which at some point has to instantiate several hundred classes (some information that should be displayed to the user is only available as instance attributes of these classes for reasons we have no influence on for the purposes of this question). It is much preferable to simply be able to omit the information coming from the single class with the syntax error than having the entire transaction dump on any user trying to use it while the class has the error. – ACuriousMind Apr 16 '18 at 21:19
  • @ACuriousMind I never encountered that situation. Why SAP never does that? Your case must be really uncommon. – Sandra Rossi Apr 16 '18 at 21:26
  • 2
    "It is much preferable to simply be able to omit the information[...]" - I'd STRONGLY argue against that. I'm working in healthcare IT - omitting information can literally have DEADLY consequences. If any part of the software is broken, let it crash and burn. – vwegert May 06 '18 at 21:09
  • @vwegert This is not intended for use in a productive system, nor is it intended to hide the fact that the object has errors entirely. The objective is to be able to display that fact that the object has errors along with the information from the non-erroneous objects *within the transaction* instead of forcing the dump on people who possibly aren't even interested in the erroneous class to begin with. In a development system it is very annoying that this transaction becomes non-functional every time a single developer manages to produce an erroneous load. – ACuriousMind May 17 '18 at 20:57
  • (Yes, you could argue this points to a deeper design flaw in the transaction, but I'm powerless to change that part.) – ACuriousMind May 17 '18 at 20:57
  • Have you tried to use command "SYSTEM-CALL DESCRIBE CLASS"? It's internal... but I wonder how much overhead you have in item #2 – fabiopagoti Oct 02 '18 at 15:14
  • We had similar requirements when we built a dependency manager. It ought to list all interfaces it was able to find a working implementation for. In our development system, chances were high that people produced temporarily broken implementations. The use case thus is uncommon for application code, but not as out-of-the-world as some think. It is also common practice for cloud apps to step around unavailable data gracefully; for harmless apps like web shops that is not nearly as deadly as vwegert suspects. :-) – Florian Nov 02 '18 at 11:03

1 Answers1

4

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.

Florian
  • 4,821
  • 2
  • 19
  • 44
  • for me, it always gives an information popup when I try to check the syntax of the class. I am getting 'Program "" did not activate fixed point arithmetic.......' message in a popup. Although, this is info but my class syntax is not checked. – Mubee Aug 24 '21 at 15:03