0% found this document useful (0 votes)
58 views5 pages

Custom ATC Check For Detecting External Local Class Usage 1738517254

The document outlines the steps to create a custom ATC (ABAP Test Cockpit) check in ABAP, including defining a check category, creating a check class, and implementing the necessary methods. It details the structure of the class, including metadata and analysis procedures, to ensure proper validation of local class instantiation. Finally, it emphasizes the use of the ATC check variant to analyze packages or objects and review flagged issues, demonstrating its application with a specific report example.

Uploaded by

snyx5bvwn2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views5 pages

Custom ATC Check For Detecting External Local Class Usage 1738517254

The document outlines the steps to create a custom ATC (ABAP Test Cockpit) check in ABAP, including defining a check category, creating a check class, and implementing the necessary methods. It details the structure of the class, including metadata and analysis procedures, to ensure proper validation of local class instantiation. Finally, it emphasizes the use of the ATC check variant to analyze packages or objects and review flagged issues, demonstrating its application with a specific report example.

Uploaded by

snyx5bvwn2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Author: Nikhil Raut

https://linkedin.com/in/nikhilraut10

Step 1: Right-click on the project and select New > ABAP Repository Object > ATC Check Category,
provide a name and description for the category and activate it.

Step 2: Create a new ATC Check and enter name, description and class name and all the other
details.
Author: Nikhil Raut
https://linkedin.com/in/nikhilraut10

Step 3: Generate the class by clicking on the implementing class in the ATC checks. The class should
implement the IF_CI_ATC_CHECK interface. Create a local class named META_DATA within it,
implementing the IF_CI_ATC_CHECK_META_DATA interface. Define all the required settings in the
META_DATA methods, such as attributes, object types, messages, and descriptions.

CLASS meta_data(local helper class of class zcl_atc_local_class_obj_check):


CLASS meta_data DEFINITION FINAL.
PUBLIC SECTION.
INTERFACES if_ci_atc_check_meta_data.

METHODS constructor
IMPORTING check_local_class_usage TYPE abap_bool.
DATA v_check_local_class_usage TYPE abap_bool.
ENDCLASS.

CLASS meta_data IMPLEMENTATION.

METHOD constructor.

ENDMETHOD.
METHOD if_ci_atc_check_meta_data~get_attributes.
RETURN VALUE #(
( name = `CheckLocalClass` kind =
if_ci_atc_check_meta_data=>attribute_kinds-boolean
value = REF #( v_check_local_class_usage ) )
).
ENDMETHOD.

METHOD if_ci_atc_check_meta_data~get_checked_object_types.
RETURN VALUE #( ( 'PROG' ) ( 'FUGR' ) ( 'CLAS' ) ).
ENDMETHOD.

METHOD if_ci_atc_check_meta_data~get_description.
RETURN 'External local class has been instantiated'.
ENDMETHOD.

METHOD if_ci_atc_check_meta_data~get_finding_code_infos.
RETURN VALUE #(
( code = 'CREATE' severity = if_ci_atc_check=>finding_severities-
error
text = 'instantiating local class outside its source program is
not allowed' )
Author: Nikhil Raut
https://linkedin.com/in/nikhilraut10

).
ENDMETHOD.

METHOD if_ci_atc_check_meta_data~get_quickfix_code_infos.
RETURN VALUE #(
( code = 'CREATE' short_text = 'Create a global class with the same
logic' )
).
ENDMETHOD.

METHOD if_ci_atc_check_meta_data~is_remote_enabled.
RETURN abap_true.
ENDMETHOD.

METHOD if_ci_atc_check_meta_data~uses_checksums.
RETURN abap_true.
ENDMETHOD.

ENDCLASS.

Step 4: Define all the required custom checks in the global class. The RUN method will be triggered
to validate the statements in the procedure.
CLASS zcl_atc_local_class_obj_check DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
INTERFACES: if_ci_atc_check.
METHODS: constructor.
PROTECTED SECTION.
PRIVATE SECTION.
METHODS: analyze_procedure
IMPORTING procedure TYPE
if_ci_atc_source_code_provider=>ty_procedure
RETURNING VALUE(findings) TYPE if_ci_atc_check=>ty_findings.
DATA: code_provider TYPE REF TO
if_ci_atc_source_code_provider,
assistant_factory TYPE REF TO
cl_ci_atc_assistant_factory,
v_check_local_class_usage TYPE abap_bool.
ENDCLASS.

CLASS ZCL_ATC_LOCAL_CLASS_OBJ_CHECK IMPLEMENTATION.

METHOD analyze_procedure.
LOOP AT procedure-statements USING KEY keyword INTO DATA(ls_statement)
WHERE keyword = 'CREATE'.
DATA(statement_idx) = sy-tabix.
CHECK line_exists( ls_statement-tokens[ lexeme = 'OBJECT' ] ).
LOOP AT ls_statement-tokens INTO DATA(ls_token)
WHERE lexeme CP '*\PROGRAM=*\CLASS=*'.
INSERT VALUE #(
code = 'CREATE'
location = code_provider->get_statement_location( ls_statement )
Author: Nikhil Raut
https://linkedin.com/in/nikhilraut10

checksum = code_provider->get_statement_checksum( ls_statement )


details = assistant_factory->create_finding_details( )-
>attach_quickfixes(
assistant_factory->create_quickfixes( ) )
) INTO TABLE findings.

ENDLOOP.

ENDLOOP.
ENDMETHOD.

METHOD constructor.
v_check_local_class_usage = abap_true.
ENDMETHOD.

METHOD if_ci_atc_check~get_meta_data.
RETURN NEW meta_data( check_local_class_usage =
v_check_local_class_usage ).
ENDMETHOD.

METHOD if_ci_atc_check~run.
code_provider = data_provider->get_code_provider( ).
DATA(procedures) = code_provider->get_procedures( code_provider-
>object_to_comp_unit( object ) ).
LOOP AT procedures->* ASSIGNING FIELD-SYMBOL(<procedure>).
INSERT LINES OF analyze_procedure( <procedure> ) INTO TABLE findings.
ENDLOOP.
ENDMETHOD.

METHOD if_ci_atc_check~set_assistant_factory.
assistant_factory = factory.
ENDMETHOD.

METHOD if_ci_atc_check~set_attributes ##NEEDED.


DATA(check_local_class) = attributes[ name = `CheckLocalClass` ]-value.
v_check_local_class_usage = check_local_class->*.

ENDMETHOD.

METHOD if_ci_atc_check~verify_prerequisites.

ENDMETHOD.
ENDCLASS.
Author: Nikhil Raut
https://linkedin.com/in/nikhilraut10

Step 5: Create a check variant and include your custom ATC check in it. Adjust parameters as
needed.

Results: Use the ATC Check Variant to analyse packages or objects in ADT and review flagged issues.
The below report ZR_TEST contains a statement to instantiate an external local class, our custom
ATC check flagged this issue.

You might also like