解决方案

FindBugs配置介绍

seo靠我 2023-09-24 06:53:41

FindBugs bug类型说明

Findbugs是一个静态分析工具,它检查类或者JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。Findbugs自带检测器,其中有60余种Bad pracSEO靠我tice,80余种Correctness,1种 Internationalization,12种Malicious code vulnerability,27种Multithreaded correcSEO靠我tness,23种Performance,43种Dodgy。

一、Bug级别

根据Bug可能导致的后果,FindBugs定义了若干Bug级别,主要的级别如下所示:

Bad Practice: 不好的实践

CoSEO靠我rrectness: 正确性

Experimental:试验性的

Internationalization:   国际化的

Malicious code vulnerability: 存在漏洞的有害代码

MuSEO靠我ltithreaded correctness: 多线程正确性

Performance:性能

Security:安全

Dodgy: 欺骗性代码

二、常见bug及处理方法

http://wenku.baidu.cSEO靠我om/link?url=kEfgjhK2h1Lghe0hkkpPsw4VCpHFLMI16nwPJiTw1QU_BfjhKDH5oeAcmhn5KD53_WpOs3Z5LwTUfvmqGFSXg1D1SEO靠我DKJhvAJEefrscRPjxoq

http://wenku.baidu.com/link?url=dluPWnkv3bINtb0ndbf_9qwpGsvsZxlpDHzWFRC8_oDI7G5pRSEO靠我Ug2-K83TGPI93HQ8kb7cvIdVt-CunOhTC22AtCqRAVEDmHoM26K2msgQ9a

FindBugs插件设置说明

来源网址:file:///C:/Users/niupp/SEO靠我AppData/Local/Yodao/DeskDict/frame/20141120132153/index.html#

1、打开设置界面:项目>属性>FindBugs

Enable project sSEO靠我pecific settings:使能项目的个性化设置;

Run automatically(also on full build):自动运行(编译工程和文件的时候自动运行);

analysis effoSEO靠我rt:分析工作,选择Minimal速度最快但不够准确;

Store issue evaluation in:问题评价的存储,应该是选择是否将问题上传到FindBugs Cloud上;

Reporter CSEO靠我onfiguration:报告设置;

Minimunm rank to report:等级报告,选择某数字表示报告该数字以上等级bug,1表示最严重,20最轻微;

Minimunm Confidence SEO靠我to report:选择Low,Low以上级别都会显示,即所有级别的提示信息都会被显示;

Reported(visible )bug categories:报告(可见)的bug种类,选择相关种类后,该类SEO靠我型的bug才会显示;

Bad Practice: 不好的实践;

Malicious code vulnerability: 存在漏洞的有害代码;

Correctness: 正确性;

Performance:性SEO靠我能;

Security:安全;

Dodgy code: 欺骗性代码;

Experimental:试验性的;

Multithreaded correctness: 多线程正确性;

InternationalizaSEO靠我tion: 国际化的;

Mark bugs with…rank as:标记bug的等级;

Filter files:过滤文件,包括或者排除特定的类和方法的bug检测,将过去的XML的FindBugs结果报SEO靠我告添加后可以将报告中的bug排除分析;

Plugins and misc.Settings:插件和misc的设置;

Run FB analysis as extra job(independent froSEO靠我m build job):运行FB分析作为额外的工作(独立于构建工作),可以改进用户体验特别是对于递增编译;

Cache .class data(useful for slow file system SEO靠我& lot of RAM)(experimental):类文件的缓存数据,为下一次的FindBugs分析可以重用该缓存数据,缓存会一直保持知道下一次项目的构建;

Currently, plugins cSEO靠我an only be updated from the workspace settings:现在,插件只能通过工作区的设置来升级;

Detector configuration:检测器配置,可以选择所SEO靠我要进行检查的相关的Bug 模式条目,不勾选的检测器将不会在进行分析,“Grayed out”的检测器会运行但是不会输出报告到界面;

2、检测器类型

http://wenku.baidu.com/link?SEO靠我url=u37YN6F58LkqIsPwcaQJ1RPN-4khSqzTQt9tvydRZO-kn1Me4VY2jzub6OsmxzXXBDoQVN4IqxnTr74d2GGTPTrdS-zuVBLlSEO靠我aYvjbLDBukG

AppendingToAnObjectOutputStream:添加到一个对象的输出流;

AtomicityProblem:并发抽象上的操作序列(如get / put)将不会自动执SEO靠我行;

BadAppletConstructor:坏的Applet构造函数;

BadResultSetAccess:寻找getXXX或setXXX方法调用的结果集字段索引为0,但实际结果集字段索引从1开始;SEO靠我

BadSyntaxForRegularExpression:正则表达式的无效语法;

