Aligning Eclipse with the Java Programming Specification

Programming specifications are very important things that make the team's code easy to read and maintain, and also facilitate future functional expansion.

If you want to do good work, you must first sharpen your tools! As a Java programmer, working with Eclipse can be a lifetime. Setting Eclipse into a mode that conforms to the company's programming norms will save us a lot of time.

Anyone who uses Eclipse knows that the shortcut key of Ctrl + Shift + F is to automatically typeset the written code and make it easier to read.

When I first joined the company, an old programmer told me not to use this shortcut, because it would clutter up the code, such as automatically arranging different parameters of a very long method on different lines. In fact, what he didn't know was that Ctrl + Shift + F is based on certain rules, and these rules can be set by yourself.

After setting Eclipse according to the company's programming specifications, after writing the code, a Ctrl + Shift + F, the page will become neat and beautiful. The same is true when reading other people's code.

I don't talk about formal programming specifications here, just how to set up our Eclipse step by step according to the programming specifications.

1. Set the JDK version and encoding format 

 The basic settings in Eclipse are all set in Windows -> Preferences. The personal settings for Java programming are located in Windows -> Preferences -> Java.

(1), JDK version

For Java development, first set the JDK version. MyEclipse has its own Jdk, but eclipse does not.

Follow the steps below to set up a custom JDK.

First in Java -> Compiler, set the Compiler compliance level to 1.6.

Then in the directory on the right side of Java -> Installed JREs, you can see the JDK address, you need to add JDK, you can go to add -> Stankard VM -> Next -> Directory to locate the JDK folder, Finish!

(PS: The jdk path settings are attached here:

CLASSPATH= .;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar

JAVA_HOME = C:/Program Files/Java/jdk1.5.0

PATH  = %JAVA_HOME%/bin;%JAVA_HOME%/jre/bin)

 

(2), encoding format

preferences->General Click workspace, there is a Text file encoding below the right, click Other, select Utf-8, and new projects will be utf-8 encoded in the future.

 

2. Java Doc settings (comment template)

Window -> Preferences -> Java -> Code Style There are a lot of settings for setting the code style, and the comment template is set in Code Style->Code Templates.

(1), File header JavaDoc template settings:

Comments -> Files, click Edit... on the right, and cover the JavaDoc example of the company file header, as follows:

/**

* Copyright <year> Xxx Inc.

*/

(2) JavaDoc template settings such as classes/interfaces and methods are similar to the above:

Similarly, Comments -> type sets the class/interface JavaDoc template, and Methods sets the method.

 

(Attachment: javadoc comment tag syntax
  @author The description of the class indicates the author who developed the module of this class
  @version The description of the class indicates the version of the module of the
  class @see The reference to the description of the class, attribute and method is the related topic
  @ param Description of the method Description of a parameter in the
  method @return Description of the method Description of the return value of the
  method @exception Description of the method Description of the exceptions that the method may throw)

The following is a very standard annotation template found on the Internet:

File annotation tags:
/**  
* @Project: ${project_name}
* @Title: ${file_name}
* @Package ${package_name}
* @Description: ${todo}
* @author qsk
* @date ${ date} ${time}
* @Copyright: ${year} www.xxx.cn Inc. All rights reserved.
* @version V1.0  
*/
Class (Types) annotation label (class annotation):
/**
* @ ClassName: ${type_name}
* @Description: ${todo}
* @author qsk
* @date ${date} ${time}
* @version v1.0
* ${tags}
*/
Fields annotation tags:
/ ** @Fields ${field} : ${todo} */

Constructor tags:
/**
* <p>Title: </p>
* <p>Description: </p>
* ${tags}
*/

Constructor & Methods tags:
/**
* @Title: ${enclosing_method}
* @Description: ${todo}
${tags} * @author qsk
* @throws
*/
Overriding Methods tags:
/*
* <p>Title: ${enclosing_method}</p>
* <p>Description: </p>
* ${tags}
* ${see_to_overridden}
*/

Delegate Methods tag:
/**
* ${ tags}
* ${see_to_target}
*/

getter method tags:
/**
* @return ${bare_field_name}
*/
setter method tags:
/**
 * @param ${param} the ${bare_field_name} to set
 */

 

3. Code style settings

We can set our own Formatter as needed.

