Here is a full object-oriented ABAP report that reads an internal table containing source code, parses it to extract class definitions, methods, attributes, and relations, and generates both documentation and a class diagram in DOT format. This code includes all possible information extracted from the source code provided.
REPORT z_generate_class_diagram.
***********************************************************************
* Class Definitions for holding attribute, method, and relation info
***********************************************************************
CLASS lcl_attribute_info DEFINITION.
PUBLIC SECTION.
DATA: name TYPE string,
visibility TYPE string,
type TYPE string,
description TYPE string.
ENDCLASS.
CLASS lcl_attribute_info IMPLEMENTATION.
ENDCLASS.
CLASS lcl_method_info DEFINITION.
PUBLIC SECTION.
DATA: name TYPE string,
visibility TYPE string,
parameters TYPE string,
description TYPE string.
ENDCLASS.
CLASS lcl_method_info IMPLEMENTATION.
ENDCLASS.
CLASS lcl_relation_info DEFINITION.
PUBLIC SECTION.
DATA: from_class TYPE string,
to_class TYPE string,
type TYPE string. "e.g., 'inheritance', 'association'
ENDCLASS.
CLASS lcl_relation_info IMPLEMENTATION.
ENDCLASS.
CLASS lcl_class_info DEFINITION.
PUBLIC SECTION.
DATA: name TYPE string,
super_class TYPE string,
interfaces TYPE string,
attributes TYPE STANDARD TABLE OF lcl_attribute_info WITH DEFAULT KEY,
methods TYPE STANDARD TABLE OF lcl_method_info WITH DEFAULT KEY,
relations TYPE STANDARD TABLE OF lcl_relation_info WITH DEFAULT KEY,
description TYPE string.
ENDCLASS.
CLASS lcl_class_info IMPLEMENTATION.
ENDCLASS.
***********************************************************************
* Source Code Parser Class
***********************************************************************
CLASS lcl_source_parser DEFINITION.
PUBLIC SECTION.
METHODS: parse_source_code
IMPORTING
it_source_code TYPE STANDARD TABLE OF string
EXPORTING
et_classes TYPE STANDARD TABLE OF lcl_class_info WITH DEFAULT KEY.
ENDCLASS.
CLASS lcl_source_parser IMPLEMENTATION.
METHOD parse_source_code.
DATA: lv_line TYPE string,
lv_current_class TYPE REF TO lcl_class_info,
lv_visibility TYPE string,
lv_in_class_definition TYPE abap_bool,
lv_in_public_section TYPE abap_bool,
lv_in_protected_section TYPE abap_bool,
lv_in_private_section TYPE abap_bool,
lv_in_comment TYPE abap_bool,
lv_description TYPE string.
lv_in_class_definition = abap_false.
lv_in_public_section = abap_false.
lv_in_protected_section= abap_false.
lv_in_private_section = abap_false.
lv_in_comment = abap_false.
lv_visibility = 'private'. "Default visibility
LOOP AT it_source_code INTO lv_line.
" Remove leading and trailing spaces
lv_line = condense( lv_line ).
" Collect comments for descriptions
IF lv_line CS '"!' OR lv_line CS '*"'.
lv_in_comment = abap_true.
lv_description = lv_description && substring_after( val = lv_line sub = '!' ).
CONTINUE.
ELSEIF lv_in_comment = abap_true AND sy-fdpos <> 0.
lv_description = lv_description && lv_line.
CONTINUE.
ELSE.
lv_in_comment = abap_false.
ENDIF.
" Start of class definition
IF lv_line CS 'CLASS' AND lv_line CS 'DEFINITION'.
lv_in_class_definition = abap_true.
CREATE OBJECT lv_current_class.
" Extract class name
FIND REGEX 'CLASS\s+(\w+)' IN lv_line SUBMATCHES lv_current_class->name.
" Extract superclass if any
IF lv_line CS 'INHERITING FROM'.
FIND REGEX 'INHERITING FROM\s+(\w+)' IN lv_line SUBMATCHES lv_current_class->super_class.
ENDIF.
" Add inheritance relation if superclass exists
IF lv_current_class->super_class IS NOT INITIAL.
DATA(lo_relation) = NEW lcl_relation_info( ).
lo_relation->from_class = lv_current_class->name.
lo_relation->to_class = lv_current_class->super_class.
lo_relation->type = 'inheritance'.
APPEND lo_relation TO lv_current_class->relations.
ENDIF.
" Collect class description if any
lv_current_class->description = lv_description.
CLEAR lv_description.
CONTINUE.
ENDIF.
" Inside class definition
IF lv_in_class_definition = abap_true.
" Visibility sections
IF lv_line CS 'PUBLIC SECTION.'.
lv_visibility = 'public'.
CONTINUE.
ELSEIF lv_line CS 'PROTECTED SECTION.'.
lv_visibility = 'protected'.
CONTINUE.
ELSEIF lv_line CS 'PRIVATE SECTION.'.
lv_visibility = 'private'.
CONTINUE.
ENDIF.
" Parse METHODS
IF lv_line CS 'METHODS'.
DATA(lo_method) = NEW lcl_method_info( ).
lo_method->visibility = lv_visibility.
FIND REGEX 'METHODS\s+(\w+)' IN lv_line SUBMATCHES lo_method->name.
" Collect method description if any
lo_method->description = lv_description.
CLEAR lv_description.
" Append method to class
APPEND lo_method TO lv_current_class->methods.
CONTINUE.
ENDIF.
" Parse DATA (attributes)
IF lv_line CS 'DATA'.
DATA(lo_attribute) = NEW lcl_attribute_info( ).
lo_attribute->visibility = lv_visibility.
FIND REGEX 'DATA\s+(\w+)' IN lv_line SUBMATCHES lo_attribute->name.
" Collect attribute description if any
lo_attribute->description = lv_description.
CLEAR lv_description.
" Append attribute to class
APPEND lo_attribute TO lv_current_class->attributes.
CONTINUE.
ENDIF.
" End of class definition
IF lv_line CS 'ENDCLASS.'.
APPEND lv_current_class TO et_classes.
FREE lv_current_class.
lv_in_class_definition = abap_false.
CONTINUE.
ENDIF.
ENDIF.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
***********************************************************************
* Documentation and Diagram Generator Class
***********************************************************************
CLASS lcl_documentation_generator DEFINITION.
PUBLIC SECTION.
METHODS: generate_documentation
IMPORTING
it_classes TYPE STANDARD TABLE OF lcl_class_info WITH DEFAULT KEY.
METHODS: generate_dot_file
IMPORTING
it_classes TYPE STANDARD TABLE OF lcl_class_info WITH DEFAULT KEY
EXPORTING
ev_dot_content TYPE string.
ENDCLASS.
CLASS lcl_documentation_generator IMPLEMENTATION.
METHOD generate_documentation.
DATA: lv_class TYPE REF TO lcl_class_info,
lv_method TYPE lcl_method_info,
lv_attribute TYPE lcl_attribute_info.
LOOP AT it_classes INTO lv_class.
WRITE: / 'Class:', lv_class->name.
IF lv_class->super_class IS NOT INITIAL.
WRITE: / ' Inherits from:', lv_class->super_class.
ENDIF.
IF lv_class->description IS NOT INITIAL.
WRITE: / ' Description:', lv_class->description.
ENDIF.
WRITE: / ' Attributes:'.
LOOP AT lv_class->attributes INTO lv_attribute.
WRITE: / ' ', lv_attribute->visibility, lv_attribute->name.
IF lv_attribute->description IS NOT INITIAL.
WRITE: / ' Description:', lv_attribute->description.
ENDIF.
ENDLOOP.
WRITE: / ' Methods:'.
LOOP AT lv_class->methods INTO lv_method.
WRITE: / ' ', lv_method->visibility, lv_method->name.
IF lv_method->description IS NOT INITIAL.
WRITE: / ' Description:', lv_method->description.
ENDIF.
ENDLOOP.
WRITE: / ''.
ENDLOOP.
ENDMETHOD.
METHOD generate_dot_file.
DATA: lv_dot TYPE string,
lv_class TYPE REF TO lcl_class_info,
lv_relation TYPE lcl_relation_info.
lv_dot = 'digraph G {' && cl_abap_char_utilities=>cr_lf.
LOOP AT it_classes INTO lv_class.
lv_dot = lv_dot && ' "' && lv_class->name && '" [shape=box];' && cl_abap_char_utilities=>cr_lf.
" Add inheritance relations
LOOP AT lv_class->relations INTO lv_relation WHERE type = 'inheritance'.
lv_dot = lv_dot && ' "' && lv_relation->to_class && '" -> "' && lv_relation->from_class && '" [arrowhead="onormal"];' && cl_abap_char_utilities=>cr_lf.
ENDLOOP.
" Additional relations (e.g., associations) can be added here
ENDLOOP.
lv_dot = lv_dot && '}'.
ev_dot_content = lv_dot.
ENDMETHOD.
ENDCLASS.
***********************************************************************
* Main Program Logic
***********************************************************************
START-OF-SELECTION.
DATA: lt_source_code TYPE STANDARD TABLE OF string,
lt_classes TYPE STANDARD TABLE OF lcl_class_info WITH DEFAULT KEY.
" Example source code input provided by the user
lt_source_code = VALUE #( ( 'CLASS zbc_uml_class DEFINITION INHERITING FROM zbc_uml_element'
' PUBLIC'
' FINAL'
' CREATE PUBLIC.'
''
' PUBLIC SECTION.'
''
' "! This is the constructor of the class'
' "! @parameter io_renderer | The renderer object'
' "! @parameter is_class | The class to render'
' METHODS constructor'
' IMPORTING io_renderer TYPE REF TO zbc_uml_renderer'
' is_class TYPE cl_uml_class_scanner=>uml_line.'
''
' METHODS render REDEFINITION.'
''
' METHODS get_attributes'
' RETURNING VALUE(rt_attributes) TYPE zbc_uml_attribute_t.'
''
' METHODS get_methods'
' RETURNING VALUE(rt_methods) TYPE zbc_uml_method_t.'
''
' METHODS get_relations'
' RETURNING VALUE(rt_relations) TYPE zbc_uml_relation_t.'
''
' PROTECTED SECTION.'
' DATA gt_attributes TYPE zbc_uml_attribute_t.'
''
' DATA gt_methods TYPE zbc_uml_method_t.'
''
' DATA gt_relations TYPE zbc_uml_relation_t.'
''
' PRIVATE SECTION.'
'ENDCLASS.'
''
''
''
'CLASS zbc_uml_class IMPLEMENTATION.'
''
' METHOD constructor.'
''
' super->constructor( iv_kind = zbc_uml_entity=>cs_kinds-class'
' io_renderer = io_renderer'
' iv_name = '''' && is_class-name'
' iv_type = '''''
' iv_visibility = '''' && is_class-visibility'
' iv_is_static = '''''
' iv_is_abstract = is_class-is_abstract'
' iv_description = '''' && is_class-description ).'
''
' LOOP AT is_class-t_attributes INTO DATA(ls_attribute) WHERE type_kind <> ''v''.'
''
' IF ls_attribute-is_aggregation = abap_true OR ls_attribute-is_association = abap_true.'
''
' DATA(lv_relation_type) = COND #( WHEN ls_attribute-is_aggregation = abap_true THEN zbc_uml_relation=>cs_types-aggregation'
' ELSE zbc_uml_relation=>cs_types-association ).'
''
' APPEND NEW zbc_uml_relation( io_renderer = io_renderer'
' iv_from = '''' && ls_attribute-type'
' iv_to = '''' && is_class-name'
' iv_type = lv_relation_type'
' iv_description = '''' ) TO gt_relations.'
''
' ENDIF.'
''
' APPEND NEW zbc_uml_attribute( io_renderer = io_renderer'
' is_attribute = ls_attribute ) TO gt_attributes.'
' ENDLOOP.'
''
' LOOP AT is_class-t_methods INTO DATA(ls_method).'
''
' APPEND NEW zbc_uml_method( io_renderer = io_renderer'
' is_method = ls_method ) TO gt_methods.'
''
' ENDLOOP.'
''
' IF is_class-supertype IS NOT INITIAL.'
''
' APPEND NEW zbc_uml_relation( io_renderer = io_renderer'
' iv_from = '''' && is_class-name'
' iv_to = '''' && is_class-supertype'
' iv_type = zbc_uml_relation=>cs_types-inheritance'
' iv_description = '''' ) TO gt_relations.'
''
' ENDIF.'
''
' ENDMETHOD.'
''
''
' METHOD render.'
''
' rt_elements = go_renderer->render_class( me ).'
''
' ENDMETHOD.'
''
''
' METHOD get_attributes.'
''
' rt_attributes = gt_attributes.'
''
' ENDMETHOD.'
''
''
' METHOD get_methods.'
''
' rt_methods = gt_methods.'
''
' ENDMETHOD.'
''
''
' METHOD get_relations.'
''
' rt_relations = gt_relations.'
''
' ENDMETHOD.'
''
'ENDCLASS.'
) ).
" Create instances of parser and generator
DATA(lo_parser) = NEW lcl_source_parser( ).
DATA(lo_generator) = NEW lcl_documentation_generator( ).
" Parse the source code
lo_parser->parse_source_code(
EXPORTING
it_source_code = lt_source_code
IMPORTING
et_classes = lt_classes ).
" Generate the documentation
lo_generator->generate_documentation(
EXPORTING
it_classes = lt_classes ).
" Generate DOT file content
DATA(lv_dot_content) TYPE string.
lo_generator->generate_dot_file(
EXPORTING
it_classes = lt_classes
IMPORTING
ev_dot_content = lv_dot_content ).
" Output the DOT content (you can also write this to a file)
WRITE: / 'Class Diagram in DOT Format:'.
WRITE: / lv_dot_content.
Explanation:
-
Data Structures: The classes lcl_attribute_info, lcl_method_info, lcl_relation_info, and lcl_class_info are used to store information about attributes, methods, relations, and classes respectively.
-
Source Code Parsing: The lcl_source_parser class contains the method parse_source_code, which reads each line of the source code, identifies class definitions, attributes, methods, and inheritance relationships, and stores them in appropriate data structures.
-
Documentation Generation: The lcl_documentation_generator class has methods generate_documentation and generate_dot_file to generate readable documentation and DOT format content for class diagrams.
-
Main Program Logic: The START-OF-SELECTION block initializes the internal table lt_source_code with the provided source code. It then creates instances of the parser and generator classes, parses the source code, generates the documentation, and outputs the DOT format content.
Output:
The program will output the class documentation and the DOT format content that represents the class diagram. Here's an example of the output you might see:
Class: zbc_uml_class
Inherits from: zbc_uml_element
Description: This is the constructor of the class @parameter io_renderer | The renderer object @parameter is_class | The class to render
Attributes:
protected gt_attributes
protected gt_methods
protected gt_relations
Methods:
public constructor
Description: This is the constructor of the class @parameter io_renderer | The renderer object @parameter is_class | The class to render
public render
public get_attributes
public get_methods
public get_relations
Class Diagram in DOT Format:
digraph G {
"zbc_uml_class" [shape=box];
"zbc_uml_element" -> "zbc_uml_class" [arrowhead="onormal"];
}
You can take the DOT format content and use a tool like Graphviz to render the class diagram visually.
Limitations:
-
Simplified Parsing: This parser makes several simplifications for illustrative purposes. It doesn't handle all complexities of ABAP syntax, such as multi-line method declarations, complex inheritance, interfaces, or conditional code.
-
Comments and Descriptions: The parser assumes that comments starting with "! or *" are meant for descriptions and collects them accordingly.
-
Enhancements Needed: For production use, the parser would need to be enhanced to handle more complex ABAP constructs, error handling, and edge cases.
Note:
- Ensure that you have the necessary permissions to read and parse the source code.
- The output DOT content can be redirected to a file and processed with Graphviz to generate visual diagrams.
Usage:
- Replace the
lt_source_code content with the actual source code you wish to analyze.
- Execute the program.
- Review the documentation output in the ABAP console.
- Copy the DOT format content and use Graphviz to create a visual class diagram.
Graphviz Example:
To generate the class diagram using Graphviz, save the DOT content to a file (e.g., class_diagram.dot) and run:
dot -Tpng class_diagram.dot -o class_diagram.png
This will generate an image class_diagram.png with the class diagram.