ABAP Tools - Work with Eclipse (Refactoring)
Today's article is about working with Eclipse and how you can efficiently and quickly refactor your source code.
Table of contents
A good development tool can be recognized, among other things, by the speed at which new source code can be implemented and how far the tool can help you get information. But the significance of the errors and the connection of additional plugins is also very important.
In today's article we want to take a closer look at the implementation time and how Eclipse can support you to change existing code.
Preparation
Refactoring is about adapting existing source code. Shrinking or splitting methods, renaming variables or methods, everything belongs to refactoring source code. Eclipse supports you in various ways.
For our example we use a small class with the implementation of the classrun interface. We will now work on this example with the various methods of refactoring.
CLASS zcl_bs_test_refactoring DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_bs_test_refactoring IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA:
ld_identifier TYPE zbs_test_data-identifier.
ld_identifier = 'REFACTORING'.
SELECT SINGLE *
FROM zbs_test_data
WHERE identifier = @ld_identifier
INTO @DATA(ls_data).
out->write( 'The found data:' ).
out->write( ls_data ).
ENDMETHOD.
ENDCLASS.
Extract method
The SELECT should be outsourced to a new method so that we can also use it in other places. Usually it is also common to outsource database access to a separate object, in this example we keep everything in one class.
Select the complete SELECT statement and press CTRL + 1 to display the quick fix. Here you can select the entry "Extract method".
After the outsourcing, the new method "new_method" is created and the Select is now in this method. As you have determined, the corresponding interfaces to the method were generated automatically from the context. You can still change the name of the method or just confirm with ENTER. In this case we adapt the exchanged call of the method with an inline declaration, since an explicit definition is not required here.
" After refactoring
DATA ls_data TYPE zbs_test_data.
ls_data = new_method( ld_identifier ).
" After adjustment
DATA(ls_data) = new_method( ld_identifier ).
Change visibility
The method is also required as a public method because other classes should also use it. To do this, you simply had to go to the method definition and you can work again with the quick fix (CTRL + 1). With "Make new_method public" you can set the method directly as public in the class.
Rename
The method name is no longer the right one for our needs, so you can rename it very easily, although there are now at least three places to use it. To do this, go to the name of the method, regardless of whether it is a call or during the definition, and call the Rename popup via quick fix or directly rename (SHIFT + ALT + R).
After the action you will find that the usage in coding has changed everywhere. The renaming is always carried out completely and consistently, so you do not have to be afraid that the code can no longer be activated later.
Hint: If you use class or methods in other programs, they will also be updated and changed. So you should be careful not to change too much at once. However, the changed objects are recorded as normal in transport requests.
Create constant
As you may have noticed, we seem to have a constant value in source code. So that it is not hard-coded in the source code, we will create it as a class constant in the class. The Quick-Fix supports you again when you position the cursor on the literal.
You can then give the newly created class constant a new name and finally confirm the action with ENTER.
Create text-symbol
The outsourcing of literals in text symbols works just as easily as with the constants and with the quick fix, you have all options at hand.
After selection, you can change the text again or set the length of the field before you confirm and create a new text symbol.
Final result
You can find the final result after the refactoring here. A lot has happened in the code and we are now providing a public method that can be used with the class.
CLASS zcl_bs_test_refactoring DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
METHODS select_test_data
IMPORTING
id_identifier TYPE zbs_test_data-identifier
RETURNING
VALUE(rs_data) TYPE zbs_test_data.
PROTECTED SECTION.
PRIVATE SECTION.
CONSTANTS c_main_identifier TYPE string VALUE 'REFACTORING' ##NO_TEXT.
ENDCLASS.
CLASS zcl_bs_test_refactoring IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA:
ld_identifier TYPE zbs_test_data-identifier.
ld_identifier = c_main_identifier.
DATA(ls_data) = select_test_data( ld_identifier ).
out->write( TEXT-001 ).
out->write( ls_data ).
ENDMETHOD.
METHOD select_test_data.
SELECT SINGLE *
FROM zbs_test_data
WHERE identifier = @id_identifier
INTO @rs_data .
ENDMETHOD.
ENDCLASS.
Conclusion
We converted the existing coding with almost no effort. So you can see how well Eclipse can help you to quickly and efficiently adapt and change your existing coding. Imagine you should have made all the changes in SE24, how much more time would it have taken you?