Friday, January 20, 2012

ABAP: Count number of code lines in METHODS

As you know it's pretty easy to count the no. of code lines for Include/Module/Report programs, you have to simply use statement READ REPORT and you are done.
But in case of counting the code lines in class methods it's a bit.

The same can be achieved by following simple steps:

1.     Do a select on TADIR for the class name as obj_name and build the object range with 'I' 'EQ' and low as 'CLAS'.

2.     Find all the class include using FM: SEO_CLASS_GET_METHOD_INCLUDES ( pass clskey = clsname)
e.g CALL FUNCTION 'SEO_CLASS_GET_METHOD_INCLUDES'
           EXPORTING
            clskey                       = is_class_key
          IMPORTING
            includes                     = it_class_incl
          EXCEPTIONS
            _internal_class_not_existing = 1
            OTHERS                       = 2.
        IF sy-subrc <> 0.
          CONTINUE.
        ENDIF.

3.     Build  the sub_name internal table using it_class_incl-cpdkey-cpdname.

4.     Get the source code using FM : SEO_METHOD_GET_SOURCE
e.g  CALL FUNCTION 'SEO_METHOD_GET_SOURCE'
        EXPORTING
          mtdkey                              = is_class_cmp
        IMPORTING
          source_expanded                     = it_class_code
        EXCEPTIONS
          _internal_method_not_existing       = 1
          _internal_class_not_existing        = 2
          version_not_existing                = 3
          inactive_new                        = 4
          inactive_deleted                    = 5
          OTHERS                              = 6.
      IF sy-subrc <> 0.
        CONTINUE.
      ENDIF.

5.     After this you have the source code in lt-class_code and do a COUNT.
e.g       is_result-count LINES( it_class_code ). "initial count
      
LOOP AT it_class_code ASSIGNING .
        
IF  IS INITIAL.
          
DELETE it_class_code.
        
ELSE.
          
IF (1) = '*'.
            
DELETE it_class_code.
          
ENDIF.
        
ENDIF.
      
ENDLOOP.
       is_result
-count_check LINES( it_class_code ). " without comments
      
LOOP AT it_class_code ASSIGNING .
        
FIND '.' IN .
        
IF sy-subrc <> 0.
          
FIND ',' IN .
          
IF sy-subrc <> 0.
            
DELETE it_class_code.
          
ENDIF.
        
ENDIF.
      
ENDLOOP.
  is_result-count_check_final LINES( it_class_code )."final count 

Cheers,
Sandeep 

No comments:

Post a Comment