In recent releases, SAP allows you to develop ABAP programs and classes using the popular Eclipse IDE. Eclipse provides a number of useful features. We can work with a number of systems using Eclipse. Eclipse also provides a number of useful templates that can be modified according to our needs. It also includes code completion features in order to work faster. In this chapter, we look at all these features in detail.
One of the main reasons we use Eclipse instead of the ABAP Editor is the ease of code completion and shortcuts that are not provided in the ABAP Editor. These allow developers to code programs faster.
Configuring the ABAP development environment with ABAP Development Tools (ADT) in Eclipse
Creating your first ABAP project
Using Eclipse to code unit test classes
Using the Quick Fix feature of Eclipse with ABAP Objects
Installing Eclipse and Loading ABAP Perspective
In this section, we see how to install Eclipse and the ABAP Development tools (ADT) in order to develop ABAP classes and methods. Eclipse allows you to create programs (and local classes) as well as global classes (and unit test classes).
Before working with Eclipse, you need to download and install Eclipse from www.eclipse.org . Once Eclipse is installed, launch it using the desktop shortcut or via the Program list.
Within the dialog box that appears, place the URL relevant to the current version. For example, you use https://tools.hana.ondemand.com/oxygen for Eclipse Oxygen. Then press Enter in order to see the available options.
Select ABAP Development Tools for SAP NetWeaver and choose Next. On the next screen, you will be shown the features that will be installed. Choose Next. Accept the license agreement and click Finish in order to start the installation. This will install ABAP Development tools (ADT) on Eclipse.
- 1.
Close any Welcome page that appears. Choose the menu path Window ➤ Open Perspective ➤ Other. Alternately, you may click the button on the top-right corner, as shown in Figure 8-2.
- 2.
Choose ABAP from the list and click Open. This will load the ABAP perspective.
It is a good idea to familiarize yourself with the layout of the tools within the Eclipse IDE. The ABAP perspective has various tools that are arranged in order for you to work easily. You are free to change positions of views in order to suit your liking.
- 3.
Go to the Project Explorer. The various sections in the Eclipse IDE (for the ABAP perspective) are shown in Figure 8-4.
On the top-left, we have the list of project (and connection) nodes that list the various objects in that system. Below the projects is the outline window (showing the main structure and elements) of the selected object. On the right are the main editors for writing code for classes and programs, and under Editors is the window showing miscellaneous messages such as errors, warnings, and the program output in some cases.
Creating an ABAP Project in Eclipse
Within Eclipse, we need to have an ABAP project corresponding to SAP systems connections. Follow these steps:
Here you can either create a new system connection or select an existing one from the list. We assume that the connection is already there on our SAP Logon pad. From the list of backend systems, select the system you want to connect to and choose Next. The connection settings will appear.
On the Connection Settings screen, accept the default settings and click the Next button.
This will then create the ABAP project, which is in the left pane. (It now corresponds to a connection to the backend SAP system of our choice.)
Creating a Global Class in Eclipse
In this section, we learn how to create a global class using Eclipse (same as the SE24 transaction). The required steps are described next.
Enter a suitable name and description for the class and superclass (if applicable). Then click Next. On the next screen, click the Finish button.
Here we can write the additional code we need to specify within the class. These may include public or private (or protected) methods, attributes, and events. We will add a private attribute called NUM with a type of integer. We will also add an instance constructor method along with an instance method called DISP_NUM.
Here we see the components of the class we are in. (In our case it is the NUM attribute and the DISP_NUM method .) We can choose which component is used by double-clicking the relevant item in the list.
This class may be used globally by all classes in the system and may also be tested via transaction code SE24.
We may click the relevant tab in order to display the code editor at the top part of the screen and insert the appropriate code. Since we have used the global classes editor for our current example, these tabs have not been used.
Creating a Local Class in Eclipse
Right-click your project and choose New ➤ ABAP Program. This will create an ABAP program.
You can then type the definition and implementation code of the given class into the program (see Figure 8-17).
Both the definition and implementation parts are automatically adjusted.
We can also make the first_test method protected or public, using the appropriate options. Currently it is private, so the private option is not shown. We can also delete the method. This will delete it from the definition and implementation.
Note that a new option has been added called Add Implementation.
The cursor will jump inside the newly created method, and we can start writing code immediately.
Method Wizard in Eclipse
We have an embedded form-based wizard within the Eclipse editor for quick creation of class methods along with its signature. In this section, we see how this tool may be used.
Suppose you are in the editor and have already created an ABAP class and are currently within a class method. You now need to define another method that will be called from the method you are currently in.
One way of doing this is to go to the definition of the class and add the new method and its signature. Then implement the method code and come back to the calling point (of the new method) within the current method.
Another quick shortcut is to define the new method by entering the name of the new method in the code and calling the Quick Fix feature .
- 1.
Enter the name METHOD2 followed by parentheses at the point of calling the new method.
- 2.Press CTRL+1 upon placing the cursor on the method name. This will display the popup shown in Figure 8-22.
- 3.Double-click the Create method method2 line shown in the popup. A Method Signature popup will appear, as shown in Figure 8-23.
- 4.
This will display a wizard that defines the signature of the new method we like to define. Here we specify the method details, such as visibility, access, and name.
- 5.
We also specify the method parameters using the table control provided. We can add and remove method parameters using the Add and Remove buttons, respectively. We specify if we need importing, exporting, changing, or returning parameters. The type of the relevant parameters can be specified using the given value help.
- 6.When you are done, click the Finish button. You will see that the wizard automatically generates the new method’s declaration along with the necessary parameters (see Figure 8-24).
An empty implementation of Method2 will also be created. We can now proceed within the code of the new method in the implementation.
Unit Test Templates Using Eclipse
Eclipse offers several useful templates for fast and easy code completion. Using and being aware of such templates is essential for ABAP developers. These are also applicable to unit test programming and are very easy for practicing Test Driven Development (TDD). We can code tests and then design global classes by generating blank methods via the CTRL+1 Quick Fix feature from the test class.
Simply place the cursor on test in the program and press CTRL+SPACE. This will display the popup shown in Figure 8-26.
As you see, Eclipse has inserted the basic coding for our test class. The template for the ABAP test class has been used. It contains a class definition having the “for testing” addition. By default, the risk level is harmless, and the duration is specified as short. The name is ltcl_ and the developer can change it. It contains a FIRST_TEST method, which calls the fail method of the CL_ABAP_UNIT_ASSERT class.
For global classes, things are even better. As mentioned, when editing global classes within Eclipse, we have a number of tabs at the bottom of screen. This is very useful when used in context of unit tests. These tabs allow the developer to easily see which section belongs to global classes (production code), test classes, etc. They are a really quick and easy way to find out where to write code for each section.
To create a new unit test class, click the Test Classes tab, type test in the Test Class view, and press CTRL+SPACE to see the suggestions. Choose testClass – Test class (ABAP Unit), and the test class will be generated. It can be later modified.
For testing and writing assertions, you may simply initialize variables and then code the method in which test and assertions are written. No need to remember the assertion syntax. Simply type assert and use CTRL+SPACE. Choose the assertEquals template shown on display.
Modifying Templates
As mentioned earlier in this chapter, Eclipse provides a number of templates for code completion. Templates are very powerful and make the coding process very fast. The shortcuts CTRL+SPACE and CTRL+1 for code generation make coding faster than when using SE80.
You may create new templates or change existing ones.
Note
Templates can be used for any ABAP development and not only in development related to ABAP classes.
To change templates, follow the menu path Window ➤ Preferences.
Under the node ABAP development on the left pane, choose Source -> Templates. On the right side, you will see the various templates available. There are buttons for editing an existing template and the New button to create a new template.
Each template is denoted by a name, which may be called from within a program.
These templates belong to the General, ABAP, ABAP Objects, or Web Dynpro categories (or contexts). The existing ABAP object-related templates are assertEquals, lif, lcl, and testClass.
When you are coding the program, simply type the entire name of the template or part of it. Then use the code completion technique CTRL+SPACE or the CRL+1 shortcut to see proposals with code completion.
Here, we change the pattern of the code for the assertEquals method . Then click OK. This will take us back to the previous screen. Make sure to click the Apply button.
Summary
In this chapter, you configured the ABAP development environment with ABAP Development Tools (ADT) in Eclipse, created your first ABAP project, used Eclipse to code unit test classes, and saw Eclipse’s Quick Fix feature used in conjunction with ABAP Objects.