ABAP - Novos Comandos
ABAP - Novos Comandos
ABAP - Novos Comandos
Inline Declarations
Constructor Expressions
Table Expressions: Selection, Table Comprehension, and Reduction
Grouping
Further News
Public
Avoid redundancies
Inline declarations
Type inference
Public
p2 = g( ) ).
Computation in Operands
s = |Hello,\n| &
|today is { sy-datum date = iso }\n| &
|now is { sy-uzeit time = iso }|.
String Templates
Public
Inline Declarations
Expressions
Inline Declarations
Inline Declaration
... DATA(var) ...
... FIELD-SYMBOL(<fs>) ...
These new operators combine the declaration and (initial) value assignment of a variable / field symbol.
Allowed at write positions for which a type can be determined statically from the context.
This inferred type is given to the declared symbol.
Public
Expressions
Inline Declarations DATA( )
LOOP AT tab INTO DATA(line).
ENDLOOP.
oref->meth( IMPORTING
p1 = DATA(a1)
p2 = DATA(a2)
RECEIVING
rc = DATA(ok) ).
Public
Expressions
Inline Declarations DATA( )
DATA
DATA
DATA
DATA
ixml
document
streamfact
istream
TYPE
TYPE
TYPE
TYPE
REF
REF
REF
REF
TO
TO
TO
TO
if_ixml.
if_ixml_document.
if_ixml_stream_factory.
if_ixml_istream.
ixml
document
= cl_ixml=>create( ).
= ixml->create_document( ).
streamfact
stream
= ixml->create_stream_factory( ).
= streamfact->create_istream_xstring( ).
DATA(ixml)
= cl_ixml=>create( ).
DATA(document) = ixml->create_document( ).
DATA(stream)
= ixml->create_stream_factory(
)->create_istream_xstring( ).
Public
Expressions
Inline Declarations FIELD-SYMBOL( )
FIELD-SYMBOLS: <line1> LIKE LINE OF tab,
<line> LIKE LINE OF tab,
<comp> TYPE t.
READ TABLE tab ASSIGNING <line1>.
LOOP AT tab ASSIGNING <line>.
ASSIGN COMPONENT OF <line> TO <comp>.
ENDLOOP.
READ TABLE tab ASSIGNING FIELD-SYMBOL(<line1>).
LOOP AT tab ASSIGNING FIELD-SYMBOL(<line>).
ASSIGN COMPONENT OF <line> TO FIELD-SYMBOL(<comp>).
ENDLOOP.
Public
10
Constructor Expressions
Expressions
Constructor Expressions
Syntax:
operator type( )
operator #( )
These new operators construct values of a specified type or inferred type (#), where operator is one
of { NEW, VALUE, } and the syntax inside( ) depends on operator.
NEW
creates objects / data objects
VALUE creates values (esp. of structured types)
REF
creates data references
CONV
converts values
CAST
performs up or down casts of references
EXACT performs lossless calculations or assignments
COND and SWITCH compute values conditionally
CORRESPONDING maps/moves components in structured types
Public
12
Expressions
Constructor Expressions Operator NEW( )
Public
13
Expressions
Constructor Expressions Operator VALUE( )
method( VALUE #(
col1 = 10
( col2 = 'a' )
( col2 = 'b' )
( LINES OF othertab ) ) ).
Public
14
Expressions
Constructor Expressions Operator REF( )
DATA dref TYPE REF TO t_param.
GET REFERENCE OF param INTO dref.
DATA(ptab) = VALUE abap_parmbind_tab(
( name = name
kind = cl_abap_objectdescr=>exporting
value = dref ) ).
CALL METHOD (class)=>(meth) PARAMETER-TABLE ptab.
DATA(ptab) = VALUE abap_parmbind_tab(
( name = name
kind = cl_abap_objectdescr=>exporting
value = REF #( param ) ) ).
CALL METHOD (class)=>(meth) PARAMETER-TABLE ptab.
2014 SAP SE or an SAP affiliate company. All rights reserved.
Public
15
Expressions
Constructor Expressions Operator CONV( )
DATA(i) = 1 / 4.
CHECK ` ` = abap_false.
Public
16
Expressions
Constructor Expressions Local Variable Bindings
Several constructor operators allow the binding of intermediate results to expression-local variables.
Public
17
Expressions
Constructor Expressions Operator CAST( )
Public
18
Expressions
Constructor Expressions Operator EXACT( )
Public
19
Expressions
Constructor Expressions Operator COND( )
DATA time TYPE string.
DATA(now) = sy-timlo.
IF now < '120000'.
time = |{ now TIME = ISO } AM|.
ELSEIF now > '120000'.
time = |{ CONV t( now - 12 * 3600 )
TIME = ISO } PM|.
ELSEIF now = '120000'.
time = `High Noon`.
ELSE.
RAISE EXCEPTION TYPE cx_cant_be.
ENDIF.
DATA(time) =
COND string(
LET now = sy-timlo IN
WHEN now < '120000' THEN
|{ now TIME = ISO } AM|
WHEN now > '120000' THEN
|{ CONV t( now - 12 * 3600 )
TIME = ISO } PM|
WHEN now = '120000' THEN
`High Noon`
ELSE
THROW cx_cant_be( ) ).
Public
20
Expressions
Constructor Expressions Operator SWITCH( )
DATA number TYPE string.
CASE sy-index.
WHEN 1.
number = 'one'.
WHEN 2.
number = 'two'.
WHEN 3.
number = 'three'.
WHEN OTHERS.
RAISE EXCEPTION TYPE cx_overflow.
ENDCASE.
DATA(number) =
SWITCH string( sy-index
WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
WHEN 3 THEN 'three'
ELSE THROW cx_overflow( ) ).
Public
21
Expressions
Constructor Expressions Operator CORRESPONDING( )
This operator applies name-matching recursively to structures and tables.
Components with distinct names can be mapped explicitly.
Components with equal names can be excluded from mapping.
LOOP AT itab1 ASSIGNING <line1>.
MOVE-CORRESPONDING <line1> TO line2.
line2-foo = <line1>-bar.
CLEAR line2-baz.
INSERT line2 INTO TABLE itab2.
ENDLOOP.
Public
22
Exercise
Table Expressions
Expressions
Table Expressions
Syntax:
tab[ ]
Public
25
Expressions
Table Selection Specification of Table Line
wa = tab[ idx ].
READ TABLE tab INDEX idx INTO wa.
Public
26
Expressions
Table Selection Retrieval Method
READ TABLE tab ASSIGNING <line>.
<line>-col = 10.
SP8
Public
27
Expressions
Table Selection Chaining
11
12
13
14
15
16
17
18
10
Public
28
Expressions
Table Selection Built-in Functions
ENDIF.
IF line_exists( tab[ ] ).
ENDIF.
Public
29
Expressions
Table Comprehensions Building Tables Functionally
Table-driven:
For each selected line in the source table(s), construct a line in the result table.
Generalization of value constructor from static to dynamic number of lines
Condition-driven:
SP8
Construct lines in the result table until the condition on the loop variable is false.
Public
30
Expressions
Table Comprehensions - Example
Build cross product of two tables.
LOOP AT tab1 INTO line1 WHERE val = 1.
LOOP AT tab2 ASSIGNING <line2>.
line3-c = f( line1-a ).
line3-d = <line2>-b.
APPEND line3 TO tab1_x_tab2.
ENDLOOP.
ENDLOOP.
tab1_x_tab2 = VALUE #(
FOR line1
IN tab1 WHERE ( val = 1 )
FOR <line2> IN tab2
( c = f( line1-a ) d = <line2>-b ) ).
Public
31
Expressions
Reductions Computing Aggregate Values Functionally
Syntax:
SP8
In each iteration (table-driven or condition-driven), re-compute the result value (and helper variables).
Generalization of table comprehension from table to arbitrary result type
Public
32
Expressions
Reductions - Example
Render columns of an internal table into an HTML string.
Public
33
Grouping
SP8
Grouping
Processing Table Lines in Groups Statement Variant
LOOP AT tab bind(x)
WHERE
GROUP BY groupkey(x)
bind(g).
ENDLOOP.
ENDLOOP.
Public
35
Grouping
Processing Table Lines in Groups Expression Variant
Table Comprehension
VALUE #( FOR GROUPS g OF x IN tab GROUP BY groupkey(x)
( FOR m IN GROUP g ( ) )
)
Reduction
REDUCE #( INIT r =
FOR GROUPS g OF x IN tab GROUP BY groupkey(x)
NEXT r = FOR m IN GROUP g ( )
)
Public
36
Grouping
Processing Table Lines in Groups - Example
Group flights by duration.
LOOP AT flights ASSIGNING FIELD-SYMBOL(<flg>)
GROUP BY COND #( WHEN <flg>-fltime < 120 THEN 0
WHEN <flg>-fltime > 600 THEN 99
ELSE trunc( <flg>-fltime / '60' ) )
ASCENDING
ASSIGNING FIELD-SYMBOL(<fd>).
WRITE / |Duration: { COND #( WHEN <fd> = 0 THEN `less than 2`
WHEN <fd> = 99 THEN `more than 10`
ELSE <fd> ) } hours|.
LOOP AT GROUP <fd> ASSIGNING <flg>.
WRITE: |{ <flg>-cityfrom }-{ <flg>-cityto }: { <flg>-fltime }|.
ENDLOOP.
ENDLOOP.
Public
37
wa1-b = 9.
INTO TABLE it1.
wa1-b = 5.
INTO TABLE it1.
ABAP 7.40
" no redundant variable declarations
" value constructor expression (for table);
" inline declaration with inferred type
DATA(it1) = VALUE tt1( ( a = 7 b = 9 )
( a = 3 b = 5 ) ).
" table comprehension
it2 = VALUE #( FOR <wa1> IN it1
( t1_to_t2( <wa1> ) ) ).
Public
38
Expressions
ABAP is Extensively Expression Enabled Pitfalls
Obfuscation
lcl=>cm_ii(
lcl=>factory2(
f_in1 = lcl=>factory0( )->im_ii( i )
f_in2 = lcl=>cm_ii( 2 ** i ) - 3 +
itab[ 2 ]-s-xtab[ x = 'abc' ]
)->m_ii(
lcl=>factory1( f_in = itab[ a = 2
b = 4711 / 2
]-x
)->m_lif_i( lcl=>cm_ii( 5 ) )->im_ii( 6 )
)
).
Performance
itab1[ 1 ]-a = itab2[ 1 ]-x.
itab1[ 1 ]-b = itab2[ 1 ]-y.
itab1[ 1 ]-c = itab2[ 1 ]-z.
Public
39
Exercise
Further News
JSON
ABAP and JSON
JSON Support in sXML-Library
Backported to
7.02!
JSON - JavaScript Object Notation, data format in text form for data exchange.
JSON-XML - SAP-specific representation of JSON data in XML format
asJSON - ABAP Serialization JSON,
Canonical JSON representation for serialization/deserialization of ABAP data by transformation ID
Public
42
JSON
Readers and Writers
JSON to JSON-XML and Vice Versa
DATA(json) = cl_abap_codepage=>convert_to( `{"TEXT":"JSON"}` ).
DATA(json_reader) = cl_sxml_string_reader=>create( json ).
DATA(xml_writer) = cl_sxml_string_writer=>create( ).
json_reader->next_node( ).
json_reader->skip_node( xml_writer ).
cl_demo_output=>display_xml( xml_writer->get_output( ) ).
DATA(xml) = cl_abap_codepage=>convert_to(
`<object><str name="TEXT">JSON</str></object>` ).
DATA(xml_reader) = cl_sxml_string_reader=>create( xml ).
DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
xml_reader->next_node( ).
xml_reader->skip_node( json_writer ).
cl_demo_output=>display_json( json_writer->get_output( ) ).
Public
43
JSON
Transformation ID
JSON to JSON-XML and Vice Versa
DATA(json) = `{"TEXT":"JSON"}`.
CALL TRANSFORMATION id SOURCE XML json
RESULT XML DATA(xml).
cl_demo_output=>display_xml( xml ).
Public
44
JSON
asJSON
ABAP to JSON and Vice Versa
DATA(text) = `JSON`.
DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE text = text
RESULT XML json_writer.
cl_demo_output=>display_json( json_writer->get_output( ) ).
DATA(json) = `{"TEXT":"JSON"}`.
DATA text TYPE string.
CALL TRANSFORMATION id SOURCE XML json
RESULT text = text.
cl_demo_output=>display( text ).
Public
45
ABAP Channels
ABAP Messaging Channels (AMC) and ABAP Push Channels (APC)
A
P
C
AS ABAP Session
Application Server 1
A
P
C
AMC
WebSocket Protocol
AS ABAP Session
Application Server 2
Public
46
ABAP Channels
Characteristcs
ABAP Push Channel (APC)
Integration of WebSocket (RFC 6455) into ABAP
Outlook: SAPGUI Push Channel
Public
47
ABAP Channels
ABAP Messaging Channels
Enable message based communications between ABAP programs across the boundaries of application servers.
CAST if_amc_message_producer_text(
cl_amc_channel_manager=>create_message_producer(
i_application_id = 'DEMO_AMC'
i_channel_id
= '/demo_text' )
)->send( i_message = ... ).
cl_amc_channel_manager=>create_message_consumer(
i_application_id = 'DEMO_AMC'
i_channel_id
= '/demo_text'
)->start_message_delivery( i_receiver = receiver ).
WAIT FOR MESSAGING CHANNELS
UNTIL receiver->text_message IS NOT INITIAL
UP TO 60 SECONDS.
2014 SAP SE or an SAP affiliate company. All rights reserved.
Public
48
ABAP Channels
ABAP Push Channels ABAP side
Enable bidirectional communications between ABAP programs and the
internet using the WebSocket protocol. APCs can be connected to AMCs.
METHOD if_apc_ws_extension~on_start.
...
IF amc_flag = abap_true.
TRY.
i_context->get_binding_manager(
)->bind_amc_message_consumer(
i_application_id = 'DEMO_AMC'
i_channel_id
= '/demo_text' ).
CATCH cx_apc_error INTO DATA(exc).
MESSAGE exc->get_text( ) TYPE 'X'.
ENDTRY.
ELSE.
"Default behavior
ENDIF.
ENDMETHOD.
METHOD if_apc_ws_extension~on_message.
...
IF amc_flag = abap_true.
CAST if_amc_message_producer_text(
cl_amc_channel_manager=>create_message_producer(
i_application_id = 'DEMO_AMC'
i_channel_id
= '/demo_text' )
)->send( i_message = ... ).
ELSE.
DATA(message_manager) =
i_message->get_context( )->get_message_manager( ).
DATA(message) = message_manager->create_message( ).
message->set_text( ... ).
message_manager->send( message ).
ENDIF.
Public
49
ABAP Channels
ABAP Push Channels Internet side
Public
50
Further News
Security Checks
SLIN_SEC
DATA name TYPE string.
DATA customers TYPE TABLE OF scustom WITH EMPTY KEY.
cl_demo_input=>request( CHANGING field = name ).
DATA(cond) = `country = 'DE' AND name = '` && name && `'`.
TRY.
SELECT * FROM scustom
INTO TABLE customers
WHERE (cond).
cl_demo_output=>display( customers ).
CATCH cx_sy_dynamic_osql_syntax.
cl_demo_output=>display( 'Wrong input' ).
ENDTRY.
Public
51
Further News
ABAP Doc for ABAP in Eclipse
Inline Documentation of Source Code based Development Objects
F2
Public
52
Documentation
See ABAP Keyword Documentation
Classic
2014 SAP SE or an SAP affiliate company. All rights reserved.
ADT
Public
53