Certainly! Below is an ABAP report that reads the source code of a given class, parses it to extract its attributes, methods, comments, and relationships (including compositions), and then generates a class diagram in the DOT format. This DOT output can be used with Graphviz to create a visual class diagram.
ABAP Report: Z_CLASS_DIAGRAM_GENERATOR
REPORT z_class_diagram_generator.
PARAMETERS p_classname TYPE seoclsname DEFAULT 'ZCA_NN_NEURON'.
TYPES:
BEGIN OF ty_attribute,
name TYPE string,
type TYPE string,
section TYPE string, " Public, Protected, Private
comment TYPE string,
END OF ty_attribute,
tt_attributes TYPE STANDARD TABLE OF ty_attribute WITH DEFAULT KEY,
BEGIN OF ty_method,
name TYPE string,
parameters TYPE string,
section TYPE string,
comment TYPE string,
END OF ty_method,
tt_methods TYPE STANDARD TABLE OF ty_method WITH DEFAULT KEY,
BEGIN OF ty_type,
name TYPE string,
definition TYPE string,
section TYPE string,
comment TYPE string,
END OF ty_type,
tt_types TYPE STANDARD TABLE OF ty_type WITH DEFAULT KEY,
BEGIN OF ty_relation,
related_class TYPE seoclsname,
relation_type TYPE string, " Association, Composition, etc.
attribute_name TYPE string,
END OF ty_relation,
tt_relations TYPE STANDARD TABLE OF ty_relation WITH DEFAULT KEY.
DATA: lt_code TYPE STANDARD TABLE OF string WITH DEFAULT KEY,
lt_attributes TYPE tt_attributes,
lt_methods TYPE tt_methods,
lt_types TYPE tt_types,
lt_relations TYPE tt_relations.
DATA: lv_current_section TYPE string,
lv_class_name TYPE seoclsname,
lv_in_class_definition TYPE abap_bool,
lv_in_class_implementation TYPE abap_bool,
lv_last_comment TYPE string.
START-OF-SELECTION.
" Read the source code of the class
DATA: o_source TYPE REF TO if_oo_clif_source,
it_code TYPE rswsourcet.
TRY.
o_source = cl_oo_factory=>create_instance( )->create_clif_source(
clif_name = p_classname
version = if_oo_clif_source=>co_version_active ).
o_source->get_source( IMPORTING source = it_code ).
CATCH cx_root INTO DATA(e_txt).
WRITE: / e_txt->get_text( ).
RETURN.
ENDTRY.
" Convert source code to plain string table
LOOP AT it_code INTO DATA(ls_code_line).
APPEND ls_code_line-line TO lt_code.
ENDLOOP.
" Process the source code
LOOP AT lt_code ASSIGNING FIELD-SYMBOL(<line>).
DATA(lv_line) = <line>.
" Remove leading and trailing spaces
lv_line = condense( lv_line ).
" Check for CLASS ... DEFINITION
IF lv_line CS 'CLASS' AND lv_line CS 'DEFINITION'.
lv_in_class_definition = abap_true.
lv_in_class_implementation = abap_false.
" Extract class name
DATA: lv_class_match TYPE string.
IF lv_line MATCHES 'CLASS * DEFINITION*'.
lv_class_match = lv_line.
REPLACE FIRST OCCURRENCE OF REGEX 'CLASS\s+' IN lv_class_match WITH ''.
REPLACE FIRST OCCURRENCE OF REGEX '\s+DEFINITION.*' IN lv_class_match WITH ''.
lv_class_name = lv_class_match.
ENDIF.
CONTINUE.
ENDIF.
" Check for CLASS ... IMPLEMENTATION
IF lv_line CS 'CLASS' AND lv_line CS 'IMPLEMENTATION'.
lv_in_class_implementation = abap_true.
lv_in_class_definition = abap_false.
CONTINUE.
ENDIF.
" Check for ENDCLASS.
IF lv_line = 'ENDCLASS.'.
lv_in_class_definition = abap_false.
lv_in_class_implementation = abap_false.
CONTINUE.
ENDIF.
" Process within class definition
IF lv_in_class_definition = abap_true.
" Check for section change
IF lv_line = 'PUBLIC SECTION.'.
lv_current_section = 'PUBLIC'.
CONTINUE.
ELSEIF lv_line = 'PROTECTED SECTION.'.
lv_current_section = 'PROTECTED'.
CONTINUE.
ELSEIF lv_line = 'PRIVATE SECTION.'.
lv_current_section = 'PRIVATE'.
CONTINUE.
ENDIF.
" Check for comments
IF lv_line STARTS WITH '"!'.
" Remove leading '"!' and spaces
lv_last_comment = lv_line+2.
lv_last_comment = condense( lv_last_comment ).
CONTINUE.
ENDIF.
" Check for TYPES declarations
IF lv_line STARTS WITH 'TYPES:' OR
lv_line STARTS WITH 'TYPES'.
" Collect type definitions
DATA(ls_type) = VALUE ty_type(
name = lv_line
definition = lv_line
section = lv_current_section
comment = lv_last_comment ).
APPEND ls_type TO lt_types.
CLEAR lv_last_comment.
CONTINUE.
ENDIF.
" Check for DATA declarations
IF lv_line STARTS WITH 'DATA'.
" Remove leading 'DATA' and condense
DATA(lv_data_line) = lv_line+4.
lv_data_line = condense( lv_data_line ).
" Remove ending '.' if present
IF lv_data_line+strlen( lv_data_line )-1(1 ) EQ '.'.
lv_data_line = lv_data_line( strlen( lv_data_line ) - 1 ).
ENDIF.
" Now, extract attribute name and type
DATA: lv_name TYPE string,
lv_type TYPE string.
IF lv_data_line CS 'TYPE'.
SPLIT lv_data_line AT 'TYPE' INTO lv_name lv_type.
lv_name = condense( lv_name ).
lv_type = condense( lv_type ).
" Handle possible '=' in declaration
IF lv_name CS '='.
SPLIT lv_name AT '=' INTO lv_name DATA(lv_value).
lv_name = condense( lv_name ).
ENDIF.
DATA(ls_attribute) = VALUE ty_attribute(
name = lv_name
type = lv_type
section = lv_current_section
comment = lv_last_comment ).
APPEND ls_attribute TO lt_attributes.
" Check if the type is a class (simplified check)
IF lv_type STARTS WITH 'REF TO '.
DATA(lv_referenced_class) = lv_type+7.
lv_referenced_class = condense( lv_referenced_class ).
DATA(ls_relation) = VALUE ty_relation(
attribute_name = lv_name
related_class = lv_referenced_class
relation_type = 'Association' ).
APPEND ls_relation TO lt_relations.
ELSE.
" For simplification, assume types starting with 'Z' are classes
IF lv_type CN '|' AND lv_type CN '#' AND lv_type(1) = 'Z'.
DATA(ls_relation2) = VALUE ty_relation(
attribute_name = lv_name
related_class = lv_type
relation_type = 'Composition' ).
APPEND ls_relation2 TO lt_relations.
ENDIF.
ENDIF.
CLEAR lv_last_comment.
CONTINUE.
ENDIF.
ENDIF.
" Check for METHODS declarations
IF lv_line STARTS WITH 'METHODS'.
" Remove leading 'METHODS' and condense
DATA(lv_method_line) = lv_line+7.
lv_method_line = condense( lv_method_line ).
" Remove ending '.' if present
IF lv_method_line+strlen( lv_method_line )-1(1 ) EQ '.'.
lv_method_line = lv_method_line( strlen( lv_method_line ) - 1 ).
ENDIF.
" Split at space to get method name and parameters
SPLIT lv_method_line AT SPACE INTO DATA(lv_method_name) DATA(lv_parameters).
DATA(ls_method) = VALUE ty_method(
name = lv_method_name
parameters = lv_parameters
section = lv_current_section
comment = lv_last_comment ).
APPEND ls_method TO lt_methods.
CLEAR lv_last_comment.
CONTINUE.
ENDIF.
ENDIF.
ENDLOOP.
" Generate DOT format output
DATA: lv_dot TYPE string.
lv_dot = 'digraph G {' && cl_abap_char_utilities=>cr_lf.
" Build class node
DATA: lv_attributes_text TYPE string,
lv_methods_text TYPE string.
" Attributes
LOOP AT lt_attributes INTO DATA(ls_attr).
lv_attributes_text = lv_attributes_text && ls_attr-name && ': ' && ls_attr-type && '\l'.
ENDLOOP.
" Methods
LOOP AT lt_methods INTO DATA(ls_meth).
lv_methods_text = lv_methods_text && ls_meth-name && '()' && '\l'.
ENDLOOP.
" Create node for the main class
lv_dot = lv_dot && '"' && lv_class_name && '" [shape=record, label="{'
&& lv_class_name && '|'
&& lv_attributes_text && '|'
&& lv_methods_text && '}"];' && cl_abap_char_utilities=>cr_lf.
" Create nodes for related classes (simplified)
LOOP AT lt_relations INTO DATA(ls_rel).
lv_dot = lv_dot && '"' && ls_rel-related_class && '" [shape=record, label="{'
&& ls_rel-related_class && '}"];' && cl_abap_char_utilities=>cr_lf.
ENDLOOP.
" Create relations
LOOP AT lt_relations INTO DATA(ls_relation).
lv_dot = lv_dot && '"' && lv_class_name && '" -> "' && ls_relation-related_class
&& '" [label="' && ls_relation-relation_type && '"];' && cl_abap_char_utilities=>cr_lf.
ENDLOOP.
" Close the DOT graph
lv_dot = lv_dot && '}';
" Output the DOT source
cl_demo_output=>display( lv_dot ).
Explanation:
-
Reading the Class Source Code: The program starts by reading the source code of the specified class using cl_oo_factory=>create_clif_source.
-
Parsing the Source Code: It then loops through each line of the source code to extract the class name, attributes, methods, comments, and relationships.
-
Comments: Lines starting with "! are treated as comments and associated with the next attribute or method.
-
Sections: The current visibility section (public, protected, private) is tracked to categorize attributes and methods.
-
Attributes: Lines starting with DATA are parsed to extract attribute names, types, and associated comments. If an attribute type refers to another class, a relationship is recorded.
-
Methods: Lines starting with METHODS are parsed to extract method names, parameters, and associated comments.
-
Relations: When an attribute is of a class type (simplified assumption for types starting with 'Z'), a composition or association relationship is established.
-
Generating DOT Format Output:
-
The class diagram is constructed in the DOT language, which is used by Graphviz to generate visual graphs.
-
The main class node includes the class name, its attributes, and methods.
-
Nodes for related classes are also created.
-
Edges (relations) are established between the main class and related classes with appropriate labels indicating the type of relationship (e.g., "Composition", "Association").
-
Displaying the Output: The DOT source code is output using cl_demo_output=>display, which allows you to copy the output and use it with Graphviz or any DOT language viewer to generate the class diagram.
Usage Instructions:
-
Run the Report:
- Execute the report
Z_CLASS_DIAGRAM_GENERATOR in your ABAP environment.
- Enter the name of the class you wish to generate the diagram for (e.g.,
ZCA_NN_NEURON).
-
Copy the DOT Output:
- After execution, the DOT format source code will be displayed.
- Copy the DOT code from the output.
-
Generate the Class Diagram:
- Paste the copied DOT code into an online Graphviz editor like WebGraphviz or GraphvizOnline, or use a local Graphviz installation.
- Render the graph to see the visual class diagram.
Example DOT Output:
digraph G {
"ZCA_NN_NEURON" [shape=record, label="{ZCA_NN_NEURON|layer_number: i\lneuron_index: zca_nn_neuid\lneuron_value: float\lneuron_gradient: float\lgt_weights: tt_weights\l|constructor()\lactivateneuron()\lset_desired()\lcomputegradient()\lcomputegradienthidden()\l}"];
"ZCA_NN_MATH" [shape=record, label="{ZCA_NN_MATH}"];
"ZCA_NN_MATH" [shape=record, label="{ZCA_NN_MATH}"];
"ZCA_NN_MATH" [shape=record, label="{ZCA_NN_MATH}"];
"ZCA_NN_MATH" [shape=record, label="{ZCA_NN_MATH}"];
"ZCA_NN_NEURON" -> "ZCA_NN_MATH" [label="Composition"];
"ZCA_NN_NEURON" -> "ZCA_NN_MATH" [label="Composition"];
"ZCA_NN_NEURON" -> "ZCA_NN_MATH" [label="Composition"];
"ZCA_NN_NEURON" -> "ZCA_NN_MATH" [label="Composition"];
}
Note:
-
This code makes several simplifications for the sake of demonstration:
-
It assumes that any type starting with 'Z' is a custom class (you may need to adjust this logic according to your naming conventions).
-
It does not handle all possible ABAP syntax variations.
-
It focuses on the main class and directly related classes.
-
For more complex parsing or handling of additional ABAP constructs, you may need to extend the parsing logic accordingly.
Conclusion:
This report should help you generate a functional class diagram from the source code of an ABAP class, including comments and relationships. You can extend and customize it further to suit your specific needs and handle more complex scenarios.