Java code analysis tool for Eclipse plug-in: findbugs

Source: Internet
Author: User

This article describes how to use the Eclipse plug-in findbugs in eclipse.

Eclipse is a very popular development platform. The open and scalable architecture allows many programmers to find their own personalized work environments.

Question:
After writing the code and completing various tests such as unit testing, the code will be submitted for formal operation. The running system can only check whether there is a problem with the code, the hidden errors in the Code are detected during system running and then modified accordingly. The later modification cost is quite high.

Solution:
There are now many Java code analysis tools, one of the open-source projects in findbugs, which can help you find some hidden errors in the Code and improve your code capabilities and system security and reliability.

Install

JDK: 1.5.0 download installation from http://java.sun.com
Eclipse: 3.1.1 download and unzip from http://www.eclipse.org
Findbugs: 0.9.4 from http://findbugs.sourceforge.net/
Official Document http://findbugs.sourceforge.net/manual/
Eclipse plugin for findbugs version 0.0.17
Download from http://findbugs.sourceforge.net/downloads.html

Tips

Eclipse plugin Management

Tip: The newly downloaded plug-ins must not all be placed in the original eclipse directory. A lot of plug-ins are exhausting you :(

  1. The premise is that you have installed the eclipse tool, for example, in the E:/opensource/Eclipse/directory, the following directory is represented by % eclipse_home %;
  2. The default plug-in is in the % eclipse_home %/Plugins directory;
  3. Create a directory named pluginsnew under % eclipse_home %;
    E:/opensource/Eclipse/pluginsnew/
  4. You downloaded a new plug-in, such as XYZ.

    Create the XYZ directory under the % eclipse_home %/pluginsnew/directory, which contains the eclipse Directory, which contains two subdirectories: Features and Plugins. The structure is shown in:

      

  5. Put the downloaded file of the new plug-in the following directory:

    % Eclipse_home %/pluginsnew/XYZ/Eclipse/Features
    % Eclipse_home %/pluginsnew/XYZ/Eclipse/plugins

  6. Create related. link files

    Create an XYZ. Link file in the % eclipse_home %/links directory.
    Such as Path = E:/opensource/Eclipse/pluginsnew/XYZ.
    In this way, if you download multiple plug-ins, you can create multiple link files, and put the link file of the plug-in to the directory of % eclipse_home %/links if you want to load the plug-in, it is easy to use and manage. Do not place it in the default installation directory;
    If your % eclipse_home % is different from this, modify the path in the XYZ. Link file.

  7. Delete and close eclipse

    Delete the % eclipse_home %/links/XYZ. Link file.
    Delete % eclipse_home %/pluginsnew/XYZ entire directory and file

  8. Restart eclipse.
Use

The findbugs plug-in installation method adopts the plug-in management of eclipse usage skills in the previous section.

After you restart eclipse, you can see the "findbugs plug-in" plug-in 0.0.17 provided by the "findbugs Project" in help => about eclipse SDK => plug-in details, as shown in:

  

Introduction

Findbugs is a program that can be found in Java programs.
It is used to find the code in the "bug patterns" list.
Bug patterns indicates an instance of code that is likely to be incorrect.

Original article: findbugs is a program to find bugs in Java programs. It looks for instances of "bug patterns" --- code instances that are likely to be errors.

Currently, findbugs has the highest version 0.9.4, but the update speed is very fast. You should always go up and check whether a new version is released.
Eclipse plugin for findbugs version 0.0.17

Requirements

Findbugs requires at least JDK 1.4.0 and later versions. findbugs is platform independent and can run on GNU/Linux, windows, MacOS X, and other platforms.

Running findbugs requires at least 256 MB of memory. If you want to analyze a large project, you need more memory.

Findbugs independent operation and ant combined detailed operation is not introduced, you can see the official document http://findbugs.sourceforge.net/manual/

The standalone operation is as follows:

  

This article describes how to use eclipse.

Open the bug Details View

Windows => show view => other... => Findbugs => bugdetails

  

In the package explorer or navigator view, right-click your Java project and choose find bugs from the shortcut menu, the sub-menu items include "find bugs" and "Clear bug Markers", as shown in:

  

  

Let's create a simple test file test. Java with the following content:

public class Test{private String[] name;public String[] getName(){return name;}public void setName(String[] name){this.name = name;}}

Click "find bugs". The following progress box is displayed during running:

After running, the following warning information is added to problems:

The warning information after findbugs is displayed not only in the problems view, but also in the source code markup box. In the source code editor, we can see the warning identifier, for example:
When the cursor points to your warning code, there will be an error message, similar to the error or warning message prompts of Eclipse.
If you select the problems in the problems view, the code editor will switch to the corresponding code to facilitate code modification based on the corresponding prompts.
Will point to locations in your code which have been identified as potential instances of bug patterns.

  

In the problems view, right-click the corresponding question entry and choose show bug details from the pop-up menu, as shown in:

  

Click it to switch to the bug Details View to display more detailed prompts.

Of course, when you click the icon with a warning message in the code editing window, it will automatically switch to the bud Details window to view the Detailed Warning Information, as shown in.

  

Based on the detailed information here, you can get the findbugs warning information about your code and corresponding solutions. According to the prompts, you can quickly and conveniently modify the code.

  

As prompted, we changed the code to the following, and then run the code without warning.

public class Test{private String[] name;public String[] getName(){    String[] temp = name;return temp;}public void setName(String[] name){    String[] temp = name;this.name = temp;}}

Configure findbugs
Right-click your project and choose Properties> findbugs>

  

Configurable information includes settings for the following four options:

  1. Run findbugs automatically switch
    When this option is selected, findbugs will automatically run when you modify the Java class. If you set the eclipse automatic compilation switch, findbugs will run after you modify the Java file and save it, and display the corresponding information.
    If this option is not selected, you can run findbugs to check your code each time you need it.

  2. Minimum priority to report option
    This option is used to display the information of the selected level. You can select either low, medium, or high, which is similar to the level setting of log4j.
    For example:
    If you select the high option, only the prompt information of the high level will be displayed.
    If you select the medium option, only medium and high-level prompts are displayed.
    If you select the low option, all levels of prompt information will be displayed.
  3. Enable bug categories selection item
    Here are some options for displaying bug categories:
    Correctness about code correctness
    Performance about Code Performance
    Internationalization about code Internationalization
    Multithreaded correctness
    Style
    Malicious Code Vulnerability

    For example, if you do not select the style check box, the warning information related to the style category will not be displayed. Others are similar.

  4. Select bug patterns to check for selection item
    Here you can select the relevant bug pattern entries to be checked
    You can see in the bug codes, detector name, and detector description what content to check. You can select or remove the corresponding check conditions as needed.
Summary

This plug-in has good functions and can help us improve Java code writing capabilities and write more secure and reliable code. We recommend that you use or add it to ant for continuous building.
Now, you can immediately come up with a project you have developed and check whether your code is correct.

Author Profile
  Dev2dev ID: yulimin, Bea dev2dev Forum web application development Moderator.
Home page: http: // 202.101.111.1/123/
Blog: http://iAMin.BlogDriver.com

 

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.