BadUseOfReturnValue:坏的返回值使用,在非空检查之后函数的返回值被丢弃;

BadlyOverriddeSEO靠我nAdapter:严重覆盖适配器,该代码使用错误的签名扩展了一个适配器类,覆盖了一个监听器方法;

BooleanReturnNull:布尔类型返回空值;

CallToUnsupportedMethod:调SEO靠我用不支持的方法;

CheckExpectedWarnings:检查预期警告;

CheckImmutableAnnotation:检查不变的注释;

CheckRelaxingNullnessAnnotatioSEO靠我n:传给方法一个声明为@NonNull的null参数;方法的返回值声明为@NonNull实际是null。

CheckTypeQualifiers:检查类型修饰符;

CloneIdiom:查找违反编写可克隆SEO靠我类的习语;

ComparatorIdiom:比较器;

ConfusedInheritance:混乱的继承;

ConfusionBetweenInheritedAndOuterMethod: 混淆继承和外部方SEO靠我法;

CrossSiteScripting:寻找明显的跨站脚本漏洞;

DefaultEncodingDetector:默认编码检测器,当对字符串使用upper或lowercase方法,如果是国际的字符串,SEO靠我可能会不恰当的转换。

DoInsideDoPrivileged:寻找doPrivileged内应该执行的代码块;

DontCatchIllegalMonitorStateException:查找try -SEO靠我 catch块的非法监控状态异常;

DontIgnoreResultOfPutIfAbsent:不要忽视PutIfAbsent的结果;

DontUseEnum:不使用枚举;

DroppedExceptionSEO靠我:DE查找代码中,捕获了异常,但不采取措施来处理异常的情况;

DumbMethodInvocations:愚蠢的方法调用;

DumbMethods:愚蠢的方法;

DuplicateBranches:查找如 SEO靠我if/else or switch的重复分支;

EmptyZipFileEntry:空的zip文件;

EqualsOperandShouldHaveClassCompatibleWithThis:不兼容的SEO靠我类定义equals方法;

ExplicitSerialization:明确的序列化;

FinalizerNullsFields:终结器null字段;

FindBadCast2:查找坏了使用数据流分析的对象引SEO靠我用;

FindBadForLoop:查找不正确的循环;

FindCircularDependencies:查找类间的循环依赖;

FindDeadLocalStores:查找无用的局部变量存储;

FindDouSEO靠我bleCheck:查找双重检查锁定;

FindEmptySynchronizedBlock:检查孔的同步块;

FindFieldSelfAssignment:

FindFinalizeInvocationsSEO靠我:查找调用finalize()或与此相关的问题;

FindFloatEquality:

FindHEmismatch:

FindInconsistentSync2:

FindJSR166LockMonitorSEO靠我enter:

FindLocalSelfAssignment2:

FindMaskedFields:

FindMismatchedWaitOrNotify:

FindNakedNotify:

FindNonShSEO靠我ortCircuit:

FindNullDeref:

FindNullDerefsInvolvingNonShortCircuitEvaluation:

FindOpenStream:

FindPuzzlerSEO靠我s:

FindRefComparison:

FindReturnRef:

FindRoughConstants:

FindRunInvocations:

FindSelfComparison:

FindSelfCSEO靠我omparison2:

FindSleepWithLockHeld:

FindSpinLoop:

FindSqlInjection:

FindTwoLockWait:

FindUncalledPrivateMeSEO靠我thods:

FindUnconditionalWait:

FindUninitializedGet:

FindUnrelatedTypesInGenericContainer:

FindUnreleasedSEO靠我Lock:

FindUnsatisfiedObligation:

FindUnsyncGet:

FindUseOfNonSerializableValue:

FindUselessControlFlow:

FoSEO靠我rmatStringChecker:

HugeSharedStringConstants:

IDivResultCastToDouble:

FindBugs bug代码举例

一、不需要修改的Bug

1、源代码

pSEO靠我rivateString[]name;

publicString[] getName(){

returnname;

}

2、Bug描述

Bug: Findbugs.getName() may expose inSEO靠我ternal representation by returning name

Returning a reference to a mutable object value stored in oneSEO靠我 of the object’s fields exposes the internal representation of the object.  If instances are accesseSEO靠我d by untrusted code, and unchecked changes to the mutable object would compromise security or other SEO靠我important properties, you will need to do something different. Returning a new copy of the object isSEO靠我 better approach in many situations.

Rank: Of Concern (18),confidence: Normal

Pattern: EI_EXPOSE_REP

TySEO靠我pe: EI,Category: MALICIOUS_CODE (Malicious code vulnerability)  存在漏洞的有害代码

说明:Bug: Findbugs.getName()可SEO靠我能会因为返回值暴露内部存储对象地址。

