Eclipse插件FindBugs安装及使用笔记

今天学习了下安装和使用Eclipse的FindBugs插件,本文为学习笔记。

我使用的操作系统为Win7旗舰版,Eclipse版本为Luna Service Release 1 (4.4.1)

以下是一个专门介绍FindBugs安装方法的页面:http://findbugs.cs.umd.edu/eclipse/

页面内容如下:

Welcome to the FindBugs Eclipse plugin update site.

This web page provides automatic distribution and updates for the Eclipse plugin for FindBugs.

Plugin requirements

This plugin has primarily been tested with Eclipse 3.3 and 3.4 It should work with 3.x releases, but let us know if you have any problems. The plugin is not compatible with versions of Eclipse preceding 3.3. The plugin runs under Java 1.5/5.0, or newer.

Plugin installation

If you have previously installed a version of the FindBugs plugin prior to mid-May, 2006, then you should remove it first. Simply remove the de.tobject.findbugs_0.0.n directory from Eclipse's plugins directory.

To install the FindBugs plugin:

  1. In Eclipse, click on Help -> Software Update -> Find and Install...
  2. Choose the Search for new features to install option, and click Next.
  3. Click New Remote Site.
  4. Enter the following:
    • Name: FindBugs update site
    • URL: one of the following (note: no final slash on the url)
      • http://findbugs.cs.umd.edu/eclipse for official releases
      • http://findbugs.cs.umd.edu/eclipse-candidate for candidate releases and official releases
      • http://findbugs.cs.umd.edu/eclipse-daily for all releases, including developmental ones
    and click OK.
  5. "FindBugs update site" should appear under Sites to include in search.
    Click the checkbox next to it to select it, and click Finish.
  6. You should see FindBugs Feature under Select features to install.
    (You may have to click on one or two triangles to make it visible in the tree.)
    Select the checkbox next to it and click next.
  7. Select the I accept option to accept the license and click Next.
  8. Make sure the location is correct where you're installing it. The default (your workspace) should be fine. Click Finish.
  9. The plugin is not digitally signed. Go ahead and install it anyway.
  10. Click Yes to make Eclipse restart itself.

按照里面所说的方法,结合EclipseLuna的实际情况,安装FindBugs插件操作步骤如下:

1、找到菜单Help→Install New Software

2、点击Add按钮

3、在“Add Repository”界面,Location就填写 http://findbugs.cs.umd.edu/eclipse

加载出要更新的内容后,点击Next继续

4、再次点击Next继续

5、确认安装,勾选“I accept ...”,并点击Finish开始安装

FindBugs is free software distributed under the terms of the Lesser GNU Public License.

http://www.gnu.org/licenses/lgpl.html

6、漫长的读条时间

我在安装时曾弹出过一个警告界面,点击OK即可

7、安装成功后,点击重启Eclipse使FindBugs插件生效

至此FindBugs就安装完毕了,在菜单Help→AboutEclipse打开的界面中可以查看到FindBugs的版本信息:

下面用一个最简单的例子说明FindBugs的使用方法。

建立一个Java工程名为FindBugsTest,内含FindBugsTest.java代码如下:

public class FindBugsTest {
    public static void main(String[] args) {        
        Integer x = 1;
        Long y = 2L;
        if (x.equals(y)) {
            // bla bla bla ...
        }
    }
}

使用FindBugs前需要先调出FindBugsExplorer,找到Window→ShowView→Other

在里面找到FindBugs下的BugExplorer,点击OK

在PackageExplorer的项目图标上单击鼠标右键,在上下文菜单中点击FindBugs→FindBugs

即可运行FindBugs,在BugsExplorer中即可看到FindBugs运行结果:

被判定为有问题的行,左侧会出现一个虫形标记。更详细的说明在BugExplorer中单击鼠标右键,在上下文菜单中点击Properties即可查看。

分析结果存储在一个XML文件中,BugExplorer读取的便是此XML:

<?xml version="1.0" encoding="UTF-8"?>

