-
Notifications
You must be signed in to change notification settings - Fork 286
/
zcl_demo_abap_objects_misc.clas.abap
1067 lines (883 loc) · 42 KB
/
zcl_demo_abap_objects_misc.clas.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"! <p class="shorttext"><strong>ABAP object orientation (2)</strong><br/>ABAP cheat sheet example class</p>
"!
"! <p>The example class demonstrates syntax and concepts related to ABAP object orientation. It adds
"! to class {@link zcl_demo_abap_objects}, and includes additional syntax examples.<br/>
"! Choose F9 in ADT to run the class.</p>
"!
"! <h2>Note</h2>
"! <p>Find information on <strong>getting started with the example class</strong> and the
"! <strong>disclaimer</strong> in the ABAP Doc comment of class {@link zcl_demo_abap_aux}.</p>
CLASS zcl_demo_abap_objects_misc DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
INTERFACES zdemo_abap_objects_interface.
METHODS constructor IMPORTING text TYPE string OPTIONAL.
CLASS-METHODS class_constructor.
PROTECTED SECTION.
PRIVATE SECTION.
"------------ Demonstrating constructors ------------
DATA instance_timestamp TYPE utclong.
CLASS-DATA static_timestamp TYPE utclong.
DATA instance_name TYPE string.
CLASS-DATA stat_constr_call_count TYPE i.
CLASS-DATA instance_constr_call_count TYPE i.
"----------------------------------------------------
"------------ Demonstrating optional parameters ------------
METHODS meth_opt_1 IMPORTING num TYPE i OPTIONAL
RETURNING VALUE(str) TYPE string.
METHODS meth_opt_2 IMPORTING num TYPE i DEFAULT 1
RETURNING VALUE(str) TYPE string.
METHODS meth_opt_3 IMPORTING num1 TYPE i
num2 TYPE i OPTIONAL
num3 TYPE i DEFAULT 1
RETURNING VALUE(str) TYPE string.
"-----------------------------------------------------------
"------------ Demonstrating preferred parameters ------------
METHODS meth_pref
IMPORTING num1 TYPE i OPTIONAL
num2 TYPE i OPTIONAL
num3 TYPE i DEFAULT 1
PREFERRED PARAMETER num1
RETURNING VALUE(text) TYPE string.
"-----------------------------------------------------------
"--- Demonstrating the excursion inline declarations, returning parameters ---
CLASS-METHODS meth1 IMPORTING i_str TYPE string
i_tab TYPE string_table OPTIONAL
EXPORTING e_dec TYPE decfloat34
e_tab TYPE string_table
RETURNING VALUE(r_int) TYPE i.
CLASS-METHODS meth2 RETURNING VALUE(r_tab) TYPE string_table.
"-----------------------------------------------------------------------------
"------------ Demonstrating self-reference me ------------
DATA str TYPE string.
METHODS meth RETURNING VALUE(text) TYPE string.
"-----------------------------------------------------------
"------------ Demonstrating method chaining ------------
METHODS add_text IMPORTING str TYPE string
RETURNING VALUE(ref) TYPE REF TO zcl_demo_abap_objects_misc.
METHODS add_space RETURNING VALUE(ref) TYPE REF TO zcl_demo_abap_objects_misc.
METHODS add_period RETURNING VALUE(ref) TYPE REF TO zcl_demo_abap_objects_misc.
METHODS return_text RETURNING VALUE(str) TYPE string.
METHODS display_text IMPORTING cl_run_ref TYPE REF TO if_oo_adt_classrun_out.
DATA text TYPE string.
"-------------------------------------------------------
"------------ Demonstrating interface ------------
ALIASES res FOR zdemo_abap_objects_interface~add_result.
ALIASES add FOR zdemo_abap_objects_interface~addition.
ALIASES subtr FOR zdemo_abap_objects_interface~subtraction.
"-------------------------------------------------
"------------ Demonstrating friendship ------------
TYPES str4friend TYPE string.
CLASS-METHODS get_hello RETURNING VALUE(hello) TYPE str4friend.
"--------------------------------------------------
"------------ Demonstrating formal parameters ------------
"Local types and data objects used in the example
TYPES c3 TYPE c LENGTH 3.
TYPES der_type TYPE TABLE FOR CREATE zdemo_abap_rap_ro_m.
DATA int TYPE i.
DATA itab TYPE TABLE OF zdemo_abap_fli_ve WITH EMPTY KEY.
"Various syntax options for completely typing formal parameters
"Note: The example parameters are all specified for passing
"actual parameters by reference.
METHODS formal_params_compl_types IMPORTING
"---- Non-generic built-in ABAP types ----
i_a TYPE i
i_b TYPE string
"---- ABAP DDIC types ----
i_c TYPE land1 "elementary type
i_d TYPE timestampl "elementary type
i_e TYPE zdemo_abap_fli "structured type based on DDIC database table
i_f TYPE string_hashed_table "table type
"---- ABAP CDS types (all of the examples are structured types) ----
i_g TYPE zdemo_abap_fli_ve "CDS view entity
i_h TYPE zdemo_abap_abstract_ent "CDS abstract entity
i_i TYPE zdemo_abap_table_function "CDS table function
"---- Data types declared in public section of a class ----
i_j TYPE zcl_demo_abap_dtype_dobj=>t_pub_text_c30 "elementary type
i_k TYPE zcl_demo_abap_amdp=>carr_fli_struc "structured type
i_l TYPE zcl_demo_abap_amdp=>carr_fli_tab "table type
"---- Data types declared in an interface ----
i_m TYPE zdemo_abap_get_data_itf=>occ_rate "elementary type
i_n TYPE zdemo_abap_get_data_itf=>carr_tab "table type
"---- Local types ----
i_o TYPE c3 "elementary type
i_p TYPE der_type "table type (BDEF derived type)
"---- Note: Examples such as the following are not allowed type specifications of formal parameters. ----
"---- In the following cases, extra (local) type declarations with TYPES are required before the --------
"---- method declaration to type the formal parameters. -------------------------------------------------
"i_no1 TYPE c LENGTH 3
"i_no2 TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY
"---- Reference types ----
i_q TYPE REF TO i "Data reference
i_r TYPE REF TO zdemo_abap_carr "Data reference
i_s TYPE REF TO zcl_demo_abap_unit_test "Object reference
i_t TYPE REF TO data "Data reference (considered as complete typing, too)
i_u TYPE REF TO object "Object reference (considered as complete typing, too)
"---- TYPE LINE OF addition (structured type based on a table type) ----
i_v TYPE LINE OF zcl_demo_abap_amdp=>carr_fli_tab
i_w TYPE LINE OF der_type
"---- LIKE addition (types based on existing data objects) ----
i_x LIKE int "Local data object
i_y LIKE zcl_demo_abap_dtype_dobj=>comma "Constant specified in a class
i_z LIKE zdemo_abap_objects_interface=>stat_str "Data object specified in an interface
"---- LIKE LINE OF addition (types based on existing internal tables) ----
i_1 LIKE LINE OF itab "Local internal table
"---- LIKE REF TO addition (reference types based on existing data object) ----
i_2 LIKE REF TO int "Local elementary data object
i_3 LIKE REF TO itab "Local internal table
.
METHODS formal_params_generic_types IMPORTING
"---- Any data type ----
i_data TYPE data
i_any TYPE any
"---- Character-like types ----
i_c TYPE c "Text field with a generic length
i_clike TYPE clike "Character-like (c, n, string, d, t, and character-like flat structures)
i_csequence TYPE csequence "Text-like (c, string)
i_n TYPE n "Numeric text with generic length
i_x TYPE x "Byte field with generic length
i_xsequence TYPE xsequence "Byte-like (x, xstring)
"---- Numeric types ----
i_decfloat TYPE decfloat "decfloat16 decfloat34
i_numeric TYPE numeric "Numeric (i, int8, p, decfloat16, decfloat34, f, (b, s))
i_p TYPE p "Packed number (generic length and number of decimal places)
"---- Internal table types ----
i_any_table TYPE ANY TABLE "Internal table with any table type
i_hashed_table TYPE HASHED TABLE
i_index_table TYPE INDEX TABLE
i_sorted_table TYPE SORTED TABLE
i_standard_table TYPE STANDARD TABLE
i_table TYPE table "Standard table
"---- Other types ----
i_simple TYPE simple "Elementary data type including enumerated types and
"structured types with exclusively character-like flat components
.
"-----------------------------------------------------------
ENDCLASS.
CLASS zcl_demo_abap_objects_misc IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( |ABAP cheat sheet example: ABAP Object Orientation (2)\n\n| ).
out->write( |1) Complete Typing of Formal Parameters\n\n| ).
out->write( `No output for this section. See the signature of the formal_params_compl_types method, `
&& `which demonstrates multiple syntax variants for typing formal parameters completely.` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `2) Generic Typing of Formal Parameters` ) ).
"Structure including various components of specific types
"They represent actual parameters in the method call below
DATA: BEGIN OF s,
c3 TYPE c LENGTH 3,
c10 TYPE c LENGTH 10,
n4 TYPE n LENGTH 4,
str TYPE string,
time TYPE t,
date TYPE d,
dec16 TYPE decfloat16,
dec34 TYPE decfloat34,
int TYPE i,
pl4d2 TYPE p LENGTH 4 DECIMALS 2,
tab_std TYPE STANDARD TABLE OF string WITH EMPTY KEY,
tab_so TYPE SORTED TABLE OF string WITH NON-UNIQUE KEY table_line,
tab_ha TYPE HASHED TABLE OF string WITH UNIQUE KEY table_line,
xl1 TYPE x LENGTH 1,
xstr TYPE xstring,
structure TYPE zdemo_abap_carr, "character-like flat structure
END OF s.
"The following method call specifies various actual parameters for the
"generic formal parameters.
"Note the comments for allowed and not allowed example assignments of
"actual parameters.
formal_params_generic_types(
"------------- Any data type -------------
"--- data/any: Allowed (examples) ---
i_data = s-c3
"i_data = s-time
"i_data = s-tab_std
"i_data = s-xstr
i_any = s-c3
"i_any = s-time
"i_any = s-tab_std
"i_any = s-xstr
"------------- Character-like types -------------
"--- c: Allowed (examples) ---
i_c = s-c3
"i_c = s-c10
"--- c: Not allowed (examples) ---
"i_c = s-str
"i_c = s-n4
"--- clike: Allowed (examples) ---
i_clike = s-c3
"i_clike = s-c10
"i_clike = s-str
"i_clike = s-structure
"i_clike = s-time
"i_clike = s-date
"i_clike = s-n4
"--- clike: Not allowed (examples) ---
"i_clike = s-xstr
"i_clike = s-xl1
"i_clike = s-pl4d2
"--- csequence: Allowed (examples) ---
i_csequence = s-c3
"i_csequence = s-c10
"i_csequence = s-str
"--- csequence: Not allowed (examples) ---
"i_csequence = s-time
"i_csequence = s-date
"i_csequence = s-structure
"--- n: Allowed ---
i_n = s-n4
"--- n: Not allowed (examples) ---
"i_n = s-c3
"i_n = s-int
"--- x: Allowed ---
i_x = s-xl1
"--- x: Not allowed (examples) ---
"i_x = s-xstr
"i_x = s-c3
"--- xsequence: Allowed ---
i_xsequence = s-xstr
"i_xsequence = s-xl1
"--- xsequence: Not allowed (examples) ---
"i_xsequence = s-c3
"i_xsequence = s-str
"--- decfloat: Allowed ---
i_decfloat = s-dec16
"i_decfloat = s-dec34
"--- decfloat: Not allowed (examples) ---
"i_decfloat = s-int
"i_decfloat = s-pl4d2
"--- numeric: Allowed (examples) ---
i_numeric = s-int
"i_numeric = s-dec16
"i_numeric = s-dec34
"i_numeric = s-pl4d2
"--- numeric: Not allowed (examples) ---
"i_numeric = s-n4
"i_numeric = s-date
"--- p: Allowed ---
i_p = s-pl4d2
"--- p: Not allowed (examples) ---
"i_p = s-dec16
"i_p = s-dec34
"--- any table: Allowed ---
i_any_table = s-tab_std
"i_any_table = s-tab_ha
"i_any_table = s-tab_so
"--- any table: Not allowed (examples) ---
"i_any_table = s-structure
"i_any_table = s-c3
"--- hashed table: Allowed ---
i_hashed_table = s-tab_ha
"--- hashed table: Not allowed ---
"i_hashed_table = s-tab_std
"i_hashed_table = s-tab_so
"--- index table: Allowed ---
i_index_table = s-tab_std
"i_index_table = s-tab_so
"--- index table: Not allowed ---
"i_index_table = s-tab_ha
"--- sorted table: Allowed ---
i_sorted_table = s-tab_so
"--- sorted table: Not allowed ---
"i_sorted_table = s-tab_std
"i_sorted_table = s-tab_ha
"--- standard table/table: Allowed ---
i_standard_table = s-tab_std
i_table = s-tab_std
"--- standard table/table: Not allowed ---
"i_standard_table = s-tab_so
"i_standard_table = s-tab_ha
"i_table = s-tab_so
"i_table = s-tab_ha
"--- simple: Allowed (examples) ---
i_simple = s-structure
"i_simple = s-c3
"i_simple = s-n4
"i_simple = s-int
"i_simple = s-pl4d2
"i_simple = s-xstr
"i_simple = s-str
"--- simple: Not allowed (examples) ---
"i_simple = s-tab_ha
"i_simple = s-tab_so
).
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3) Defining Parameters as Optional` ) ).
DATA(meth_opt_1_result_a) = meth_opt_1( ).
DATA(meth_opt_1_result_b) = meth_opt_1( 2 ).
DATA(meth_opt_2_result_a) = meth_opt_2( ).
DATA(meth_opt_2_result_b) = meth_opt_2( 3 ).
"The commented out statement is not possible as there is one
"non-optional parameter.
"DATA(meth_opt_3_result_a) = meth_opt_3( ).
DATA(meth_opt_3_result_b) = meth_opt_3( 4 ).
DATA(meth_opt_3_result_c) = meth_opt_3( num1 = 5 num2 = 6 ).
DATA(meth_opt_3_result_d) = meth_opt_3( num1 = 7 num3 = 8 ).
out->write( data = meth_opt_1_result_a name = `meth_opt_1_result_a` ).
out->write( |\n| ).
out->write( data = meth_opt_1_result_b name = `meth_opt_1_result_b` ).
out->write( |\n| ).
out->write( data = meth_opt_2_result_a name = `meth_opt_2_result_a` ).
out->write( |\n| ).
out->write( data = meth_opt_2_result_b name = `meth_opt_2_result_b` ).
out->write( |\n| ).
out->write( data = meth_opt_3_result_b name = `meth_opt_3_result_b` ).
out->write( |\n| ).
out->write( data = meth_opt_3_result_c name = `meth_opt_3_result_c` ).
out->write( |\n| ).
out->write( data = meth_opt_3_result_d name = `meth_opt_3_result_d` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `4) Defining Input Parameters as Preferred` ) ).
DATA(text1) = meth_pref( num1 = 3 num2 = 3 num3 = 3 ).
out->write( text1 ).
DATA(text2) = meth_pref( num1 = 3 num2 = 3 ).
out->write( text2 ).
DATA(text3) = meth_pref( num2 = 3 num3 = 3 ).
out->write( text3 ).
DATA(text4) = meth_pref( num1 = 3 num3 = 3 ).
out->write( text4 ).
DATA(text5) = meth_pref( num2 = 3 ).
out->write( text5 ).
DATA(text6) = meth_pref( num3 = 3 ).
out->write( text6 ).
DATA(text7) = meth_pref( ).
out->write( text7 ).
"Not specifying the name of the formal parameter. The
"actual parameter is assigned to the preferred input
"parameter.
DATA(text8) = meth_pref( 3 ).
out->write( text8 ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `5) Constructors` ) ).
"Notes:
"- The static constructor is called only once, even when multiple class instances are created,
" leading to a constant static_timestamp value and stat_constr_call_count value, which remains 1.
"- The instance_timestamp attribute shows different timestamps for each created instance.
"- The static attribute instance_constr_call_count increases with each instance. Note that running
" the class with F9 in ADT also calls the instance and static constructors. Thus, the final
" instance_constr_call_count totals the number of DO loop passes plus 1, starting with 2 for inst1
" instead of 1.
DATA itab_constr TYPE string_table.
DO 5 TIMES.
DATA(inst) = NEW zcl_demo_abap_objects_misc( |inst{ sy-index }| ).
APPEND |-------------- Instance "{ inst->instance_name }" --------------| TO itab_constr.
APPEND |instance_timestamp: { inst->instance_timestamp }| TO itab_constr.
APPEND |static_timestamp: { inst->static_timestamp }| TO itab_constr.
APPEND |instance_constr_call_count: { inst->instance_constr_call_count }| TO itab_constr.
APPEND |stat_constr_call_count: { inst->stat_constr_call_count }| TO itab_constr.
APPEND INITIAL LINE TO itab_constr.
ENDDO.
out->write( data = itab_constr name = `itab_constr` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `6) Excursion: Inline Declarations, Returning Parameters` ) ).
"Note:
"- Calling the method in the same class means specifying 'zcl_some_class=>' is optional here.
"- There is no proper implementation in the method implementations.
"- There is not output for this section as it is meant to visualize on syntax options.
"Standalone method call
"Specifying target data objects for all output parameters
"Inline declarations are handy because you can create an
"appropriately typed data object in place. No need to
"create an extra variable, check on the type etc.
zcl_demo_abap_objects_misc=>meth1(
EXPORTING
i_str = `ABAP`
IMPORTING
e_dec = DATA(a)
e_tab = DATA(b)
RECEIVING
r_int = DATA(c)
).
"Functional method call
"The target data object of the returning parameter is specified on the left side of an assignment.
"Note: In this case, you cannot specify inline declarations for the exporting parameters.
DATA e TYPE decfloat34.
DATA f TYPE string_table.
DATA(g) = zcl_demo_abap_objects_misc=>meth1(
EXPORTING
i_str = `ABAP`
IMPORTING
"e_dec = DATA(h)
"e_tab = DATA(i)
e_dec = e
e_tab = f
).
"Benefits of returning parameters: They can, for example, be used in expressions
"The following snippets show a selection (and ignore the available exporting
"parameters).
CASE zcl_demo_abap_objects_misc=>meth1( i_str = `ABAP` ).
WHEN 0. ...
WHEN 1. ...
WHEN OTHERS. ...
ENDCASE.
IF zcl_demo_abap_objects_misc=>meth1( i_str = `ABAP` ) > 5.
...
ELSE.
...
ENDIF.
"IF used with a predicative method call
"The result of the relational expression is true if the result of the functional
"method call is not initial and false if it is initial. The data type of the result
"of the functional meth1od call, i. e. the return value of the called functional method,
"is arbitrary. A check is made for the type-dependent initial value.
IF zcl_demo_abap_objects_misc=>meth1( i_str = `ABAP` ).
...
ELSE.
...
ENDIF.
DO zcl_demo_abap_objects_misc=>meth1( i_str = `ABAP` ) TIMES.
...
ENDDO.
"Method call result as actual parameter
DATA(j) = zcl_demo_abap_objects_misc=>meth1( i_str = `ABAP` i_tab = zcl_demo_abap_objects_misc=>meth2( ) ).
"Examples of returning parameters typed with a table type
LOOP AT zcl_demo_abap_objects_misc=>meth2( ) INTO DATA(wa1).
...
ENDLOOP.
READ TABLE zcl_demo_abap_objects_misc=>meth2( ) INTO DATA(wa2) INDEX 1.
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `7) Self-Reference me` ) ).
str = `AP`.
DATA(text) = meth( ).
out->write( data = text name = `text` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `8) Method Chaining and Chained Attribute Access` ) ).
DATA(txt1) = NEW zcl_demo_abap_objects_misc(
)->add_text( `Hallo`
)->add_space(
)->add_text( xco_cp=>sy->user( )->name
)->add_period(
)->add_space(
)->add_text( `This`
)->add_space(
)->add_text( `is`
)->add_space(
)->add_text( `an`
)->add_space(
)->add_text( `example`
)->add_space(
)->add_text( `of`
)->add_space(
)->add_text( `method`
)->add_space(
)->add_text( `chaining`
)->add_period(
)->return_text( ).
out->write( data = txt1 name = `txt1` ).
out->write( |\n| ).
"The following example chained method call includes a chained attribute
"access at the end so that the target variable contains the content of
"the attribute.
"Example result: Today is 2025-03-05. It's 14:30:38. Have a nice day.
DATA(txt2) = NEW zcl_demo_abap_objects_misc(
)->add_text( `Today`
)->add_space(
)->add_text( `is`
)->add_space(
)->add_text( xco_cp=>sy->date( )->as( xco_cp_time=>format->iso_8601_extended )->value
)->add_period(
)->add_space(
)->add_text( `It's`
)->add_space(
)->add_text( xco_cp=>sy->time( xco_cp_time=>time_zone->user
)->as( xco_cp_time=>format->iso_8601_extended
)->value
)->add_period(
)->add_space(
)->add_text( `Have`
)->add_space(
)->add_text( `a`
)->add_space(
)->add_text( `nice`
)->add_space(
)->add_text( `day`
)->add_period(
)->text.
out->write( data = txt2 name = `txt2` ).
out->write( |\n| ).
"----------------------------------------------------------------
"-------- Method chaining with a standalone statement -----------
"----------------------------------------------------------------
"In the example, the final method call in the chain receives
"the classrun instance available in the implementation of the
"if_oo_adt_classrun~main method. The method implementation
"includes the writing to the console.
"Console output: Lorem ipsum dolor sit amet
NEW zcl_demo_abap_objects_misc( )->add_text( `Lorem`
)->add_space(
)->add_text( `ipsum`
)->add_space(
)->add_text( `dolor`
)->add_space(
)->add_text( `sit`
)->add_space(
)->add_text( `amet`
)->display_text( out ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `9) Demonstrating Upcasts and Downcasts Using the RTTS Inheritance Tree` ) ).
*Hierarchy tree of the classes:
*
*CL_ABAP_TYPEDESCR
* |
* |--CL_ABAP_DATADESCR
* | |
* | |--CL_ABAP_ELEMDESCR
* | | |
* | | |--CL_ABAP_ENUMDESCR
* | |
* | |--CL_ABAP_REFDESCR
* | |--CL_ABAP_COMPLEXDESCR
* | |
* | |--CL_ABAP_STRUCTDESCR
* | |--CL_ABAP_TABLEDESCR
* |
* |--CL_ABAP_OBJECTDESCR
* |
* |--CL_ABAP_CLASSDESCR
* |--CL_ABAP_INTFDESCR
"------------ Object reference variables ------------
"Static and dynamic types
"Defining an object reference variable with a static type
DATA tdo TYPE REF TO cl_abap_typedescr.
"Retrieving type information
"The reference the reference variable points to is either cl_abap_elemdescr,
"cl_abap_enumdescr, cl_abap_refdescr, cl_abap_structdescr, or cl_abap_tabledescr.
"So, it points to one of the subclasses. The static type of tdo refers to
"cl_abap_typedescr, however, the dynamic type is one of the subclasses mentioned.
"in the case of the example, it is cl_abap_elemdescr. Check in the debugger.
DATA some_string TYPE string.
tdo = cl_abap_typedescr=>describe_by_data( some_string ).
"Some more object reference variables
DATA tdo_super TYPE REF TO cl_abap_typedescr.
DATA tdo_elem TYPE REF TO cl_abap_elemdescr.
DATA tdo_data TYPE REF TO cl_abap_datadescr.
DATA tdo_gen_obj TYPE REF TO object.
"------------ Upcasts ------------
"Moving up the inheritance tree
"Assignments:
"- If the static type of target variable is less specific or the same, an assignment works.
"- The target variable inherits the dynamic type of the source variable.
"Static type of target variable is the same
tdo_super = tdo.
"Examples for static types of target variables that are less specific
"Target variable has the generic type object
tdo_gen_obj = tdo.
"Target variable is less specific because the direct superclass of cl_abap_elemdescr
"is cl_abap_datadescr
"Note: In the following three assignments, the target variable remains initial
"since the source variables do not (yet) point to any object.
tdo_data = tdo_elem.
"Target variable is less specific because the direct superclass of cl_abap_datadescr
"is cl_abap_typedescr
tdo_super = tdo_data.
"Target variable is less specific because the class cl_abap_typedescr is higher up in
"the inheritance tree than cl_abap_elemdescr
tdo_super = tdo_elem.
"The casting happens implicitly. You can also excplicitly cast and use
"casting operators, but it is usually not required.
tdo_super = CAST #( tdo ).
tdo_super ?= tdo.
"In combination with inline declarations, the CAST operator can be used to provide a
"reference variable with a more general type.
DATA(tdo_inl_cast) = CAST cl_abap_typedescr( tdo_elem ).
CLEAR: tdo_super, tdo_elem, tdo_data, tdo_gen_obj.
"------------ Downcasts ------------
"Moving down the inheritance tree
"Assignments:
"- If the static type of the target variable is more specific than the static type
" of the source variable, performing a check whether it is less specific or the same
" as the dynamic type of the source variable is required at runtime before the assignment
"- The target variable inherits the dynamic type of the source variable, however, the target
" variable can accept fewer dynamic types than the source variable
"- Downcasts are always performed explicitly using casting operators
"Static type of the target is more specific
"object -> cl_abap_typedescr
tdo_super = CAST #( tdo_gen_obj ).
"cl_abap_typedescr -> cl_abap_datadescr
"Note: Here, the dynamic type of the source variable is cl_abap_elemdescr.
tdo_data = CAST #( tdo ).
"cl_abap_datadescr -> cl_abap_elemdescr
tdo_elem = CAST #( tdo_data ).
"cl_abap_typedescr -> cl_abap_elemdescr
tdo_elem = CAST #( tdo_super ).
"------------ Error prevention in downcasts ------------
"In the examples above, the assignments work. The following code snippets
"deal with examples in which a downcast is not possible. An exception is
"raised.
DATA str_table TYPE string_table.
DATA tdo_table TYPE REF TO cl_abap_tabledescr.
"With the following method call, tdo points to an object with
"reference to cl_abap_tabledescr.
tdo = cl_abap_typedescr=>describe_by_data( str_table ).
"Therefore, the following downcast works.
tdo_table = CAST #( tdo ).
"You could also achieve the same in one statement and with inline
"declaration.
DATA(tdo_table_2) = CAST cl_abap_tabledescr( cl_abap_typedescr=>describe_by_data( str_table ) ).
"Example for an impossible downcast
"The generic object reference variable points to cl_abap_elemdescr after the following
"assignment.
tdo_gen_obj = cl_abap_typedescr=>describe_by_data( some_string ).
"Without catching the exception, the runtime error MOVE_CAST_ERROR
"occurs. There is no syntax error at compile time. The static type of
"tdo_gen_obj is more generic than the static type of the target variable.
"The error occurs when trying to downcast, and the dynamic type is used.
TRY.
tdo_table = CAST #( tdo_gen_obj ).
CATCH cx_sy_move_cast_error.
ENDTRY.
"Note: tdo_table sill points to the reference as assigned above after trying
"to downcast in the TRY control structure.
"Using CASE TYPE OF and IS INSTANCE OF statements, you can check if downcasts
"are possible.
"Note: In case of ...
"- non-initial object reference variables, the dynamic type is checked.
"- initial object reference variables, the static type is checked.
"------------ IS INSTANCE OF ------------
DATA some_tdo TYPE REF TO cl_abap_typedescr.
some_tdo = cl_abap_typedescr=>describe_by_data( str_table ).
IF some_tdo IS INSTANCE OF cl_abap_elemdescr.
DATA(tdo_a) = CAST cl_abap_elemdescr( some_tdo ).
ELSE.
"This branch is executed. The downcast is not possible.
...
ENDIF.
IF some_tdo IS INSTANCE OF cl_abap_elemdescr.
DATA(tdo_b) = CAST cl_abap_elemdescr( some_tdo ).
ELSEIF some_tdo IS INSTANCE OF cl_abap_refdescr.
DATA(tdo_c) = CAST cl_abap_refdescr( some_tdo ).
ELSEIF some_tdo IS INSTANCE OF cl_abap_structdescr.
DATA(tdo_d) = CAST cl_abap_structdescr( some_tdo ).
ELSEIF some_tdo IS INSTANCE OF cl_abap_tabledescr.
"In this example, this branch is executed. With the check,
"you can make sure that the downcast is indeed possible.
DATA(tdo_e) = CAST cl_abap_tabledescr( some_tdo ).
ELSE.
...
ENDIF.
DATA initial_tdo TYPE REF TO cl_abap_typedescr.
IF initial_tdo IS INSTANCE OF cl_abap_elemdescr.
DATA(tdo_f) = CAST cl_abap_elemdescr( some_tdo ).
ELSEIF initial_tdo IS INSTANCE OF cl_abap_refdescr.
DATA(tdo_g) = CAST cl_abap_refdescr( some_tdo ).
ELSEIF initial_tdo IS INSTANCE OF cl_abap_structdescr.
DATA(tdo_h) = CAST cl_abap_structdescr( some_tdo ).
ELSEIF initial_tdo IS INSTANCE OF cl_abap_tabledescr.
DATA(tdo_i) = CAST cl_abap_tabledescr( some_tdo ).
ELSE.
"In this example, this branch is executed. The static
"type of the initial object reference variable is used,
"which is cl_abap_typedescr here.
...
ENDIF.
"------------ CASE TYPE OF ------------
"The examples are desinged similarly to the IS INSTANCE OF examples.
DATA(dref) = REF #( str_table ).
some_tdo = cl_abap_typedescr=>describe_by_data( dref ).
CASE TYPE OF some_tdo.
WHEN TYPE cl_abap_elemdescr.
DATA(tdo_j) = CAST cl_abap_elemdescr( some_tdo ).
WHEN TYPE cl_abap_refdescr.
"In this example, this branch is executed. With the check,
"you can make sure that the downcast is indeed possible.
DATA(tdo_k) = CAST cl_abap_refdescr( some_tdo ).
WHEN TYPE cl_abap_structdescr.
DATA(tdo_l) = CAST cl_abap_structdescr( some_tdo ).
WHEN TYPE cl_abap_tabledescr.
DATA(tdo_m) = CAST cl_abap_tabledescr( some_tdo ).
WHEN OTHERS.
...
ENDCASE.
"Example with initial object reference variable
CASE TYPE OF initial_tdo.
WHEN TYPE cl_abap_elemdescr.
DATA(tdo_n) = CAST cl_abap_elemdescr( some_tdo ).
WHEN TYPE cl_abap_refdescr.
DATA(tdo_o) = CAST cl_abap_refdescr( some_tdo ).
WHEN TYPE cl_abap_structdescr.
DATA(tdo_p) = CAST cl_abap_structdescr( some_tdo ).
WHEN TYPE cl_abap_tabledescr.
DATA(tdo_q) = CAST cl_abap_tabledescr( some_tdo ).
WHEN OTHERS.
"In this example, this branch is executed. The static
"type of the initial object reference variable is used,
"which is cl_abap_typedescr here.
...
ENDCASE.
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `10) Interface Implementation` ) ).
"Note:
"- The example demonstrates that the interface methods declared with DEFAULT
" IGNORE and DEFAULT FAIL are not required to be implemented. In this example
" class, the methods are not implemented. In the zcl_demo_abap_objects class,
" which also includes the interface, they are implemented (but do not contain
" code)
"- So, if implementations are desired, you can manually add the implementations (if
" not automatically added using the ADT quickfix) for these methods.
"- The following code demonstrates calls to implemented interface methods. The
" example also demonstrates the calling of the non-implemented methods, which
" can indeed be specified. Since there is no implementation in the example class,
" errors occur.
"Examples using an object reference variable
DATA(oref) = NEW zcl_demo_abap_objects_misc( ).
oref->add( num1 = 1 num2 = 2 ).
DATA(res1) = oref->res.
out->write( data = res1 name = `res1` ).
out->write( |\n| ).
oref->subtr( num1 = 1 num2 = 2 ).
DATA(res2) = oref->zdemo_abap_objects_interface~subtr_result.
out->write( data = res2 name = `res2` ).
out->write( |\n| ).
"Referring to a type declared in the interface
DATA char_a TYPE zdemo_abap_objects_interface~c3.
DATA char_b TYPE zdemo_abap_objects_interface=>c3.
"Calling non-implemented methods
DATA(int_ig_a) = oref->zdemo_abap_objects_interface~meth_ignore( ).
ASSERT int_ig_a = 0.
TRY.
DATA(int_fl_a) = oref->zdemo_abap_objects_interface~meth_fail( ).
CATCH cx_sy_dyn_call_illegal_method INTO DATA(error).
DATA(error_text) = error->get_text( ).
out->write( data = error_text name = `error_text` ).
out->write( |\n| ).
ENDTRY.
"Similar examples using an interface reference variable
DATA iref TYPE REF TO zdemo_abap_objects_interface.
iref = NEW zcl_demo_abap_objects_misc( ).
iref->addition( num1 = 3 num2 = 5 ).
DATA(res3) = iref->add_result.
out->write( data = res3 name = `res3` ).
out->write( |\n| ).
iref->subtraction( num1 = 3 num2 = 5 ).
DATA(res4) = iref->subtr_result.
out->write( data = res4 name = `res4` ).
out->write( |\n| ).
"Referring to a type declared in the interface
DATA char_c TYPE iref->c3.
"Calling non-implemented methods
DATA(int_ig_b) = iref->meth_ignore( ).
ASSERT int_ig_b = 0.
TRY.
DATA(int_fl_b) = iref->meth_fail( ).
CATCH cx_sy_dyn_call_illegal_method INTO error.
error_text = error->get_text( ).
out->write( data = error_text name = `error_text` ).
out->write( |\n| ).
ENDTRY.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `11) Friendship between Global and Local Classes` ) ).
"Notes:
"- Global class: When running the class, a method of the local class that is declared in the private
" section there is called. As a result of this method call, a string is assigned to an attribute
" that is also declared in the private section of the local class. This attribute is accessed by
" the global class, and finally displayed in the ADT console
"- CCDEF include (Class-relevant Local Types tab in ADT): The LOCAL FRIENDS addition makes the local
" class a friend of the global class. The private components of the global class can then be accessed
" by the local class.
"- CCIMP include (Local Types tab in ADT): The FRIENDS addition makes the global class a friend of the
" local class. The private components of the local class can then be accessed by the global class.
" A type declared in the private section of the global class is used to type an attribute.
" The method, which is also declared in the private section, includes a method call in the implementation.
" It is a method declared in the private section of the global class.
local_class=>say_hello( ).
DATA(hello) = local_class=>hello.
out->write( data = hello name = `hello` ).
ENDMETHOD.
METHOD formal_params_compl_types.
... "No implementation
ENDMETHOD.
METHOD formal_params_generic_types.
... "No implementation
ENDMETHOD.
METHOD meth_opt_1.
IF num IS SUPPLIED.
str = |The parameter is supplied. Value: "{ num }".|.
ELSE.
str = |The parameter is not supplied. Initial value: "{ num }".|.
ENDIF.
ENDMETHOD.
METHOD meth_opt_2.
str = COND #( WHEN num IS SUPPLIED THEN |The parameter is supplied. Value: "{ num }".|
ELSE |The parameter is not supplied. Default value: "{ num }".| ).
ENDMETHOD.
METHOD meth_opt_3.
str = |num1: "{ num1 }" / |.
str &&= |{ COND #( WHEN num2 IS SUPPLIED THEN |num2 (is supplied): "{ num2 }"|
ELSE |num2 (is not supplied; initial value): "{ num2 }"| ) } / |.
str &&= |{ COND #( WHEN num3 IS SUPPLIED THEN |num3 (is supplied): "{ num3 }"|
ELSE |num3 (is not supplied; default value): "{ num3 }"| ) } |.
ENDMETHOD.
METHOD meth_pref.
DATA(addition) = num1 + num2 + num3.
text = |IS SUPPLIED: num1 "{ COND #( WHEN num1 IS SUPPLIED THEN 'X' ELSE '' ) }", | &&
|num2 "{ COND #( WHEN num2 IS SUPPLIED THEN 'X' ELSE '' ) }", | &&
|num3 "{ COND #( WHEN num3 IS SUPPLIED THEN 'X' ELSE '' ) }" / | &&
|Addition result "{ addition }"|.
ENDMETHOD.
METHOD class_constructor.
static_timestamp = utclong_current( ).
stat_constr_call_count += 1.
ENDMETHOD.
METHOD constructor.
instance_timestamp = utclong_current( ).
instance_constr_call_count += 1.
IF text IS SUPPLIED AND text IS NOT INITIAL.
instance_name = text.
ENDIF.
ENDMETHOD.