This commit is contained in:
danrega
2025-02-07 14:13:47 +01:00
parent c730345e05
commit afe9e8523a
7 changed files with 115 additions and 96 deletions

View File

@@ -52,12 +52,16 @@
- [Class-Based and Classic Exceptions](#class-based-and-classic-exceptions)
- [ABAP Unit Tests](#abap-unit-tests)
- [ABAP Doc Comments](#abap-doc-comments)
- [Escaping Characters](#escaping-characters)
- [Escape Character](#escape-character)
- [More Information](#more-information)
- [Executable Examples](#executable-examples)
This ABAP cheat sheet provides an overview on selected syntax options and concepts related to ABAP object orientation.
> **💡 Note**<br>
> - This ABAP cheat sheet provides an overview on selected syntax options and concepts related to ABAP object orientation. It is supported by code snippets and an executable example. They are **not** suitable as role models for object-oriented design. Their primary focus is on the syntax and functionality. For more details, refer to the respective topics in the ABAP Keyword Documentation. Find an overview in the topic [ABAP Objects - Overview](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_objects_oview.htm).
> - The cheat sheet is supported by code snippets and an executable example. They are **not** suitable as role models for object-oriented design. Their primary focus is on the syntax and functionality.
> - For more details, refer to the respective topics in the ABAP Keyword Documentation. Find an overview in the topic [ABAP Objects - Overview](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_objects_oview.htm).
> - The [executable examples](#executable-examples) reflect several points and code snippets covered in the cheat sheet.
@@ -2802,7 +2806,7 @@ Components specified in superclasses
<br>
The inheritance tree of the following example classes looks as follows:
The inheritance tree of the following example classes is as follows:
```
LCL1
@@ -3240,7 +3244,7 @@ Additions impacting inheritance
<td>
- You can use the `FINAL` addition to prevent classes from being inherited. These classes cannot inherit from other classes.
- You can use the `FINAL` addition to prevent classes from being inherited.
- You can also apply the `FINAL` addition to methods to prevent them from being redefined in subclasses.
- Refer to the next section for notes on these topics and other inheritance and instantiation features.
@@ -3278,7 +3282,7 @@ Constructors
<br>
The inheritance tree of the following example classes looks as follows:
The inheritance tree of the following example classes is as follows:
```
LCL1
@@ -3381,7 +3385,7 @@ ENDCLASS.
<br>
The inheritance tree of the following example classes looks as follows:
The inheritance tree of the following example classes is as follows:
```
LCL1
@@ -3585,7 +3589,7 @@ The table below includes selected syntax related to inheritance in class and met
> **💡 Note**<br>
> - Some of the syntax options have already been mentioned previously. This is to summarize.
> - The code examples shows local classes and interfaces declared, for example, in the [CCIMP include](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenccimp_glosry.htm) of a class pool.
> - The code examples show local classes and interfaces declared, for example, in the [CCIMP include](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenccimp_glosry.htm) of a class pool.
> - The snippets provided do not represent all possible syntax combinations. For the complete picture, refer to the ABAP Keyword Documentation. Additional syntax options are available in the context of friendship (`GLOBAL FRIENDS/FRIENDS`), testing (`FOR TESTING`), [RAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenarap_glosry.htm) (`FOR BEHAVIOR OF`; to declare [ABAP behavior pools](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenbehavior_pool_glosry.htm)), and more.
> - The order of the additions can vary.
@@ -3615,7 +3619,7 @@ CLASS zcl_demo DEFINITION
- `... PUBLIC ...`: Specifies that the class is a global class, available globally within the class library. Most of the subsequent snippets use the `PUBLIC` addition as the focus is on global classes.
- `... FINAL ...`: Specifies that the class cannot have any subclasses, effectively prohibiting inheritance. This addition seals off a branch of the inheritance tree. In final classes, all methods are automatically final.
- `... CREATE PUBLIC ...`: Specifies that the class can be instantiated wherever it is visible.
- `... CREATE PUBLIC ...`: Specifies that the class can be instantiated wherever it is visible. Not specifying the addition `CREATE ...` means `CREATE PUBLIC` by default.
<br>
@@ -10348,7 +10352,7 @@ ENDCLASS.
<p align="right"><a href="#top">⬆️ back to top</a></p>
### Escaping Characters
### Escape Character
- You may encounter [`!` characters](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/ABENNAMES_ESCAPING.html) specified before operands, particularly in signatures of [procedures](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenprocedure_glosry.htm).
- They are used to distinguish the operand's name from ABAP words.

View File

@@ -1394,7 +1394,7 @@ MODIFY ENTITY root_ent
- The example demonstrates the long form of an ABAP EML `MODIFY` statement.
- The example statement uses the [`FROM`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapmodify_entity_entities_fields.htm) addition instead of `FIELDS ( ... ) WITH`. See more details in the documentation and below.
-
``` abap
MODIFY ENTITIES OF root_ent "full name of root entity
@@ -1714,7 +1714,7 @@ MODIFY ENTITIES OF root_ent
current status of instances in the transactional buffer which
includes unsaved modifications on instances. If an instance is not
yet available in the transactional buffer, the currently persisted
data set is automatically read into the transactional buffer.
data set is (automatically - in case of a managed scenario) read into the transactional buffer.
- Note that read operations are always implicitly enabled for each
entity listed in a BDEF, i. e. there is no extra definition in the
BDEF in contrast to, for example, create or update.

View File

@@ -217,13 +217,11 @@ ENDMETHOD.
Note:
- In the [restricted ABAP language
version](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrestricted_version_glosry.htm "Glossary Entry")
scope, only reads are allowed. Hence, the addition `OPTIONS READ-ONLY` is mandatory. Furthermore, you must make sure
- In [ABAP for Cloud Development](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenabap_for_sap_cloud_glosry.htm) (i.e. the [restricted ABAP language version](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrestricted_version_glosry.htm "Glossary Entry")
scope), only reads are allowed. Hence, the addition `OPTIONS READ-ONLY` is mandatory. Furthermore, you must make sure
that the database objects that are specified after `USING` are accessible.
- Generally, in the [unrestricted ABAP language
version](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenunrestricted_version_glosry.htm "Glossary Entry")
scope ([ABAP for Cloud Development](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenabap_for_sap_cloud_glosry.htm)), more syntax options are allowed for AMDP method declaration
- Generally, in [Standard ABAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstandard_abap_glosry.htm) (i.e. the [unrestricted ABAP language
version](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenunrestricted_version_glosry.htm "Glossary Entry") scope), more syntax options are allowed for AMDP method declaration
and implementation parts. Check the ABAP Keyword Documentation for
more details as covered further down.
@@ -400,7 +398,7 @@ You can then use the CDS table function as source for a
- Native SQL is passed directly to the database.
- AMDP, which uses Native SQL, also does not support implicit client handling.
- While AMDP is permitted in ABAP Cloud, accessing client-dependent data via Native SQL is not supported.
- When using AMDP in ABAP Cloud, it is crucial to access only the current client. Client-safety must be ensured..
- When using AMDP in ABAP Cloud, it is crucial to access only the current client. Client-safety must be ensured.
- AMDP methods in ABAP Cloud must be client-safe, meaning the SQLScript code should access data only in your client. Use only artifacts that limit access to a single client or those that are client-independent.
- Consequently, all objects in the `USING` list must be client-safe, including CDS table functions implemented as AMDP methods.
- There are additions to cover client-safe aspects, ensuring access only to your client data.

View File

@@ -230,7 +230,7 @@ The statements to end an SAP LUW have already been mentioned above: [`COMMIT WOR
`ROLLBACK WORK`
- Similar to `COMMIT WORK` statements, this statement closes the current SAP LUW and opens a new one.
- Among other things, this statement ...
- causes all changes within a SAP LUW to be undone, that is, all previous registrations for the current SAP LUW are removed.
- causes all changes within an SAP LUW to be undone, that is, all previous registrations for the current SAP LUW are removed.
- triggers a database rollback on all currently open database connections, which also terminates the current database LUW.
> **💡 Note**<br>
@@ -372,6 +372,7 @@ After the import of the repository, proceed as follows:
- Run the program by choosing `F8`.
> **💡 Note**<br>
> - The examples in the *main* branch of the ABAP cheat sheet repository are designed to be imported into the SAP BTP ABAP Environment. For Standard ABAP, you can find examples (such as `zdemo_abap_sap_luw`) in the other branches of the repository.
> - The executable example ...
> - demonstrates the SAP LUW using classic dynpros to provide a self-contained and simple example that highlights the considerations regarding implicit database commits, without putting the spotlight on dynpros. Note that classic dynpros are outdated for application programs. New developments should use web-based UIs, such as SAP Fiori UIs.
> - covers the following topics in simple contexts:

View File

@@ -1330,6 +1330,7 @@ After the import of the repository, proceed as follows:
- Run the program by choosing `F8`.
> **💡 Note**<br>
> - The examples in the *main* branch of the ABAP cheat sheet repository are designed to be imported into the SAP BTP ABAP Environment. For Standard ABAP, you can find examples (such as `ZDEMO_ABAP_SELSCR_LISTS_INTRO`) in the other branches of the repository.
> - The executable examples ...
> - do not claim to include meaningful selection screens and lists.
> - are not intended to be role models for proper selection screen and list design.

View File

@@ -55,8 +55,8 @@ The main data types for date, time, and time stamps in ABAP are as follows:
**Built-in ABAP types**
| Type | Length | Meaning | Value Range | Initial value | Notes |
|---|---|---|---|---|---|
| `d` | 8 characters | For storing a calendar date (i.e. an exact day, week, or month of the Gregorian calendar) in a date field. A valid value has the format `yyyymmdd`. | Any eight Unicode characters that can be encoded in UCS-2 are valid values. These values must be digits that conform to the calendar rules in the format `yyyymmdd`. yyyy (year): 0001 to 9999; mm (month): 01 to 12; dd (day): 01 to 31 | 00000000 | It is a character-like type and mostly used for input and output of dates. Regarding assignments of data objects with numeric data types and calculations: The content of the time field is converted to the number of seconds since 00:00:00. |
| `t` | 6 characters | For storing a time in a time field. A valid value has the format `hhmmss`. | Any six Unicode characters that can be encoded in UCS-2 are valid values. These values must represent times in accordance with the 24-hour clock format, specifically `hhmmss`. hh (hours): 00 to 23; mm (minutes): 00 to 59; ss (seconds): 00 to 59. | 000000 | It is a character-like type and mostly used for input and output of times. Regarding assignments of data objects with numeric data types and calculations: Valid values are converted to the number of days since 01.01.0001. |
| `d` | 8 characters | For storing a calendar date (i.e. an exact day, week, or month of the Gregorian calendar) in a date field. A valid value has the format `yyyymmdd`. | Any eight Unicode characters that can be encoded in UCS-2 are valid values. These values must be digits that conform to the calendar rules in the format `yyyymmdd`. yyyy (year): 0001 to 9999; mm (month): 01 to 12; dd (day): 01 to 31 | 00000000 | It is a character-like type and mostly used for input and output of dates. Regarding assignments of data objects with numeric data types and calculations: Valid values are converted to the number of days since 01.01.0001. |
| `t` | 6 characters | For storing a time in a time field. A valid value has the format `hhmmss`. | Any six Unicode characters that can be encoded in UCS-2 are valid values. These values must represent times in accordance with the 24-hour clock format, specifically `hhmmss`. hh (hours): 00 to 23; mm (minutes): 00 to 59; ss (seconds): 00 to 59. | 000000 | It is a character-like type and mostly used for input and output of times. Regarding assignments of data objects with numeric data types and calculations: The content of the time field is converted to the number of seconds since 00:00:00. |
| `utclong` | 8 byte | For storing a time stamp (i.e. a combined date/time specification). A time stamp field represents a unique time in UTC reference time (UTC: Coordinated Universal Time, which is the basis for representing worldwide time data). | Internal 8-byte integer representation of a UTC time stamp exact to 100 nanoseconds, in ISO-8601 notation between 0001-01-01T00:00:00.0000000 and 9999-12-31T23:59:59.9999999 | 0 | Find more details, e.g. on the special initial value, [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenutclong.htm). |

View File

@@ -761,87 +761,102 @@ ENDDO.
Example:
```abap
"----------------------------------------------------------------------------
"------------ RESUMABLE addition in method declarations and ----------------
"------------ RESUMABLE addition with RAISE EXCEPTION/THROW -----------------
"----------------------------------------------------------------------------
"The following demo method declaration shows one prerequisite of resumable
"exceptions. The RAISING clause specifies a demo exception class with the
"RESUMABLE addition.
CLASS zcl_demo_abap DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
CLASS-METHODS meth_resumable
IMPORTING num1 TYPE i
num2 TYPE i
RETURNING VALUE(div_result) TYPE string
RAISING RESUMABLE(zcx_demo_abap_error_b).
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
"The following demo method declaration shows one prerequisite of resumable
"exceptions. The RAISING clause specifies a demo exception class with the
"RESUMABLE addition.
CLASS-METHODS meth_resumable
IMPORTING num1 TYPE i
num2 TYPE i
RETURNING VALUE(div_result) TYPE string
RAISING RESUMABLE(zcx_demo_abap_error_b).
ENDCLASS.
...
"Demo method implementation demonstrating a RAISE EXCEPTION statement
"with the RESUMABLE addition (and an example for statement using the COND
"operator commented out)
METHOD meth_resumable.
CLASS zcl_demo_abap IMPLEMENTATION.
IF num2 = 0 AND num1 <> 0.
RAISE RESUMABLE EXCEPTION TYPE zcx_demo_abap_error_b.
div_result = `No result. Resumed!`.
ELSE.
div_result = num1 / num2.
ENDIF.
METHOD if_oo_adt_classrun~main.
"div_result = COND #( WHEN num2 = 0 AND num1 <> 0 THEN num1 / num2
" ELSE THROW RESUMABLE zcx_demo_abap_error_b( ) ).
"----------------------------------------------------------------------------
"----------------------- CATCH BEFORE UNWIND -------------------------------
"----------------------------------------------------------------------------
"Example using the method
"The following example creates a table containing integers from -5 to 5. This
"table is looped across to have different values for divisions. Here, this
"value represents the second operand. The first operand is the sy-tabix
"value. The result is added to an internal table. When the second operand's
"value is 0, the exception is raised. In the CATCH BEFORE UNWIND block, the
"is_resumable attribute is evaluated. An info message is added to the internal
"table, and the processing continues after executing the RESUME statement.
ENDMETHOD.
DATA restab TYPE string_table.
TYPES ty_inttab TYPE TABLE OF i WITH EMPTY KEY.
DATA(inttab) = REDUCE ty_inttab( INIT tab = VALUE ty_inttab( )
FOR i = -5 UNTIL i > 5
NEXT tab = VALUE #( BASE tab ( i ) ) ).
"----------------------------------------------------------------------------
"----------------------- CATCH BEFORE UNWIND -------------------------------
"----------------------------------------------------------------------------
"Example using the method
"The following example creates a table containing integers from -5 to 5. This
"table is looped across to have different values for divisions. Here, this
"value represents the second operand. The first operand is the sy-tabix
"value. The result is added to an internal table. When the second operand's
"value is 0, the exception is raised. In the CATCH BEFORE UNWIND block, the
"is_resumable attribute is evaluated. An info message is added to the internal
"table, and the processing continues after executing the RESUME statement.
DATA restab TYPE string_table.
TYPES ty_inttab TYPE TABLE OF i WITH EMPTY KEY.
DATA(inttab) = REDUCE ty_inttab( INIT tab = VALUE ty_inttab( )
FOR i = -5 UNTIL i > 5
NEXT tab = VALUE #( BASE tab ( i ) ) ).
LOOP AT inttab INTO DATA(wa).
TRY.
DATA(divres) = meth_resumable(
num1 = sy-tabix
num2 = wa
).
APPEND |{ sy-tabix } / { wa } = { divres }| TO restab.
CATCH BEFORE UNWIND zcx_demo_abap_error_b INTO DATA(error_resume).
DATA(is_resumable) = error_resume->is_resumable.
IF is_resumable IS NOT INITIAL.
APPEND |Exception raised. Is resumable? -> "{ is_resumable }"| TO restab.
RESUME.
ENDIF.
ENDTRY.
ENDLOOP.
LOOP AT inttab INTO DATA(wa).
TRY.
DATA(divres) = meth_resumable(
num1 = sy-tabix
num2 = wa
).
APPEND |{ sy-tabix } / { wa } = { divres }| TO restab.
CATCH BEFORE UNWIND zcx_demo_abap_error_b INTO DATA(error_resume).
DATA(is_resumable) = error_resume->is_resumable.
IF is_resumable IS NOT INITIAL.
APPEND |Exception raised. Is resumable? -> "{ is_resumable }"| TO restab.
RESUME.
ENDIF.
ENDTRY.
ENDLOOP.
out->write( data = restab name = `restab` ).
*Content of the result table:
*1 / -5 = 0.2-
*2 / -4 = 0.5-
*3 / -3 = 1-
*4 / -2 = 2-
*5 / -1 = 5-
*1 / -5 = 0.2-
*2 / -4 = 0.5-
*3 / -3 = 1-
*4 / -2 = 2-
*5 / -1 = 5-
*Exception raised. Is resumable? -> "X"
*6 / 0 = No result. Resumed!
*7 / 1 = 7
*8 / 2 = 4
*9 / 3 = 3
*10 / 4 = 2.5
*6 / 0 = No result. Resumed!
*7 / 1 = 7
*8 / 2 = 4
*9 / 3 = 3
*10 / 4 = 2.5
*11 / 5 = 2.2
ENDMETHOD.
METHOD meth_resumable.
"Demo method implementation demonstrating a RAISE EXCEPTION statement
"with the RESUMABLE addition (and an example for statement using the COND
"operator commented out)
IF num2 = 0 AND num1 <> 0.
RAISE RESUMABLE EXCEPTION TYPE zcx_demo_abap_error_b.
div_result = `No result. Resumed!`.
ELSE.
div_result = num1 / num2.
ENDIF.
"Statement with COND operator using RESUMABLE
"div_result = COND #( WHEN num2 = 0 AND num1 <> 0 THEN num1 / num2
" ELSE THROW RESUMABLE zcx_demo_abap_error( ) ).
ENDMETHOD.
ENDCLASS.
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
@@ -856,7 +871,7 @@ ENDLOOP.
- If an exception is raised and ...
- not handled, the exception text is displayed in the [short dump](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenshort_dump_glosry.htm) of the runtime error.
- handled, the text can be retrieved using the `get_text` method as shown above.
- As outlined in the next section, exception classes must implement one of the system interfaces for messages to use exception texts.
- As outlined in the next section, exception classes must implement one of the system interfaces (or inherit from classes implementing them) for messages to use exception texts.
<p align="right"><a href="#top">⬆️ back to top</a></p>
@@ -1255,14 +1270,14 @@ ASSERT flag = abap_true.
### Local Exception Classes
- You can create local exception classes within your class pool for exceptions specific to your program that are not needed globally.
- The following simplified example experiments with a local exception class. It uses a message class that is part of the ABAP cheat sheet repository.
- The following simplified example explores a local exception class. It uses a message class that is part of the ABAP cheat sheet repository.
- In the example, the global class uses the exception class in a private method's signature. Therefore, the exception class declaration is placed in the *Class-Relevant Local Types* tab ([CCDEF include](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenccdef_glosry.htm)). Its implementation is in the *Local Types* tab ([CCIMP include](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenccimp_glosry.htm)) in ADT.
- You can add the following code to the three class includes of a demo class (the global class's name is `zcl_demo_abap` in the example), activate, and run the class using F9. The implementation of the private method, that includes the local exception class in the signature, contains several `RAISE EXCEPTION` statements demonstrating various syntax options of these statements. The example is designed to write exception texts to the console.
<table>
<tr>
<td> Class include </td> <td> Notes </td>
<td> Class include </td> <td> Code </td>
</tr>
<tr>
@@ -1359,7 +1374,7 @@ CLASS lcx_error DEFINITION INHERITING FROM cx_static_check.
attr4 TYPE scx_attrname VALUE '',
END OF some_error.
DATA text TYPE string.
DATA text TYPE string READ-ONLY.
METHODS constructor
IMPORTING
@@ -1523,12 +1538,12 @@ ABAP contract checks include ...
### Classic Exceptions
- In older ABAP code, you may encounter non-class-based exceptions, the predecessors of class-based exceptions.
- They should no longer be used.
- They should not be used in new developments.
- They are specified in method signatures with the `EXCEPTIONS` addition.
- Find more details in the [ABAP Keyword Documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenexceptions_non_class.htm).
Example:
- The following example shows the longer available `describe_by_name` method available in the [Runtime Type Identification (RTTI)](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrun_time_type_identific_glosry.htm) class `cl_abap_typedescr` (find more information in the Dynamic Programming cheat sheet).
- The following example shows the longer available `describe_by_name` method available in the [Runtime Type Identification (RTTI)](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrun_time_type_identific_glosry.htm) class `cl_abap_typedescr` (find more information in the [Dynamic Programming](06_Dynamic_Programming.md) cheat sheet).
- This method specifies `EXCEPTIONS` in the method signature.
- The example intentionally uses a class name that is (most probably) not available in the system.
- The exception is raised, and the `sy-subrc` value is evaluated.