Recipe 1.9 Completing Code Automatically


1.9.1 Problem

While entering code, you forgot the name of either a method you wanted to call or some of a method's parameters.

1.9.2 Solution

Use Eclipse's code assist (also called content assist ) to help out. When you enter the name of an object or class in the JDT code editor followed by a period (.) and then pause, code assist displays the members of that object or class, and you can select the one you want. You also can bring up code assist at any time (e.g., when you've positioned the cursor inside a method's parentheses, and you want to see what arguments that method takes) by pressing Ctrl-Space or by selecting Edit Content Assist.

1.9.3 Discussion

Code (or content) assist is one of the good things about using a full Java IDE. It's an invaluable tool that accelerates development, and it's a handy resource that you'll probably find yourself relying on in time. In the code example we've been developing over the previous few recipes, enter the following code to display some text:

 public class FirstApp {   public static void main(String[] args)   {  System.out.println("Stay cool.");  } } 

To work with code assist, enter System . in the main method of the FirstApp project, then pause. Code assist displays the classes and methods in the System namespace, as shown in Figure 1-11.

Figure 1-11. Using code assist

Double-click out in the code assist list so that code assist inserts that member into your code, insert a period so that the phrase now reads System.out ., and pause again. Code assist now displays the methods of the out class. Double-click the code assist suggestion println(String arg0) , and code assist inserts the following code into the main method:

 public class FirstApp {   public static void main(String[] args)   {  System.out.println( )  } } 

Edit this to add the text Stay cool . . Note that code assist adds the closing quotation mark automatically as you type:

 public class FirstApp {   public static void main(String[] args)   {  System.out.println("Stay cool.")  } } 

As soon as you enter this code, Eclipse displays it with a wavy red underline, shown in Figure 1-12, to indicate that a syntax problem exists. Rest the mouse cursor over the new code, and a tool tip appears, also shown in Figure 1-12, indicating that a semicolon is missing. Note also that a red box (displayed in stunning black and white in the figure) appears in the overview bar to the right of the code. Clicking that box jumps to the error, which is handy if you've got a lot of errors and a long code file.

Deprecated methods also are underlined automatically in the JDT editor, but in yellow, not red. Syntax warnings in general are displayed with yellow boxes in the overview bar.

Figure 1-12. A syntax error message

Add that semicolon now to the end of the line to give you the complete code and to make the wavy red line disappear.

Eclipse can format your code automatically, adding indents and cleaning up the source code nicely , which is great if you're pasting code from somewhere else. Just select Source Format, and Eclipse will handle the details. In time, you'll probably find yourself using this feature more often than you expected.

Finally, save the file by clicking the disk icon in the toolbar or by selecting File Save. An unsaved file appears with an asterisk before its name in its editor tab (as shown in Figure 1-12), but the asterisk disappears when the file is saved. If you don't save a code file before trying to compile and run that code, Eclipse will prompt you to do so. We'll run this code in the next recipe.

To sum up, code assist is a great tool for code completion, and it will start automatically when you insert a period (.) in the JDT editor after the name of an object or class. You also can make code assist appear at any time while you're typing code; just press Ctrl-Space or select Edit Content Assist.

You can configure code assist to fit your own coding style. In the left pane of the Preferences dialog select Window Preferences, and then Editor. Then click the Code Assist tab to configure it as you like.

1.9.4 See Also

Recipe 1.10 on running your code; Chapter 1 of Eclipse (O'Reilly).

Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: