Wednesday, August 20, 2008

Netbeans 6.0 - Code generation


The Java editor is capable of generating often used constructs for you automatically. Press Alt+Insert to invoke the code generation menu and pick what you want to generate

To add import statements: Use error hints: Click on the error mark with the light bulb or press Alt+Enter. It will offer you a list of possible classes to import. Pick the proper hint and you are done

Use the import class dialog: Put the caret into the name of an unimported class and press Alt+Shift+I. A list of possible classes to import will appear. Use the smart fix import: Press Ctrl+Shift+I. You will get a dialog that lists all unresolved identifiers in the source. If there is more than one option for resolving the identifier you may choose using the combo box. Classes shown in gray do not fit for some reason.

Method exit points: Putting the caret on the return type of a method definition will highlight all places where the method can exit

Exception throwing points: Putting the caret on an exception in a method declaration throws clause will highlight all the places where that exception can be thrown

If you want to surround a piece of text with a template, just select the text and invoke hints with Alt+Enter

Sometimes it is useful to select text based on Java syntax. To do this, press Alt+Shift+PERIOD (Ctrl-Shift-PERIOD on a Mac) to expand the selection and Alt+Shift+COMMA (Ctrl-Shift-COMMA on a Mac) to shrink the selection based on the structure of your Java source code

How to use Live Templates

Type a few letters from the name of the template e.g. fo| and invoke the code completion.
Pick the template you want to use. If there is a suitable collection it will be filled in for you. Several parts of the template will be rendered in blue. You may cycle between them using the Tab key. Editing the fields will change the part of the code accordingly. Press Enter or Esc to finish editing the template. Notice that if you move out of the blue boxes with the cursor the "edit template mode" continues (so you may get back using the Tab key) until you actually change text outside of the blue box.

Creating a new template You can define new templates: go to Menu->Tools->Options, then choose the Editor category and the Code Templates tab. You may want to look at the existing Java templates and try to create your own

Javadoc popup window Put the caret on a symbol of choice and press Ctrl+Shift+Space. Javadoc view window Go to Window->Javadoc View in the main menu. This will open a separate window which always shows Javadoc for the element under the caret

Opening a type in the editor If you know the name of the type (class, interface, annotation or enum) you want to open, press Alt+Shift+O and type the name into the dialog. Notice that you can use prefix, camel case and wild cards

Navigating to source or declaration Place the caret on a type or variable name and press Alt+O or Alt+G. The former will open the source declaration of the type in the editor; the latter will take you to the declaration (e.g. to the place where a variable is defined.) Hyperlinking A similar action can be achieved by holding down the Ctrl key and clicking on an element with the mouse.

Implements/Overrides annotation Methods which implement or override another method are marked with a glyph in the editor gutter. Clicking the icon will take you to the overridden method or to the method from the interface. Pressing Ctrl+B does the same.

Declaration Window If you want to see the declaration of a given element but you don't want to open it in the editor, you can open the Declaration View, which always shows the declaration of the element under the caret

No comments:

Post a Comment