Installation and use of the Eclipse plug-in Checkstyle

Source: Internet
Author: User
Tags modifier naming convention checkstyle

Reprinted from: http://www.cnblogs.com/lanxuezaipiao/p/3202169.html

Checkstyle is a project under SourceForge that provides a tool to help Java developers comply with certain coding specifications. It automates the process of code specification checking, freeing developers from this important but tedious task.

Main contents of Checkstyle Test
· Javadoc notes
• Naming Conventions
• Title
· Import Statement
• Volume size
• Blank
• Modifiers
• Block
• Code issues
• Class Design
• Hybrid check (pack some useful examples of non-essential system.out and Printstacktrace)

As can be seen from the above, Checkstyle provides most of the functionality that is checked for code specifications without providing the functionality to enhance the code quality and modify the code as much as PMD and jalopy. However, it is powerful enough for team development, especially for companies that emphasize code specifications.

The Checkstyle plug-in can be integrated into the Eclipse IDE to ensure that Java code conforms to the standard code style.

First, CheckStyle installation method

method One:1.Eclipse, select Help->software updates->find and install 2. Select Search for new features to install select Next 3. Select New Remote Site 4. Enter the update address: http://eclipse-cs.sourceforge.net/update 5. Restart after installation Method Two:1. After downloading the Eclipse plugin http://sourceforge.net/projects/eclipse-cs/, put the two folders inside plugins and Features the following files are copied to the respective plugins and features directories under the Eclipse Directory 2. If you just start eclipse directly, you might get classnotfoundexceptions Such errors (which generally do not occur), so you must add a parameter-clean when you start eclipse. This allows eclipse to update many of the plug-in information it currently installs, such as your Eclipse on D, which is D:\eclipse\eclipse.exe-clean second, the use of CheckStyleAfter the installation is successful, select the project, right-click Checkstyle->check code with Checkstyle, check for errors third, the result output of CheckstyleAfter checking you will find a lot of magnifying glass icon in the code, as shown, this is the output of checkstyle, the specific meaning is as follows:

1 Type is missing a Javadoc commentclass
Missing type description

2 "{" Should is on the previous line
' {' should be on the previous line. Workaround: Put "{" On the previous line

3 Methos is missing a Javadoc comment
The Javadoc comment is missing in front of the method. Workaround: Add the Javadoc comment, similar to this:

/**

* Set default mock parameter. (method description)

* @param additionalparameters parameter additional (parameter name)

* @return Data Manager (return value description)

* @throws Exception if has error (exception description)

*/

4 expected @throws tag for "Exception"
Note that you want to have @throws in the comments
Workaround: Add such a line to the comment before the method: * @throws Exception if has error (exception description)

5 "." is preceeded with whitespace "."
There must be no spaces in front. Workaround: Put the "." The preceding space is removed

6 "." is followed by whitespace "."
There must be no spaces behind. Workaround: Put the "." The space behind is removed

7 "=" is not preceeded with whitespace
The space is missing in front of "=". Workaround: Add a space before "="

8 "=" is not followed with whitespace "="
There is a missing space behind. Workaround: Add a space after "="

9 "}" should be on the same line "}"
Should be on the same line as the next statement. Workaround: Put "}" in front of the next line

Ten Unused @param tag for "Unused"
No parameter "unused", no comment required
Workaround: "* @param unused parameter additional (parameter name)" Remove the comment of this line unused parameter "

Variable "CA" Missing Javadoc
Variable "CA" missing Javadoc comment
Workaround: Add the Javadoc comment before the "CA" variable:/** ca. */(Note: Be sure to include "." )

Line longer than 80characters
The line length exceeds 80. Workaround: Divide it into multiple lines of writing. When necessary, you can ctrl+shift+f

Line contains a tab character
The line contains the "tab" character.

Redundant "public" modifier
The "public" modifier for redundancy. Workaround: Remove the "public" modifier

Final modifier out of order with the JSL suggestion
Final modifier in the wrong order

Avoid using the ". *" Form of import
The import format avoids the use of ". *". Workaround: You can not add import, and then need to use the direct "CTRL + O" import.

Redundant import from the same package
Import content from the same package, superfluous, delete it

Unused import-java.util.list
Import incoming java.util.list is not used. Workaround: Remove unnecessary classes that are imported

Duplicate Import to Line 13
Duplicate import of the same content. Workaround: Remove unnecessary classes that are imported

Import from Illegal package
Import content from an illegal package

"While" construct must use "{}"
"While" statement is missing "{}"

Variable "STest1" must be private and has accessor method
The variable "STest1" should be private, and there is a way to call it

Variable "ABC" must match pattern "^[a-z][a-za-z0-9]*$"
The variable "ABC" does not conform to the naming convention "^[a-z][a-za-z0-9]*$", the Workaround: Change the name to a rule-compliant named "ABC"

