MyEclipse templates Detailed (a)--java template Foundation

Source: Internet
Author: User
Tags iterable

Directory

    • Templates Introduction     
    • MyEclipse comes with templates detailed
    • New template
    • Custom Template

  Because they are more lazy, especially to knock repeated code comparison aversion, so often like to use shortcut keys and templates, MyEclipse template function is actually very powerful, as if they have only used syso this one, so recently learned a bit, the first blog, declined to reprint.

First, Templates Introduction 1.1 Templates Basic Introduction

view edit entry for MyEclipse templates, window->preferences->java->editor->templates

  

A list of all templates appears on the right side of the click:

Now the location of the red is a brief introduction:

    • Name: template names, which can be used to replace the custom code, hereinafter referred to as hot strings , which can be substituted strings.
    • Context: scope, which is used only at the specified scope to replace the custom code, this article discusses only Java-related templates.
    • Auto Insert: Automatic insertion, that is, when the template name unique does not cause ambiguity, by code prompt key (if no modification is alt+/), direct replacement does not need to select, in order to be quick, as far as possible template name to be unique, automatic insert selection, you can easily enter.
    • NEW: This is where the focus of this article is to be used, namely, how to create a new template.
    • Preview: This is the template previews.
    • Use code formatter: templates can be viewed as Java code and formatted for easy viewing.
1.2 Template Basic Syntax

Java templates are relatively simple, because the template syntax is consistent with the Java syntax, and it is relatively difficult to be unfamiliar with the identifiers represented by ${}, which describes the meanings represented by all identifiers.

Identifiers can be easily divided into attribute identifiers, operation identifiers and lookup identifiers three kinds (personal classification, easy to understand).

1.2.1 Property identifiers

A property identifier indicates that the variable represents a fixed property, and some changes depending on the location of the hot string, in the following ways.

Property identifiers
${enclosing_method} Method Name of the location method where the hot string is located.
${enclosing_method_arguments} The parameter name of the method where the hot string is located, if more than one is separated by commas.
${enclosing_package} The package name where the hot string is located.
${enclosing_project} The name of the project where the hot string is located.
${enclosing_type} The name of the class where the hot string is located.
${file} The file name where the hot string resides
${return_type} The return type of the method where the hot string is located
${primary_type_name} Hot string contains a filename without a suffix
${date} Current date, example: 2016-7-10
${time} Current time, Example: 6:00:09
${dollar} Represents a string of $
${user} Current login User Name example: Administrator
${year} Current year Example: 2014
${todo} Todo string style, default Todo

Several of these identifiers represent meanings as shown in.

Note: The difference between ${enclosing_type} and ${primary_type_name}, as shown in

1.2.2 Action Identifier
Action Identifier
${import} When the class is not referenced, refer to this class, for example: ${:import (Java.util.List, Java.util.Collection)}
${importstatic} When the class is not statically referenced, the class is statically referenced, ${:importstatic (Java.util.Collections.EMPTY_SET, ' java.lang.system.* ')}
${cursor} Cursor position: After the hot string is replaced with the specified code, the cursor is positioned first on the first variable, and the cursor is positioned at ${cursor} when you press ENTER.
${line_selection} Represents the selected row, selects rows and then presses SHIFT+ALT+Z to select the specified template for operation, and ${line_selection} in the template expression is replaced with the currently selected row.
${word_selection} Represents the selected string with double quotation marks, presses the prompt key to select the specified template for operation, and ${word_selection} in the template expression is replaced with the currently selected string.
1.2.3 Lookup Identifiers

