Please understand this report: START-OF-SELECTION. DATA(oclassname) = ZBCUMLSCANNER .…
- User
Please understand this report: START-OF-SELECTION. DATA(o_class_name) = |ZBC_UML_SCANNER|.
DATA(o_class_desc) = CAST cl_abap_classdescr( cl_abap_typedescr=>describe_by_name( o_class_name ) ). cl_demo_output=>write_data( o_class_desc->kind ).
LOOP AT o_class_desc->methods ASSIGNING FIELD-SYMBOL().
cl_demo_output=>write( <m>-name ). LOOP AT <m>-parameters ASSIGNING FIELD-SYMBOL(<p>). cl_demo_output=>write( <p>-name ). ENDLOOP.ENDLOOP.
cl_demo_output=>display( ).
Append it by the functionality to read the source code and add comments to attributes, params, classes and methods by scrapping the source code. Also append the comments within in a method to a method.
How to get the source code: TRY.
-
Objektname: Klasse oder Interface DATA(lv_obj_name) = |ZBC_UML_CLASS|.
DATA(o_source) = cl_oo_factory=>create_instance( ). DATA(o_source2) = o_source->create_clif_source( clif_name = lv_obj_name version = if_oo_clif_source=>co_version_active ). -
Typ ausgeben WRITE: / COND string( WHEN o_source2->get_type( ) = 0 THEN 'Class' ELSE 'Interface' ).
ULINE. -
Code zum Objekt holen lt_source = VALUE rswsourcet( ). o_source2->get_source( IMPORTING source = lt_source ).
CATCH cx_root INTO DATA(e_txt). WRITE: / e_txt->get_text( ).ENDTRY.
Example Sourcecode to understand the pattern:
CLASS zbc_uml_scanner DEFINITION PUBLIC FINAL CREATE PUBLIC .
PUBLIC SECTION.
"! Scan the specified object and return the diagram data "! @parameter iv_obj_name | Object name "! @parameter iv_devclass | Development class "! @parameter rv_data | Diagram data CLASS-METHODS scan IMPORTING iv_obj_name TYPE sobj_name iv_devclass TYPE devclass RETURNING VALUE(rv_data) TYPE cl_uml_class_scanner=>uml_tab.PROTECTED SECTION. PRIVATE SECTION. ENDCLASS.
CLASS zbc_uml_scanner IMPLEMENTATION.
METHOD scan.
" Define which types to scan DATA: lt_scan_types TYPE cl_uml_class_scanner=>uml_range_types.- APPEND iv_obj_name TO lt_scan_types.
APPEND VALUE #( sign = 'I' option = 'EQ' low = iv_obj_name ) TO lt_scan_types." Define which packages to scan DATA: lt_scan_packages TYPE cl_uml_class_scanner=>uml_range_packages. APPEND VALUE #( sign = 'I' option = 'EQ' low = iv_devclass ) TO lt_scan_packages. cl_uml_cache=>get_singleton( )->clear_type_cache( ). " Execute the scanner TRY. DATA(lo_uml_scanner) = NEW cl_uml_class_scanner( ). lo_uml_scanner->execute( EXPORTING scan_packages = lt_scan_packages scan_types = lt_scan_types ). CATCH cx_root INTO DATA(lx_root). RAISE EXCEPTION lx_root. ENDTRY. lo_uml_scanner->set_scanner_configuration( EXPORTING scan_local_types = abap_true add_structures = abap_true scan_used_types = abap_true scan_function_groups = abap_true scan_programs = abap_true scan_deep = abap_true scan_business_objects = abap_false max_scan_depth = 10 ). " Get the diagram data DATA: lv_kind TYPE c, lr_data TYPE REF TO data. lo_uml_scanner->get_diagram( IMPORTING e_kind = lv_kind CHANGING c_data = lr_data ). IF lv_kind <> lo_uml_scanner->c_kind_uml_tab. MESSAGE 'Unexpected diagram kind' TYPE 'E'. RETURN. ENDIF. " Assign the diagram data to internal table FIELD-SYMBOLS: <lt_uml_tab> TYPE cl_uml_class_scanner=>uml_tab. ASSIGN lr_data->* TO <lt_uml_tab>. IF sy-subrc <> 0 OR <lt_uml_tab> IS INITIAL. MESSAGE 'Failed to retrieve diagram data' TYPE 'E'. RETURN. ENDIF. rv_data = <lt_uml_tab>.ENDMETHOD.
ENDCLASS.
-
- Assistant
Here's an updated version of your report that includes functionality to read the source code and extract comments for classes, methods, attributes, and parameters by parsing the source code. This code will associate the comments with the relevant elements and display them accordingly.
REPORT zuml_class_scanner. START-OF-SELECTION. DATA(o_class_name) = |ZBC_UML_SCANNER|. " Get the class description DATA(o_class_desc) = CAST cl_abap_classdescr( cl_abap_typedescr=>describe_by_name( o_class_name ) ). " Get the source code of the class DATA: lt_source TYPE STANDARD TABLE OF string, lv_source_str TYPE string. TRY. DATA(o_source) = cl_oo_factory=>create_instance( ). DATA(o_source2) = o_source->create_clif_source( clif_name = o_class_name version = if_oo_clif_source=>co_version_active ). " Get the source code lines o_source2->get_source( IMPORTING source = lt_source ). CATCH cx_root INTO DATA(e_txt). cl_demo_output=>write( e_txt->get_text( ) ). cl_demo_output=>display( ). RETURN. ENDTRY. " Concatenate source code lines into a single string lv_source_str = sy-langu. LOOP AT lt_source INTO DATA(ls_line). CONCATENATE lv_source_str ls_line cl_abap_char_utilities=>cr_lf INTO lv_source_str. ENDLOOP. " Parse source code to extract comments and associate them with elements DATA: lt_comments TYPE TABLE OF ty_comment_mapping. PERFORM parse_source_code USING lv_source_str CHANGING lt_comments. " Display class kind cl_demo_output=>write( |Class Kind: { o_class_desc->kind }| ). " Display class comment READ TABLE lt_comments INTO DATA(ls_class_comment) WITH KEY element_type = 'CLASS'. IF sy-subrc = 0. cl_demo_output=>write( |Class Comment: { ls_class_comment-comment }| ). ENDIF. " Loop over attributes LOOP AT o_class_desc->attributes ASSIGNING FIELD-SYMBOL(<attribute>). " Get attribute comment READ TABLE lt_comments INTO DATA(ls_attr_comment) WITH KEY element_type = 'ATTRIBUTE' element_name = <attribute>-name. IF sy-subrc = 0. cl_demo_output=>write( |Attribute { <attribute>-name } - { ls_attr_comment-comment }| ). ELSE. cl_demo_output=>write( |Attribute { <attribute>-name }| ). ENDIF. ENDLOOP. " Loop over methods LOOP AT o_class_desc->methods ASSIGNING FIELD-SYMBOL(<method>). " Get method comment READ TABLE lt_comments INTO DATA(ls_method_comment) WITH KEY element_type = 'METHOD' element_name = <method>-name. IF sy-subrc = 0. cl_demo_output=>write( |Method { <method>-name } - { ls_method_comment-comment }| ). ELSE. cl_demo_output=>write( |Method { <method>-name }| ). ENDIF. " Loop over parameters LOOP AT <method>-parameters ASSIGNING FIELD-SYMBOL(<param>). " Get parameter comment READ TABLE lt_comments INTO DATA(ls_param_comment) WITH KEY element_type = 'PARAMETER' parent_name = <method>-name element_name = <param>-name. IF sy-subrc = 0. cl_demo_output=>write( |Parameter { <param>-name } - { ls_param_comment-comment }| ). ELSE. cl_demo_output=>write( |Parameter { <param>-name }| ). ENDIF. ENDLOOP. ENDLOOP. cl_demo_output=>display( ). " Definition of types and form routines TYPES: BEGIN OF ty_comment_mapping, element_type TYPE string, " 'CLASS', 'ATTRIBUTE', 'METHOD', 'PARAMETER' element_name TYPE string, parent_name TYPE string, " For parameters, the parent method name comment TYPE string, END OF ty_comment_mapping. FORM parse_source_code USING iv_source_str TYPE string CHANGING ct_comments TYPE TABLE OF ty_comment_mapping. DATA: lt_lines TYPE STANDARD TABLE OF string, lv_line TYPE string. " Split source code string into individual lines SPLIT iv_source_str AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_lines. DATA: lv_current_comments TYPE string, lv_element_type TYPE string, lv_element_name TYPE string, lv_parent_name TYPE string. LOOP AT lt_lines INTO lv_line. " Remove leading and trailing spaces lv_line = condense( lv_line ). IF lv_line CS '"!'. " This line is a comment starting with "! DATA(lv_comment_line) = lv_line+2. IF lv_current_comments IS INITIAL. lv_current_comments = lv_comment_line. ELSE. CONCATENATE lv_current_comments lv_comment_line INTO lv_current_comments SEPARATED BY cl_abap_char_utilities=>cr_lf. ENDIF. ELSEIF lv_line CS 'CLASS' AND ( lv_line CS 'DEFINITION' OR lv_line CS 'IMPLEMENTATION' ). " Class definition or implementation FIND REGEX 'CLASS\s+(\w+)' IN lv_line SUBMATCHES lv_element_name. lv_element_type = 'CLASS'. " Store comments with the class IF lv_current_comments IS NOT INITIAL. APPEND VALUE ty_comment_mapping( element_type = lv_element_type element_name = lv_element_name comment = lv_current_comments ) TO ct_comments. CLEAR: lv_current_comments. ENDIF. ELSEIF lv_line CS 'PUBLIC SECTION.' OR lv_line CS 'PROTECTED SECTION.' OR lv_line CS 'PRIVATE SECTION.'. " Section markers - reset comments CLEAR: lv_current_comments. ELSEIF lv_line CS 'METHODS' OR lv_line CS 'CLASS-METHODS'. " Method definition FIND REGEX '(METHODS|CLASS-METHODS)\s+(\w+)' IN lv_line SUBMATCHES DATA(lv_dummy) lv_element_name. lv_element_type = 'METHOD'. " Store comments with the method IF lv_current_comments IS NOT INITIAL. APPEND VALUE ty_comment_mapping( element_type = lv_element_type element_name = lv_element_name comment = lv_current_comments ) TO ct_comments. CLEAR: lv_current_comments. ENDIF. ELSEIF lv_line CS 'METHOD' AND lv_line CS '.'. " Method implementation start FIND REGEX 'METHOD\s+(\w+)\.' IN lv_line SUBMATCHES lv_element_name. lv_element_type = 'METHOD_IMPL'. " Store comments with the method implementation IF lv_current_comments IS NOT INITIAL. APPEND VALUE ty_comment_mapping( element_type = lv_element_type element_name = lv_element_name comment = lv_current_comments ) TO ct_comments. CLEAR: lv_current_comments. ENDIF. ELSEIF lv_line CS 'IMPORTING' OR lv_line CS 'EXPORTING' OR lv_line CS 'CHANGING' OR lv_line CS 'RAISING'. " Parameter sections - do nothing CONTINUE. ELSEIF lv_line CS 'TYPE' AND ( lv_line CS 'DATA' OR lv_line CS 'CONSTANTS' OR lv_line CS 'CLASS-DATA' ). " Attribute definition FIND REGEX '(DATA|CONSTANTS|CLASS-DATA)\s+(\w+)' IN lv_line SUBMATCHES DATA(lv_dummy) lv_element_name. lv_element_type = 'ATTRIBUTE'. " Store comments with the attribute IF lv_current_comments IS NOT INITIAL. APPEND VALUE ty_comment_mapping( element_type = lv_element_type element_name = lv_element_name comment = lv_current_comments ) TO ct_comments. CLEAR: lv_current_comments. ENDIF. ELSEIF lv_line CS 'ENDCLASS.' OR lv_line CS 'ENDMETHOD.'. " End of class or method - reset comments CLEAR: lv_current_comments. ELSEIF lv_line CS '"! @parameter'. " Parameter comment FIND REGEX '"!\s+@parameter\s+(\w+)\s*\|\s*(.*)' IN lv_line SUBMATCHES lv_element_name DATA(lv_param_comment). " Associate with the last method encountered IF lv_element_type = 'METHOD'. lv_parent_name = lv_element_name. ENDIF. " Store the parameter comment APPEND VALUE ty_comment_mapping( element_type = 'PARAMETER' element_name = lv_element_name parent_name = lv_parent_name comment = lv_param_comment ) TO ct_comments. ELSE. " Reset comments if not a comment or relevant line CLEAR: lv_current_comments. ENDIF. ENDLOOP. ENDFORM.Explanation:
-
Getting the Class Description and Source Code:
- We use
cl_abap_typedescr=>describe_by_nameto get the class description. - We retrieve the source code of the class using
cl_oo_factoryandcreate_clif_source. - The source code is stored in
lt_source.
- We use
-
Parsing the Source Code:
- We concatenate the source code lines into a single string
lv_source_strfor easier processing. - The
parse_source_codeform routine parses the source code to extract comments and associate them with elements. - We use regular expressions to identify comments and the elements they belong to.
- Comments starting with
"!"are considered. - We handle comments for classes, methods, attributes, and parameters.
- For parameters, we specifically look for lines starting with
"! @parameter"to extract parameter comments.
- We concatenate the source code lines into a single string
-
Associating Comments with Elements:
- Comments are stored in an internal table
lt_commentsof typety_comment_mapping, which includes the element type, name, parent name (for parameters), and the comment. - When we encounter an element (e.g., method definition), we store any accumulated comments with that element and reset the comment accumulator.
- Comments are stored in an internal table
-
Displaying the Results:
- We loop over the class's methods and attributes.
- For each element, we look up its comments in
lt_commentsand display them alongside the element's name. - For parameters, we use the parent method name to correctly associate parameter comments.
-
Handling Parameter Comments:
- Parameters are associated with their parent method using the
parent_namefield inty_comment_mapping. - When displaying parameters, we read comments with both the parameter name and parent method name as keys.
- Parameters are associated with their parent method using the
Note: This code assumes that the source code follows standard formatting conventions and that comments are placed immediately before the elements they describe. It may need adjustments if the source code deviates from these conventions.
Example Output:
Assuming the class
ZBC_UML_SCANNERcontains the comments as provided in your example, the output will display the class kind, class comment, attributes with comments, methods with their comments, and method parameters with their comments.
Let me know if you need further assistance or modifications to this code!
-