NetBeans-SCSNI

5.5-Demonstrate the ability to use live code templates such as automatic generation of constructors, try/catch loops, and getters and setters.

How to use Code Generation dialog

  • 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.
CG_Dialog.png


Code Template Abbreviations: Quick Reference (Java Projects)

You can reduce the number of keystrokes when you are typing Java code by typing abbreviations to generate code from templates. The abbreviation is expanded into the template after you press the Tab key. Code templates also appear when you use code completion while editing Java files.

The IDE comes with a set of code templates. You can also create your own code templates.

Java Files

Abbreviation

Code Template

En

Enumeration

Ex

Exception

Ob

Object

Psf

public static final

Psfb

public static final boolean

Psfi

public static final int

Psfs

public static final String

St

String

ab

abstract

bo

boolean

br

break

ca

catch (

cl

class

cn

continue

df

default:

dowhile

do {
        
                } while (condition);

eq

equals

ex

extends

fa

false

fi

final

fl

float

forc

for (Iterator it = collection.iterator(); it.hasNext();) {
    Object elem = (Object) it.next();
        
                }

fore

for (Object elem : iterable) {
 
                }

fori

for (int i = 0; i < SCRAMBLED_WORD_LIST.length; i++) {
 
                }

fy

finally

ie

interface

ifelse

if (condition) {
 
} else {
 
                }

im

implements

iof

instanceof

ir

import

le

length

newo

Object name = new Object(args);

pe

protected

pr

private

psf

private static final

psfb

private static final boolean

psfi

private static final int

psfs

private static final String

pst

printStackTrace();

psvm

public static void main(String[] args) {
 
                }

pu

public

re

return

serr

System.err.println("|");

sout

System.out.println("|")

st

static

sw

switch (

sy

synchronized

tds

Thread.dumpStack();

th

throws

trycatch

try {
 
} catch (Exception e) {
 
                }

tw

throw

twn

throw new

wh

While (

whilei

    while (it.hasNext()) {
        Object elem = (Object) it.next();
        
                }

 

JSP Files

Abbreviation

Code Template

ag

application.getAttribute("|")

ap

application.putAttribute("|",)

ar

application.removeAttribute("|")

cfgi

config.getInitParameter("|")

jspf

<jsp:forward page="|"/>

jspg

<jsp:getProperty name="|" property="|"/>

jspi

<jsp:include page="|"/>

jspp

<jsp:plugin type="|" code="" codebase="">
                </jsp:plugin>

jsps

<jsp:setProperty name="|" property=""/>

jspu

<jsp:useBean id="|" type=""/>

oup

out.print("|")

oupl

out.println("|")

pcg

pageContext.getAttribute("|")

pcgn

pageContext.getAttributeNamesInScope("|")

pcgs

pageContext.getAttributesScope("|")

pcr

pageContext.removeAttribute("|")

pcs

pageContext.setAttribute("|",)

pg

<%@page |%>

pga

<%@ page autoFlush="false"%>

pgb

<%@ page buffer="|kb"%>

pgc

<%@page contentType="|"%>

pgerr

<%@page errorPage="|"%>

pgex

<%@page extends="|"%>

pgie

<%@page isErrorPage="true"%>

pgim

<%@page import="|"%>

pgin

<%@page info="|"%>

pgit

<%@page isThreadSafe="false"%>

pgl

<%@page language="java"%>

pgs

<%@page session="false"%>

rg

request.getParameter("|")

sg

session.getAttribute("|")

sp

session.setAttribute("|", )

sr

session.removeAttribute("|")

tglb

<%@taglib uri="|"%>




How to use Live Templates

Type a few letters from the name of the template e.g. fo| and invoke the code completion.

LT_InCodeCompletion.png

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.
LT_FieldEditing.png

  • Writing 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. Or see below for a concise description of the template language.
LT_Create.png

Any code template parameter can be specified by its name and a set of optional hints. Hints guide the infrastructure in computing the values assigned to parameters on template expansion. The parameter definition syntax is ${param_name hint=value hint=value ...}

As an exception, boolean hints can be written without the value part: ${param_name hint} translates to ${param_name hint=true}.

The following parameter names are reserved by the infrastructure for handling the caret and selection position:

${cursor} defines a position where the caret will be located after the editing of the code template values finishes.
${selection} defines a position for pasting the content of the editor selection. This is used by so-called 'selection templates' that appear as hints whenever the user selects some text in the editor.

Similarly, some hint names are reserved by the code template infrastructure: General:

${param_name default="value"} defines the parameter's default value.
${param_name editable=false} can be used to disable the user's editing of the parameter.

Java specific:

${param_name instanceof="java.util.Collection"} requires the parameter value to be an instance of the given type.
${param_name array} requires the parameter value to be of an array type (including arrays of primitive data types).
${param_name iterable} requires the parameter value to be of an array type or an instance of "java.lang.Iterable". Can be used in 'for-each' cycles.
${param type="java.util.Iterator"} requires the parameter value to be the given type. The infrastructure will try to use short name Iterator and import java.util.Iterator if possible.
${param_name iterableElementType} requires the parameter value to be the type of the iterable element. Can be used in 'for-each' cycles.
${param_name leftSideType} requires the parameter value to be the type of the expression on the assignment's left side.
${param_name rightSideType} requires the parameter value to be the type of the expression on the assignment's right side.
${param_name cast} defines that the parameter value would be a type cast if necessary.
${param_name newVarName } defines that the parameter value should be a 'fresh' unused variable name in the given context.

As an example, consider the definition of the "iterator type" parameter of the forc standard template: ${IT_TYPE rightSideType type="java.util.Iterator" default="Iterator" editable=false} where:

  • IT_TYPE is the parameter name,
  • rightSideType tells the infrastructure to try to resolve the type of an expression on the right side of an assignment (e.g. java.util.Iterator<java.lang.String>) and use it as the parameter value (with possible autoimporting).
  • If the type cannot be resolved, type="java.util.Iterator" tells the infrastructure to try to resolve the java.util.Iterator type and use it as the parameter value (with possible autoimporting).
  • If the type cannot be resolved, default="Iterator" tells the infrastructure - use string "Iterator" as the parameter value.
  • editable=false tells the infrastructure not to allow a user to change the parameter value.


-- Wagner R. dos Santos