调用get和set方法,修改对象属性,被修改的对象属性是一个可变的对象。

3、修改后代码

privateString[]name;

publicString[] getNSEO靠我ame() {

String[] temp =name;

returntemp;

}

二、需要处理的Bug

1、源代码

publicvoidtest(){

String abc =“abc”;

String xyz SEO靠我=newString("");

xyz = abc;

System.out.println(xyz);

}

2、Bug描述

Bug: Dead store to xyz in Findbugs.test()

ThSEO靠我is instruction assigns a value to a local variable, but the value is not read or used in any subsequSEO靠我ent instruction. Often, this indicates an error, because the value computed is never used.

Note that SEO靠我Sun’s javac compiler often generates dead stores for final local variables. Because FindBugs is a bySEO靠我tecode-based tool, there is no easy way to eliminate these false positives.

Rank: Of Concern (17),conSEO靠我fidence: Normal

Pattern: DLS_DEAD_LOCAL_STORE

Type: DLS,Category: STYLE (Dodgy code)   欺骗性代码

说明:Bug:对变量SEO靠我xyz赋值

对局部变量赋值,但是这个变量没有在后续的代码中没有使用。

确认该局部变量未使用,可去掉。

Bug: Findbugs.test() invokes inefficient new String(SEO靠我String) constructor

Using thejava.lang.String(String)constructor wastes memory because the object so SEO靠我constructed will be functionally indistinguishable from theStringpassed as a parameter.  Just use thSEO靠我e argumentStringdirectly.

Rank: Of Concern (18),confidence: Normal

Pattern: DM_STRING_CTOR

Type: Dm,CatSEO靠我egory: PERFORMANCE (Performance)  性能

说明:Bug:使用String(String)构造使效率低下

使用java.lang.String(String)构造浪费了内存,SEO靠我因为对象构造字符串作为参数传递与直接使用参数字符串功能上没有区别。

3、修改后代码

publicvoidtest(){

String abc =“abc”;

//Stringxyz= new String("SEO靠我");

String xyz = abc;

System.out.println(xyz);

}

1、源代码

publicvoidtest(){

String str =“123”;

if(str!=null){

SSEO靠我ystem.out.println(str);

}

}

2、Bug描述

Bug: Redundant nullcheck of str, which is known to be non-null in FiSEO靠我ndbugs.test()

This method contains a redundant check of a known non-null value against the constant nSEO靠我ull.

Rank: Of Concern (18),confidence: Normal

Pattern: RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE

Type: RSEO靠我CN,Category: STYLE (Dodgy code)   欺骗性代码

说明:Bug: 多余的非空检查,在Findbugs.test()已知是非空

没有必要进行if判断

3、修改后代码

publicvSEO靠我oidtest(){

Stringstr=“123”;

System.out.println(str);

}

1、Bug描述

Bug: The class name findbugs doesn’t startSEO靠我 with an upper case letter

Class names should be nouns, in mixed case with the first letter of each iSEO靠我nternal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoidSEO靠我 acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, suSEO靠我ch as URL or HTML).

Rank: Of Concern (16),confidence: Normal

Pattern: NM_CLASS_NAMING_CONVENTION

Type: SEO靠我Nm,Category: BAD_PRACTICE (Bad practice)  不好的实践

说明:类名findbugs首字母没有大写

Java中类的名称首字母需要大写

FindBugs的使用方法

来源网址SEO靠我:http://findbugs.sourceforge.net/downloads.html

FindBugs下载地址:http://findbugs.sourceforge.net/downloadSEO靠我s.html

方法一:图形界面

1、下载 FindBugs tool,打开bin目录下的FindBugs.bat文件(需要在JAVA环境下),FindBugs图形界面如下:

2、点击文件>新建,输入需要检测SEO靠我的项目信息即可。

方法二:Eclipse插件

1、在Eclipse中安装好插件后,选择需要检测的项目,右击Find bugs

FindBugs在Eclipse中的安装

来源网址:http://findbugsSEO靠我.cs.umd.edu/eclipse/

方法一:下载插件安装

1、插件下载路径:http://findbugs.sourceforge.net/downloads.html

2、下载插件解压缩后,将插件复SEO靠我制到Eclipse的plugins文件夹下,重启Eclipse即可

3、查看是否安装成功:打开Eclipse的Help>Installation Details>Plug-ins

方法二:线上安装

1、方法SEO靠我参考:http://findbugs.cs.umd.edu/eclipse/

2、打开Eclipse的Help>Install New Software…界面,点击add按钮,输入Name为FindBuSEO靠我gs,Location为http://findbugs.cs.umd.edu/eclipse,

作者:仲丽萍

链接:https://www.jianshu.com/p/9740c94c489d

来源:简书

SEO靠我作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载自请点这里

“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2