In the Code Style option, select Formatter, we choose to create our own Formatter, click "New...", and set the name.

Then jump to the style detail settings page .

Let's talk about the various tabs of Fomatter's style settings page in detail.

(1) Set the tab key in Eclipse to 4 spaces, which is more troublesome.

First of all, there is a Tab policy option under the first tab Indentation, select spaces only and save it.

Then, set to replace TAB with spaces, window->preference->General->Editors->Text Editors, there is an insert space for tabs on the right, select and save;

 

(2) The first tab, Indentation, is to set the indentation. After selection, the Preview on the right changes directly, which can be observed.

The Statements within 'switch' body option should generally be checked.

 

(3) The second tab, Braces, is to set whether the brackets wrap.

Generally, except class/Interface and array (ie the first and last) are set to Same Line, others are set to Next Line.

 

(4) The third tab, White Space, is for setting spaces.

Generally, there must be one space after comma, one space before and after all operators and assignment characters, and one space between all keywords and other keywords and non-keywords.

Because the settings are too cumbersome, I will not list them one by one here, it is very simple.

 

(5) The fourth tab, Blank Lines, is set to blank lines.

 

(6) The fifth tab New Lines is to set the braces to wrap.

 

(7) The sixth tab, Control Statements, sets the format of the control statement.

The option Insert new Line before 'else' in an 'if' statement should generally be checked.

 

(8) The seventh tab Line Wrapping is to set various specific values ​​for line spacing, indentation, and maximum line length.

Here, in order to prevent random line breaks during automatic formatting, the Maximum line width can be set to a larger value, and I directly set it to 500.

 

(9) The eighth tab, Comments, is to set the comment format.

 

(10) The ninth tab, Off/On Tags, is used to set the tag switch in the annotation.

 

4. Link JDK class library help documentation (API) 

We know that in Eclipse, using Shift+F2 for a class can open its API document, but, due to various reasons, it often cannot be opened, but in fact this can be set. as follows:

(1) Window -> Preferences -> Java -> Installed JREs 

Select the existing JDK in use and click Edit on the right

There is a list of JRE system libraries in the center of the dialog box, which contains 9 jar packages.

 

(2) Expand a Jar package, there are 2 files in it, one is the class source file src.zip, the other is javadoc location: (none), all we have to do is to replace this empty address with the actual API document on our hard disk. address. Select javadoc location, click the Javadoc Location... option on the right, click Browse... in the pop-up box, and enter the actual address of the API document on the hard disk.

 

(3) Set each jar package in turn. Finally, when we select a JDK class, Shift + F2, we can directly open the corresponding API document of the class in eclipse.

 

 5, encoding format, font, background color settings

In addition to the above settings related to programming conventions, there are many personalized settings that make Eclipse easier to use.

(1), font settings

Preferences->General->Appearance->Colors and Fonts Select Java->Java Editor Text Font from the list on the right, click Edit... to change the java font.

The font settings here will not affect the fonts of xml, txt, properties and other files, and the fonts of the initialized xml, txt, properties files are generally small and difficult to observe, see the settings below.

 

(2), xml, txt, properties font settings

Preferences -> General -> Appearance -> Colors and Fonts Select Basic -> Text font from the list on the right, click Edit... to change the font.

Sometimes I feel that the font of the console is not eye-catching. If you want to change it, you can use the following method.

 

(3), debug font settings

Preferences -> Appearance -> colors and fonts Select debug -> console font from the list on the right, click Edit... to change the font.

Other font settings are roughly the same.

When facing the computer for a long time, the bright white color will make the programmer's eyes dizzy and unable to concentrate. Setting the background color can alleviate this problem very well.

 

(4), background color settings

Preferences -> General -> Editors ->Text Editors Select it, and the right panel has an Appearance color options to set the color of various modules.

Select Background color, remove the check of System Default on the right, click Color, and set the color in it.

The ophthalmologist recommends the background color as: Hue 85, Saturation 123, Brightness 205. After setting, a beautiful and elegant light green will appear.

 

------------------------------------------------------------------------------------------------------------------------------------------------------------

PS: If you think this blog post is good, please don’t forget to recommend it, thank you.

* @author qsk

Reprinted in: https://www.cnblogs.com/zrtqsk/p/3688874.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324273053&siteId=291194637