Play eclipse-The Java code Template__java generated by automatic codes

Source: Internet
Author: User

When the code is written to a certain extent, it will be found that a lot of code is repeatedly knocking N more times, and even without exaggeration to say: close your eyes can be knocked out. A lot of these repetitive code, in addition to the speed of tapping the keyboard, there is basically no other benefits, but the long-term will waste a lot of time, reduce the efficiency of software development. For example, to output logger information during program execution, you need to add such a piece of code in each class:

Import Org.slf4j.Logger;
Import org.slf4j.LoggerFactory;

public class Xxxclass {
    /**
     * Logging mechanism.
     *
    private static Logger Logger = Loggerfactory.getlogger (Xxxclass.class);
    
}
The only difference in this code in different class files is the name of class: Xxxclass. The average person may knock one letter at a time; a lazy person will CTRL + C and CTRL + V, and then change the xxxclass; the wise man can fix it by adding a shortcut key with a few letters. This article takes the above logger as an example, introduces the intelligent person to use Java Code template realizes a more convenient and quick way.

1. Template Settings

On the Eclipse toolbar, select window-> Preferences-> Java-> Editor-> Templates to enter the following interface:


The list is Eclipse's own code Template, and the last column is "on" and the expression is not valid. To make your own code Template effective, you can select the code Template you want, click Edit ..., and in the Open Edit Template dialog box, select the Automatically insert checkbox and click OK Save the settings on the line. Use these effective, code-template methods, as shown in the following logger.

Click on "New ..." to create a new template, the template details are set as follows:


Where you need to declare logger, enter the template name logger, and then press CTRL + Space shortcut to display the template Logger prompt box as shown in the following illustration:


Select the template logger you want to add, and the code in the template is automatically added, including the import packages, comments, and logger declarations. 2. Ctrl + Space Failure

In some cases, when Ctrl + Space shortcuts are pressed, there is no response, and no prompt box is displayed. There are two possible scenarios:

Case 1: No content assist shortcut keys are set

On the Eclipse toolbar, select Window-> Preferences-> general-> keys, and enter the following shortcut key settings interface:


When you enter "content assist" in the edit box, you automatically search for the Content Assist command, select it, enter "Ctrl+space" in the "Binding" box below, or press Ctrl + space shortcut keys, and then click OK ".

Case 2:ctrl + Space shortcut key conflict

If CTRL + Space is already set to the content assist shortcut, there is no effect, possibly because eclipse's CTRL + Space shortcuts conflict with the shortcut keys for Input method switches in Windows systems. Modify the shortcut keys for the input method switch to something else, as shown in the following illustration:


3. Ctrl + Space Tip box does not have a template

If CTRL + space is pressed, the template logger is not displayed in the Prompt box, as shown in the following illustration:


This is because the template hint (Template proposals) is not selected in the content hint (Assist). On the Eclipse toolbar, select window-> Preferences-> Java-> Editor-> Content Assist-> Advanced, you can set the following settings:


        Select the checkbox in front of "Template proposals" in two boxes. If the "Template proposals" in the box below is not the first, you can select it and click the "Up" button until it is in the first position, so that the first one in each prompt box is the code Template you need.

Related Article

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.