© Rehan Zaidi 2019
R. ZaidiSAP ABAP Objectshttps://doi.org/10.1007/978-1-4842-4964-2_8

8. Creating ABAP Classes Using Eclipse

Rehan Zaidi1 
(1)
Dubai, United Arab Emirates
 

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.

Here are some of the topics this chapter addresses:
  • 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.

We can now install the ABAP Development Tools for SAP NetWeaver. From the menu bar, choose Help ➤ Install New Software. The dialog box in Figure 8-1 will appear.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig1_HTML.jpg
Figure 8-1

Install dialog

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.

Once ADT is installed, we need to open the ABAP perspective. Follow these steps to do so:
  1. 1.

    Close any Welcome page that appears. Choose the menu path Window ➤ Open Perspective ➤ Other. Alternately, you may click the ../images/478428_1_En_8_Chapter/478428_1_En_8_Figa_HTML.jpg button on the top-right corner, as shown in Figure 8-2.

     
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig2_HTML.jpg
Figure 8-2

Open perspective

This will display the dialog box shown in Figure 8-3.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig3_HTML.jpg
Figure 8-3

Perspective lists

  1. 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.

     
  2. 3.

    Go to the Project Explorer. The various sections in the Eclipse IDE (for the ABAP perspective) are shown in Figure 8-4.

     
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig4_HTML.jpg
Figure 8-4

ABAP perspective sections

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:

Choose the menu path File ➤ New ➤ ABAP Project. The dialog box shown in Figure 8-5 will appear.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig5_HTML.jpg
Figure 8-5

Backend systems

Choose the ABAP project under the ABAP node and click Next. The screen shown in Figure 8-6 appears.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig6_HTML.jpg
Figure 8-6

System connection list

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.

On the screen that now appears (see Figure 8-7), enter the login credentials and click the Finish button.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig7_HTML.jpg
Figure 8-7

Logon credentials

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.)

All our development objects will be created within the applicable projects. See Figure 8-8.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig8_HTML.jpg
Figure 8-8

ABAP project

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.

First we select the project node we created earlier. Right-click to display the context menu. From the menu, choose the New ➤ ABAP class option. The dialog box in Figure 8-9 appears.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig9_HTML.jpg
Figure 8-9

Dialog for the new ABAP class

Enter a suitable name and description for the class and superclass (if applicable). Then click Next. On the next screen, click the Finish button.

This will generate the basic coding of the global class, as shown in Figure 8-10.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig10_HTML.jpg
Figure 8-10

Basic generated code

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.

Eclipse provides syntax help when you type incorrectly. Consider for example if we typed “imporing” instead of “importing”. The error is shown as a red cross (see Figure 8-11). Keeping the cursor on it explains the error. These errors are shown during typing (i.e., before compilation).
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig11_HTML.jpg
Figure 8-11

Syntax error

Moreover, Eclipse makes it easy and quick to work with attributes and methods by providing autocomplete features. For example, consider a situation where we are in the code of a class constructor. If we type the self-referencing variable me followed by the -> operator, the popup appears, as shown in Figure 8-12.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig12_HTML.jpg
Figure 8-12

Help popup

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.

Another useful feature is the outline window, where the structure of the class is shown. For the class that we are developing, the outline shows the attribute NUM, and the methods constructor and DISP_NUM, as shown in Figure 8-13.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig13_HTML.jpg
Figure 8-13

Class outline

The complete code that we wrote for our example is shown here. This code is written in the code editor for the global class.
class ZST19_MY_CLASS definition public final create public.
 public section.
  methods disp_num .
  methods constructor importing num type i.
 protected section.
 private section.
  data num type i.
ENDCLASS.
CLASS ZST19_MY_CLASS IMPLEMENTATION.
  method disp_num.
     write : num .
  endmethod.
  method constructor.
     me->num = num.
  endmethod.
ENDCLASS.
Once you have created your class, click the ../images/478428_1_En_8_Chapter/478428_1_En_8_Figb_HTML.jpg button on the toolbar. This will activate the class and a message will appear, as shown in Figure 8-14.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig14_HTML.jpg
Figure 8-14

Activation message

