|
|
Eclipse Tutorial |
||
|
Part 3: Useful Programming Functions |
|||
Part 3: Useful Programming Functions
"Any sufficiently advanced technology is indistinguishable from magic." Arthur C. Clarke.
So far, the basic working concepts of the Eclipse development environment have been exposed. Now, Eclipse programming assist functions should be introduced. Here is where you will realise that using Eclipse for Java programming allows saving huge amounts of time. While the preceding tutorial parts are necessary, this is the most interesting one.
Compiling and Error Detection
It is important to take into account that in Eclipse, compilation errors are shown in real time underlining the appropriate code fragment with a red mark. And also the environment automatically compiles every saved code. So, you won't have to go through the annoying and slow process of compiling - watching errors - correcting errors.
Errors can be easily found because they are shown as red marks at the right margin of the Java code editor. Also, errors and warnings present in already saved files are shown at the "Tasks View", as it will be detailed later. Clicking on any of both error marks will automatically take you to the line in which the error is present. Compilation warnings are also shown using yellow marks.

Light Bulb Icon = Autocorrect
We have already seen how Eclipse detects and marks every compilation error and warning. Eclipse usually lets you autocorrect the possible mistakes just by clicking a light bulb icon present at the left margin of the code editor. Then, a window showing all the options will appear. Selecting an option with the keyboard cursors or hovering the mouse pointer over such option will open a new window in which a detailed representation of the code modifications added will be shown. Just click on the selected option (or press ENTER) to let Eclipse perform the automated correction.

CTRL + Space = Autocomplete
Once you know this useful autocompletion feature, you will be using it all the time. Through the following practical examples you will learn the common and various scenarios in which this "code assist" function will be very useful.
Creating references to other classes inside the current class is a normal task. Nevertheless, some Java classes have very long names that are difficult to remember. Also, import declarations should be added in order to resolve these references at compilation time.
Using "CTRL + Space" after typing the very first characters of a Java class name will show you the possible alternatives. You can select any of them just by left clicking on it. Notice that the corresponding import statement will be automatically added. Classes are marked with a green "C" while interfaces are marked with a purple "I". The original class package is also shown, in order to avoid possible confusions.

When defining a class it is normal to give your own user defined names to the class attributes and to inner method variables. But it may be difficult to remember the exact name. After typing the first characters of the attribute or the variable name, pressing "CTRL + Space" will show up the possible alternatives. This is similar to the class name autocompletion exposed before. Notice that local variables are marked with a grey "L" icon, while attributes are marked with an icon which may vary according to the attribute visibility.

Once that you have created a Java object, you can invoke different methods of such class. Nevertheless, it is quite frequent to forget a particular method name or even the exact parameter types and order. This problem is also solved by pressing "CTRL + Space" after typing the object name and a "." which will show up a window with the possible alternatives. Clicking the chosen alternative will automatically add the method signature to your object.

Also the constructor signature may be autocompleted by pressing "CTRL + Space" after typing the class name and "(".

Typing the first letters of a method signature modifier such as "public" or "private" and pressing "CTRL + Space" will let you automatically create a method stub. Pressing the "TAB" key will let you to jump from a stub field to another, in order to let you specify the return type, name and parameters.

Loops may be present in nearly every program. Besides creating a new loop may not be a difficult task, Eclipse autocompletion functions will considerably boost the process. Just type "do", "while" or "for" and then press "CTRL + Space" to show up the possible options. If you have created your loop to iterate over an array, selecting this option will even try to autocomplete the array name.

While programmer internal comments are indicated by a "//", Javadoc comments are initiated by a "/**" symbol. After creating a method, adding "/** + ENTER" before the method signature will autocomplete the method Javadoc fields such as "@param [parameterName] [comment]", "@return [returnedDataDescription]" and "@throws [exceptionType] [comment]". Pressing "CTRL + Space" inside a "/** ... */" block will show up every Javadoc tag.

Source Menu
Right clicking on the code editor will show up the contextual menu. The most important functions of the "Source >" submenu are the following:
By selecting this option, you can quickly comment and uncomment code. This is specially useful used with blocks of code, as it allows "temporally removing from execution" specific parts from your program. The hotkeys associated are "CTRL + /" to comment and "CTRL + \" to uncomment.
In Eclipse 3.0 these commands have been substituted by "Toggle Comment". By selecting this new command you can turn commented code into uncommented code and viceversa. Also, "Source > Add Block Comment" will surround the selected code with "/*" and "*/" symbols. Selecting block commented code will cause the "Source > Remove Block Comment" option to show up. Choosing this option you can remove block comment marks from a program block.
The code formatting function automatically performs indentation and other visual code representation functions. It is a quick way of achieving an ordered and easily understandable code presentation. You can tailor the code formatting function to your personal preferences in "Window > Preferences > Java > Code Formatter". The format hotkeys are "CTRL + SHIFT + F".
Notice that the code formatter indentation functions help to identify which pieces of code are affected by an if condition or a for loop, for example. But also, it is useful to place the cursor after a parenthesis, because it will remark the associated opening or closing parenthesis with a grey square. So, you will find out at a glance which code is placed between two parentheses.