("is followed by whitespace
"(" Cannot have space after ")" is proceeded by whitespace ")" must not be preceded by a space. " Workaround: Remove the front or back spaces

First sentence should end with a period
Workaround: You should add a "." To the end of your comment's first line of text.

Redundant throws: ' Namenotfoundexception ' is subclass of ' namingexception '.
' Namenotfoundexception ' is a subclass of ' namingexception ' that repeatedly throws an exception.
Workaround: If you throw two exceptions, one exception class is another subclass, then you only need to write the parent class
Remove the namenotfoundexception exception, the corresponding Javadoc comment Exception comment Description also need to remove

Parameter DocType should be final.
Parameter doctype should be final type, Workaround: Add a final before the parameter doctype

Trailing spaces.
Extra empty line, workaround: Remove this line of blank line

Must has at least one statement.
At least one statement

Workaround:} catch (NumberFormatException nfe) {

Log.error ("Auto renews the agreement failed", NFE);//abnormal catch inside cannot be empty, add a sentence in the exception. such as printing and so on

' > ' is not followed by whitespace. And there's ' (' is preceded with whitespace.
When defining collections and enumerations, there are spaces after the last ">", "(" There are no spaces in front of them.) Workaround: Remove generics

Got an exception-java.lang.runtimeexception:unable-get class information for @throws tag ' systemexception '.
Reason: Unreasonable throws.

Workaround: To ensure that certain types, such as some classes, interfaces are not throws. Remove the exception to the declaration. Throwing Exceptions in the implementation class

Online Reference Solutions:

1, this is the Checkstyle report of the fault. Refreh is often required to clean/build this project. If not, you can try clean all projects, restart Eclipse.

2, because the compiled class is not in the Checkstyle classpath. So, as long as the compiled class is configured in the <checkstyle/> classpath there is no problem. In addition, Also found Checkstyle line length seems to have a problem, obviously not more than 120 characters, but still error. Helpless, I put eclipse in Java > code style > Formatter in the maximum line wit H changed to 100, then format, basically no problem.

The File does not end with a newline.
WORKAROUND: Delete the error class, create a new class with the same name, copy the code all over

Utility classes should not has a public or default constructor.
There should be no public or default construction method in the inner class of the interface

WORKAROUND: In the inner class, define a private construction method, and then the inner class is declared as the final type. If there is static in front, final must also be placed after static

Variable ' Functioncode ' must be private and has accessor methods.
Change the variable to private and provide access to the method

Workaround: Change the modifier of these variables to private, then provide the Set,get method, and add the corresponding method Javadoc comment, parameter comment. and add final before the return value and the parameter type. and change the place where this variable was called to access by method

' X ' hides a field.

public class Foo
{
private int bar;

public Foo (int bar)
{
This.bar = bar;
}

Public final int Getbar ()
{
return bar;
}
}

The global private int bar; and the bar variable name of the local public Foo (int bar) repeats.
Workaround: Change the name of the parameter inside the method to be public Foo (int newBar)
{
This.bar = NewBar;
}。

Got an exception-unexpected character 0xfffd in identifier

This is because Checkstyle does not recognize the coding format that is being developed.

Online Reference Solutions:

1. Can be configured in eclipse, can be specified in Other-->checker

2, you can modify the Checkstyle configuration file:

<module name= "Checker" >

<property name= "severity" value= "Warning"/>

<property name= "CharSet" value= "UTF-8"/>

<module name= "TreeWalker" >

If it's UTF-8, add the Bold italic statement, and you're ready to go.

PNS Got an exception-java.lang.runtimeexception:unable-get class information for @throws tag *whatever*.
Online reference workaround: Check Checksytle JavaDoc---JavaDoc---and logloaderrors. If it is checkstyle self-loading error, hit a log on it, do not make the whole errors scary.
There is also a place where the same mistakes can be wrapped up. Coding problems--and redundant Throws--logloaderrors Select to

Expected @param tag for ' DataManager '.
Comment workaround for missing dataManager parameter: Add @param dataManager in comments DataManager

Parameter X should be final.
public class Foo
{
private int bar;

public Foo (int bar)
{
This.bar = bar;
}

Public final int Getbar ()
{
return bar;
}
}

Explanation: The local variable of public foo (int bar), which is considered immutable, checks for the need to add the final keyword to define public foo (final int bar) for this error, which can be ignored without checking.

Redundant ' X ' modifier.

Public interface Cachehrtreeservice extends Manager {

/**
* Organization Tree
* @param orgdto
* @return
* @throws Exception
*/
public void Setorganization (Organizationdto orgdto) throws Exception;

/**
* Organization Tree
* @return
* @throws Exception
*/
Public Organizationdto getorganization () throws Exception;
......
}

Explanation: Extra fields. Public Organizationdto getorganization () throws Exception; the public is the extra field at this time, because when interface is defined, it is public.

Need to be checked.

The should Class X is declared as final.

Explanation: For a singleton design pattern, a unique class object is required to be returned. However, Hrfactory and Contextfactory are optimized for two classes and do not require checking.
Other singleton classes still need to be checked.

The Method ' Addchildrenid ' isn't designed for extension-needs to be abstract, final or empty.

Explanation: Inherited by the parent class, this class is a bit special to ignore.

Variable ' ID ' must be private and has accessor methods.

Explanation: The Basehrdto class, which is the parent class, inherits the subclass and is more special. But other classes, fame need to add the scope ' private ' keyword. Need to be checked.

The Array brackets at illegal position.

Explanation: Code writing, habits are not the same. Need to check, just hint

Installation and use of the Eclipse plug-in Checkstyle

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.