We created a simple class that will contain the NUM private attribute with an integer type. This can be viewed using the transaction SE24 via SAP GUI, as shown in Figure 8-15.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig15_HTML.jpg
Figure 8-15

SE24 transaction

This class may be used globally by all classes in the system and may also be tested via transaction code SE24.

In addition to the global class, a number of other useful tabs exist within the Eclipse IDE at the bottom of the screen. These include the class-relevant local types, local types, test classes, and macros (see Figure 8-16).
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig16_HTML.jpg
Figure 8-16

Class-relevant tabs

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

To create a local class, we need to first create an ABAP program within which the local class will be created. This will create local classes as programs created via transaction SE38 using SAP GUI. Follow these steps to create a local class:
  • 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).

../images/478428_1_En_8_Chapter/478428_1_En_8_Fig17_HTML.jpg
Figure 8-17

Local class code

A number of features are available for coding with local classes in Eclipse, especially for working with methods. In order to see the options that are available for a particular method, place the cursor on the method name and press CTRL+1. The options are shown in Figure 8-18.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig18_HTML.jpg
Figure 8-18

Options

If you need to rename a method, simply choose the Rename first_test option. This will put the method name in a box (see Figure 8-19). As you change the name in the definition, you will see that the name changes accordingly in the implementation.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig19_HTML.jpg
Figure 8-19

Renaming a method

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.

Suppose we have a method called second_method, which only exists in the definition and has not yet been implemented. Place the cursor on the method name in the definition part and press CTRL+1. The options will now appear as shown in Figure 8-20.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig20_HTML.jpg
Figure 8-20

Add implementation option

Note that a new option has been added called Add Implementation.

Choosing this option will add an implementation to the class in which we add our method code (see Figure 8-21).
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig21_HTML.jpg
Figure 8-21

Second method 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 .

In the example, we have a method called METHOD1 . We can create a method called METHOD2 by following these steps:
  1. 1.

    Enter the name METHOD2 followed by parentheses at the point of calling the new method.

     
  2. 2.
    Press CTRL+1 upon placing the cursor on the method name. This will display the popup shown in Figure 8-22.
    ../images/478428_1_En_8_Chapter/478428_1_En_8_Fig22_HTML.jpg
    Figure 8-22

    Quick Fix in Eclipse

     
  3. 3.
    Double-click the Create method method2 line shown in the popup. A Method Signature popup will appear, as shown in Figure 8-23.
    ../images/478428_1_En_8_Chapter/478428_1_En_8_Fig23_HTML.jpg
    Figure 8-23

    Defining method signature

     
  4. 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. 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. 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).
    ../images/478428_1_En_8_Chapter/478428_1_En_8_Fig24_HTML.jpg
    Figure 8-24

    Definition of Method2

     
Moreover, the call of Method2 within Method1 is also added with the parameters we specified from the Method Creation wizard (see Figure 8-25).
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig25_HTML.jpg
Figure 8-25

Method call

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.

Let’s see an example where we have an ABAP program in which we want to create a local unit test class. Within the program, we need to type test or testClass and then call the template for Quick Fix.
  • Simply place the cursor on test in the program and press CTRL+SPACE. This will display the popup shown in Figure 8-26.

../images/478428_1_En_8_Chapter/478428_1_En_8_Fig26_HTML.jpg
Figure 8-26

Quick Fix for testClass

Double-click the test class (ABAP unit) option. You will see that the code for the unit class is inserted automatically, as shown in Figure 8-27.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig27_HTML.jpg
Figure 8-27

Inserted unit class code

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.

The dialog box shown in Figure 8-28 appears.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig28_HTML.jpg
Figure 8-28

Templates

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.

You can also change the existing templates. For example, by default, the assertion is written on one line, but you can update the template to have each parameter shown on a new line. Simply select the template and click the Edit button, as shown in Figure 8-29.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig29_HTML.jpg
Figure 8-29

Edit option

The popup appears, as shown in Figure 8-30.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig30_HTML.jpg
Figure 8-30

Editing method call

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.

Once this is done, the CTRL+SPACE option will show us the method call, as shown in Figure 8-31.
../images/478428_1_En_8_Chapter/478428_1_En_8_Fig31_HTML.jpg
Figure 8-31

Method call

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.239.182