Eclipse 3.0 allows specifying advanced code formatting options. The code formatting preferences page has been moved to "Window > Preferences > Java > Code Style > Code Formatter". Besides you can tailor the code formatting options according to your preferences, the default configuration formats code following Java conventions.
Besides the appropriate imports are always added when using code assist functions to autocomplete the name of a Java class, new imports can be added anytime using "Add Import". Also, just by clicking "Organise Imports", only the unused import statements will be removed, which is an efficient practice. The related keyboard shortcut is "CTRL + SHIFT + O".
![]()
Clicking the "Override/Implement Methods" option will open a menu window. Just by checking the desired methods, the signature of these selected methods from the superclass will be added.

Also, "Add Constructors from Superclass" will allow you to specialise all the constructors used.
Java allows specifying different levels of attribute visibility. Nevertheless, in object oriented programming the class internal attributes should always be private attributes. This means that no direct attribute modification could be performed from an external class. Because of that, the only way to access an attribute, either to read its value or to set a new value, should be using getter and setter methods. By clicking "Generate Getter and Setter" a window showing the possible methods that could be created according to the attributes defined. Then, the needed methods can be selected just by checking them. Pressing "Ok" will create the complete chosen methods.


To use this option, a piece of code should have been already selected by left clicking (or holding SHIFT) and dragging. Then, activating this option will create a try block around the selected code. And after this try block, the appropriate catch blocks, catching every type of possibly thrown exceptions, will be created. By default, a trace sentence is added in those catch blocks, allowing you to immediately identify where the exception was thrown.

Notice also that, when an exception is not caught, it will appear as a red text (standard error output) in the console view. Clicking the line of code in which the exception took place will directly lead you to that point of the program in the code editor.

Refactor Menu
Right clicking on the code editor will show up the contextual menu. These are the most interesting functions of the "Refactor >" sub menu:
In order to invoke the renaming function, an element should have been previously selected. Checking the "update references" option, every reference to the renamed element will also be updated. Notice that using the "Refactor > Rename..." option is the appropriate way of renaming elements such as ".java" files. As refactor updates the appropriate references, no compilation problems will be found. Notice that when renaming certain elements, you will be able to update also the references in Javadoc comments, comments and string literals, which can also be very useful. The "Preview" option lets you make sure that no errors will be present after the renaming operation.

Before selecting the "Refactor > Move..." option, the source file or element should have been previously selected. Then, it is only necessary to select the target destination in order to perform the move operation. Remember that this is the right way of moving files without having any problem with references and compilation paths.
To modify a method signature you can also use this refactoring option rather than changing it manually. You only have to place the cursor inside the method whose signature you want to modify. This may be a quicker way of changing visibility, return type, parameters and parameter order. Add new parameters by pressing the "Add" button and modify them using the "Edit" button.


If your class extends or inherits another class, you may want to move some elements to its superclass (pull up) or subclass (push down) respectively. Selecting the element and the appropriate option will automatically perform this operation.
Checking Documentation
Your code Javadoc documentation can be checked in real time just by placing your cursor over the selected element. To expand this documentation window, just press the "F2" function key.

External Javadoc documentation can be checked by right clicking on the appropriate project in the Package Explorer or Navigator views, selecting "Window > Preferences > Installed JRE > Edit" and specifying the Javadoc location. These Javadoc documents should have been previously downloaded (i.e., from the Java Sun webpage) or created by yourself (using the "Project > Generate Javadoc ..." option). Then, you can instantly open the external Javadoc just pressing "SHIFT + F2".
In Eclipse 3.0 there is a new Javadoc view ("Window > Show View... > Java > Javadoc"). Place the cursor over a Java element and this Javadoc view will show you its Javadoc documentation.

Importing JAR Files
Sometimes you may need some Jar files to compile your project, which may not be included on the standard JRE. Right click the appropriate project folder, choose "Properties > Java Build Path", select the "Libraries" tab, press the "Add External Jars" button and select the ".jar" or ".zip" file. The new Jar added will be visible in your package explorer view.

|
Part 3: Useful Programming Functions |
This site is optimised for a 1024x768 screen resolution.
Creado por Enrique Serrano Valle
Los contenidos de este sitio se encuentran bajo la licencia Creative Commons Attribution 2.0.
Eclipse trademark and logos appear according to Eclipse legal conditions. For more information check
http://www.eclipse.org/legal/main.htmlJava is a registered trademark of Sun Microsystems Inc.