The lookup identifier finds a variable of the specified range or type. Before that, let's briefly describe the common use of lookup identifiers.

    1. Using a variable: for example, an expression ${id:var} in which the ID is the variable name (Var represents the most recent available variable from the current position), which can be used to represent the same content with the ${id} once again, there is also the benefit of modifying the first one after the synchronization, and pressing the TAB key no longer locates the same variable.
    2. Specifies the type of lookup: for example, Expression ${id:var (int)}, which is used to find a variable of type int, you can specify multiple types at the same time separated by commas, such as ${id:var (int,double), where the first precedence is matched, and if multiple variables can be found at the same time, The use prompt displays a drop-down list for selection.
    3. When you look up a variable, only the variables that are currently accessible are found.
    4. When looking for a variable, the default is to display the closest match to the hot string, and if you do not find the specified variable, the contents of the curly brace are displayed, for example, ${var} is not found, and Var is displayed.
    5. When you do not specify a lookup type, you can use the variable name, and if you specify a lookup type, you must have a variable name , and you cannot find a variable of the base type if you do not specify a type .
Find identifiers
${var} Matches all variable names, including member variables, local variables, and parameters.
${field} Match member variable name
${localvar} Match local variable name or parameter name
${array} Match array name
${array_type} ${array} matches the type of the array
${array_element} An available variable name for the matching array, which needs to be used in conjunction with ${array}
${collection} Match a collection (implements a collection of java.util.Collection interfaces)
${iterable} Match a traversed element, an array, or an object that implements iterate
${iterable_type} ${iterable} matches the element type of the object and, if there is a generic, displays the generic type
${iterable_element} An available variable name for the ${iterable} match to the object
${index} An available index of the following table variable, generally used in the loop body
${elemtype} Get an array or implement the element type of the iterate object, for example: ${t:elemtype (a)} Elem = ${a:array}
${newtype} Get the class name according to the full path name, and reference it if there is no reference, for example: ${type:newtype (Java.util.Iterator)}
${newname} Take a variable name that has the same style and is available, for example: ${index:newname (int)}
${link} Specify the content of the hint, for example: ${kind:link (Empty_set, Empty_list, ' empty_map.entryset () ')}
${exception_variable_name} Variable name to catch exception
${argtype}

Parses the nth generic type of the specified element (template variable), if the referenced variable does not exist or if no argument is resolved to Object,n represents the first few generic variable types

Example: ${type:argtype (vector,0)} ${first:var (type)} = ${vector:var (java.util.Vector)}.get (0); matches the nearest vector takes the No. 0 object

Second, myeclipse self-brought templates detailed

  Perhaps MyEclipse's own templates is not commonly used, but by analyzing these templates you can deepen your understanding of identifiers and write templates on demand later on. Here is a brief introduction to several template templates.

2.1 Arrayadd, add an element to the array in the 2.1.1 template
New ${array_type}[${array}.length + 10, ${result}, 0, ${array}.length); ${result}[${array}. Length]= ${var};
2.1.2 Example

2.1.3 Detailed

  Based on the most recent array, another array of the same type is created, the array name has the same style, and the length is one more than the original array, and the last variable is assigned to the final one. The current example describes the use of ${array},${array_type},${result:newname (array)}.

2.2 Arraymerge Array Merge 2.2.1 template templates
new ${array_type}[${array1:array}.length +0, ${result}, 00, ${result}, ${array1}.length, $ {array}.length);
2.2.2 Example

2.2.3 Detailed

This example is a merge of two arrays. When you want to get multiple variables of the same type, you need to give each variable a name, example: ${array},${array1:array}, so that you can get multiple variables sequentially.

2.3 do2.3.1 Template Templates
 Do {    while (${condition:var (boolean)});

2.3.2 Example

2.3.3 Detailed

All the template with ${line_selection}, after selecting one or more lines, press the shortcut key shift+alt+z will be called out, select the appropriate template, template in the ${line_selection} is represented as the selected row.

${cursor}, which represents where the cursor appears after a carriage return.

2.4 for2.4.1 Template Templates
 for (${iteratortype:newtype (Java.util.Iterator)} ${iterator} = ${collection}.iterator (); ${iterator}.hasnext ();) {    = (${type}) ${iterator}.next ();    ${cursor}}

2.4.2 Example

2.4.3 Detailed

This example can be used to learn the use of ${collection},${iteratortype:newtype (Java.util.Iterator)}, which is more important is the custom variable, ${iterator}, Non-system class is a custom variable, the default display of {} content, you can modify, modify, all references in the local synchronization modification.

2.5 main2.5.1 Template Templates
 Public Static void Main (string[] args) {    ${cursor}}
2.5.2 Example

2.5.3 detailed

This is another common practice, instead of using any template identifiers, to replace a piece of code that can be used when there is duplicate code.

2.6 static_final2.6.1 Template Templates
${visibility:link (' Public ', ' protected ', ' ', ' Private ')}staticfinal ${type:link (String,int )} ${name} = ${expr:link (' "" ', 0)};

2.6.2 Example

2.6.3detailed

This is a typical ${link} usage, giving a limited number of parameters for easy user Selection

2.7 sysout2.7.1 Template Templates
System.out.println (${word_selection}${}); ${cursor}

2.7.2 Example

2.7.3 Detailed

This should be the most commonly used template, the general user input SYSO can complete the operation, because Syso already uniquely matches the name of the template so no need to choose, so the uniqueness of the template name is also helpful to quickly replace the code. Few users use sysout in accordance with the standard operation, the behavior of the general user is to get System.out.println () in the input syso, and then put in parentheses the variables in the line above the current code, so the template is optimized below.

${} represents a placeholder that enables the TAB key to be positioned at that location for input.

Third, the new template

MyEclipse built-in template does not meet the needs of the time can be built on demand.

    • Name:temlate's name, suggesting simple, easy to remember, the only
    • Context: Scope, java-is the Java full scope, Java statements-is the scope of the Java method, Java type members-is the Java method outside the scope
    • Automatically insert: recommended, automatically inserted when required, no need to select.
    • Insert Variable: Equivalent to a help document with explanations and examples of all available identifiers.    
Iv. Custom Template

  On the basis of Everyone master template, the following is only provided template templates and brief introduction, examples are no longer given, the following is just a point, I hope that according to their own needs to develop their own template. The new method is as follows:

3.1 Sysout Optimization 3.1.1 Templates template
System.out.println (${localvar}); // ${todo} need to delete${cursor}

3.1.2 Template Detailed
    • The output of the general project is output using the log, System.out.println () is generally used for simple viewing of variables that need to be deleted, so add//${todo} need to delete
    • Increase focus positioning ${cursor} so that you can press enter directly to the next line after the input is complete, simplifying the input
    • According to the usual habit, the default output of a recent variable, ${localvar}, because the base type does not match (I did not find a matching method), so you can write Sysi representative to print out the most recent INT,SYSD print out the most recent double
    • Also other template can be optimized, such as for the second, so learn the grammar can be based on their own habits to optimize.
3.2 ArrayList3.2.1 Templates Template
${:import(java.util.List, java.util.ArrayList)}list<${String}> ${listname:newname ( java.util.List)} =new arraylist<${string}>(); ${cursor}

3.2.2 Template Detailed
    • First Use ${:import (Java.util.List, Java.util.ArrayList)} to determine whether the reference, you can reduce the use of shortcut keys, the default generic is a string, if the following <> will be modified synchronously.
    • Automatically generate the available variable name ${listname:newname (java.util.List)}, you can modify it yourself
    • You can define the template named Alist so that a simple input of several characters is done ArrayList created by a shortcut.
3.3 HashMap3.3.1 Templates Template
${:import(Java.util.Map, Java.util.HashMap)}map<${String},${Object}> ${mapname:newname ( JAVA.UTIL.MAP)} =new hashmap<${string},${object}>(); ${cursor}
3.3.2Template Detailed

  The default key is string, which automatically introduces the map and HashMap classes and automatically names

3.4 log3.4.1 Templates Template
${:import(Org.apache.log4j.Logger)}privatestaticfinal Logger Logger = Logger.getlogger (${enclosing_type}.  Class);
3.4.2Template Detailed

  Auto-draw, the current class name is automatically written

3.5 junit3.5.1 Templates Template
${:import(org.junit.Test)} @Testpublicvoid  Test${newname} () {     Try {        ${cursor}    catch  (Exception e) {        e.printstacktrace ();}    }

3.5.2 Template Detailed

This is a simple template for generating JUnit test methods.

MyEclipse templates Detailed (a)--java template Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.