<BugCollection version="3.0.1-dev-20150306-5afe4d1" sequence="1" timestamp="1470407913846" analysisTimestamp="1470407912944" release="">
  <Project projectName="FindBugsTest">
    <Jar>C:\Users\Tsybius\workspace2\FindBugsTest\bin</Jar>
    <AuxClasspathEntry>C:\Users\Tsybius\workspace2\FindBugsTest\bin</AuxClasspathEntry>
    <AuxClasspathEntry>C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar</AuxClasspathEntry>
    <SrcDir>C:\Users\Tsybius\workspace2\FindBugsTest\src</SrcDir>
    <Cloud id="edu.umd.cs.findbugs.cloud.doNothingCloud" online="false"></Cloud>
  </Project>
  <BugInstance type="EC_UNRELATED_TYPES" priority="1" rank="1" abbrev="EC" category="CORRECTNESS" first="1">
    <Class classname="FindBugsTest">
      <SourceLine classname="FindBugsTest" sourcefile="FindBugsTest.java" sourcepath="FindBugsTest.java"/>
    </Class>
    <Method classname="FindBugsTest" name="main" signature="([Ljava/lang/String;)V" isStatic="true">
      <SourceLine classname="FindBugsTest" start="10" end="15" startBytecode="0" endBytecode="10" sourcefile="FindBugsTest.java" sourcepath="FindBugsTest.java"/>
    </Method>
    <Type descriptor="Ljava/lang/Long;" role="TYPE_FOUND">
      <SourceLine classname="java.lang.Long" start="54" end="1615" sourcefile="Long.java" sourcepath="java/lang/Long.java"/>
    </Type>
    <Type descriptor="Ljava/lang/Integer;" role="TYPE_EXPECTED">
      <SourceLine classname="java.lang.Integer" start="52" end="1590" sourcefile="Integer.java" sourcepath="java/lang/Integer.java"/>
    </Type>
    <LocalVariable name="y" register="2" pc="13" role="LOCAL_VARIABLE_VALUE_OF"/>
    <LocalVariable name="x" register="1" pc="13" role="LOCAL_VARIABLE_VALUE_OF"/>
    <Method classname="java.lang.Integer" name="equals" signature="(Ljava/lang/Object;)Z" isStatic="false" role="METHOD_EQUALS_USED">
      <SourceLine classname="java.lang.Integer" start="974" end="977" startBytecode="0" endBytecode="72" sourcefile="Integer.java" sourcepath="java/lang/Integer.java"/>
    </Method>
    <SourceLine classname="FindBugsTest" start="12" end="12" startBytecode="14" endBytecode="14" sourcefile="FindBugsTest.java" sourcepath="FindBugsTest.java"/>
    <SourceLine classname="FindBugsTest" start="12" end="12" startBytecode="14" endBytecode="14" sourcefile="FindBugsTest.java" sourcepath="FindBugsTest.java"/>
  </BugInstance>
  <Errors errors="0" missingClasses="0"></Errors>
  <FindBugsSummary timestamp="Fri, 5 Aug 2016 18:13:17 +0800" total_classes="1" referenced_classes="10" total_bugs="1" total_size="8" num_packages="1" java_version="1.8.0_25" vm_version="25.25-b02" cpu_seconds="1.56" clock_seconds="0.91" peak_mbytes="322.35" alloc_mbytes="494.94" gc_seconds="0.04" priority_1="1">
    <PackageStats package="" total_bugs="1" total_types="1" total_size="8" priority_1="1">
      <ClassStats class="FindBugsTest" sourceFile="FindBugsTest.java" interface="false" size="8" bugs="1" priority_1="1"/>
    </PackageStats>
    <FindBugsProfile>
      <ClassProfile name="de.tobject.findbugs.builder.FindBugs2Eclipse" totalMilliseconds="272" invocations="2" avgMicrosecondsPerInvocation="136397" maxMicrosecondsPerInvocation="227317" standardDeviationMircosecondsPerInvocation="90920"/>
      <ClassProfile name="edu.umd.cs.findbugs.classfile.engine.ClassInfoAnalysisEngine" totalMilliseconds="231" invocations="287" avgMicrosecondsPerInvocation="808" maxMicrosecondsPerInvocation="12140" standardDeviationMircosecondsPerInvocation="1464"/>
      <ClassProfile name="edu.umd.cs.findbugs.classfile.engine.ClassDataAnalysisEngine" totalMilliseconds="78" invocations="287" avgMicrosecondsPerInvocation="274" maxMicrosecondsPerInvocation="2748" standardDeviationMircosecondsPerInvocation="186"/>
      <ClassProfile name="edu.umd.cs.findbugs.detect.BuildObligationPolicyDatabase" totalMilliseconds="51" invocations="10" avgMicrosecondsPerInvocation="5109" maxMicrosecondsPerInvocation="45455" standardDeviationMircosecondsPerInvocation="13463"/>
      <ClassProfile name="edu.umd.cs.findbugs.detect.FieldItemSummary" totalMilliseconds="46" invocations="10" avgMicrosecondsPerInvocation="4671" maxMicrosecondsPerInvocation="25835" standardDeviationMircosecondsPerInvocation="7476"/>
    </FindBugsProfile>
  </FindBugsSummary>
  <ClassFeatures></ClassFeatures>
  <History>
    <AppVersion sequence="0" timestamp="1470407913846" release="" codeSize="0" numClasses="0"/>
  </History>
</BugCollection>

最后提一下,在Window→Preferences菜单下,找到Java→FindBugs即可对FindBugs插件进行一些相关的设置

END

转载于:https://my.oschina.net/Tsybius2014/blog/728077

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值