FindBug使用总结

简介: FindBug使用总结

AM : Creates an empty jar file entry(AM_CREATES_EMPTY_JAR_FILE_ENTRY)

在putNextEntry()和closeEntry()之间,没有对jar文件做其他操作。这样会给jar文件生成一个空的条目。

The code calls putNextEntry(), immediately followed by a call to closeEntry(). This results in an empty JarFile entry. The contents of the entry should be written to the JarFile between the calls to putNextEntry() and closeEntry().



AM : Creates an empty zip file entry(AM_CREATES_EMPTY_ZIP_FILE_ENTRY)

在putNextEntry()和closeEntry()之间,没有对zip文件做其他操作。这样会给zip文件生成一个空的条目。

The code calls putNextEntry(), immediately followed by a call to closeEntry(). This results in an empty ZipFile entry. The contents of the entry should be written to the ZipFile between the calls to putNextEntry() and closeEntry().



BC : Equals method should not assume anything about the type of its argument (BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS)
在equals方法中,没有对参数进行类型匹配判断。改法很简单,加上:

if (!(o instanceof [当前的class]) {

return false;

}

The equals(Object o) method shouldn't make any assumptions about the type of o. It should simply return false if o is not the same type as this.



BC : Random object created and used only once(DMI_RANDOM_USED_ONLY_ONCE)

一个java.util.Random对象只使用了一次,生成了一个随机数就废弃了。正常的做法,应该把这个对象保存起来,所有需要用到随机数的地方都调用这一个对象就行了。

This code creates a java.util.Random object, uses it to generate one random number, and then discards the Random object. This produces mediocre quality random numbers and is inefficient. If possible, rewrite the code so that the Random object is created once and saved, and each time a new random number is required invoke a method on the existing Random object to obtain it.



BIT : Check for sign of bitwise operation(BIT_SIGNED_CHECK)

一个判断语句中,使用了位操作,并且进行了>0的比较。例如:

((event.detail & SWT.SELECTED) > 0)

这个判断,本意应该是两个数字的做与操作后还有非0的位数。但是,一个不小心,与操作的结果是个负数,这就是一个bug了。最好用"!="替换">0"

This method compares an expression such as

((event.detail & SWT.SELECTED) > 0)
. Using bit arithmetic and then comparing with the greater than operator can lead to unexpected results (of course depending on the value of SWT.SELECTED). If SWT.SELECTED is a negative number, this is a candidate for a bug. Even when SWT.SELECTED is not negative, it seems good practice to use '!= 0' instead of '> 0'.
Boris Bokowski



CN : Class implements Cloneable but does not define or use clone method(CN_IDIOM)

一个类实现了Cloneable接口,但是没有声明或使用到clone方法。因为clone方法是Object类的方法,所以当前类不去声明这个方法,不会编译不通过。但是,clone是需要逐个字段去复制的,所以没有声明clone方法是不对的。

Class implements Cloneable but does not define or use the clone method.



CN : clone method does not call super.clone()(CN_IDIOM_NO_SUPER_CALL)

非final的类,定义了clone()方法,却在方法中没有调用super.clone()。

看上去,不应该调用super.clone()。如果A是B的父类,那么B调用super.clone(),则B的clone方法返回的是A的实例,看上去是错的。

但是,如果所有的clone()方法都调用了super.clone(),则最终调用的是Object.clone(),那就能返回正确的类型。

This non-final class defines a clone() method that does not call super.clone(). If this class ("A") is extended by a subclass ("B"), and the subclass B calls super.clone(), then it is likely that B's clone() method will return an object of type A, which violates the standard contract for clone().

If all clone() methods call super.clone(), then they are guaranteed to use Object.clone(), which always returns an object of the correct type.



CN : Class defines clone() but doesn't implement Cloneable (CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE)

类定义了clone()方法,但是没有声明实现Cloneable接口。这个不是个什么大问题,只是确认一下是不是漏了声明。

This class defines a clone() method but the class doesn't implement Cloneable. There are some situations in which this is OK (e.g., you want to control how subclasses can clone themselves), but just make sure that this is what you intended.



Co : Abstract class defines covariant compareTo() method(CO_ABSTRACT_SELF)

类定义了一个compareTo()方法,其参数不是Object类型。要正确的实现Comparable接口的compareTo()方法,最好的做法就是,compareTo()方法的参数就是Object类。

This class defines a covariant version of compareTo(). To correctly override the compareTo() method in the Comparable interface, the parameter of compareTo() must have type java.lang.Object.



Co : Covariant compareTo() method defined(CO_SELF_NO_OBJECT)

同上

This class defines a covariant version of compareTo(). To correctly override the compareTo() method in the Comparable interface, the parameter of compareTo() must have type java.lang.Object.



DE : Method might drop exception(DE_MIGHT_DROP)

这个方法可能放弃了异常。正常的做法,异常应该被处理或者通过某种方式被报告,或者再扔给外层。

This method might drop an exception. In general, exceptions should be handled or reported in some way, or they should be thrown out of the method.



DE : Method might ignore exception(DE_MIGHT_IGNORE)

这个方法可能忽略了异常。正常的做法,异常应该被处理或者通过某种方式被报告,或者再扔给外层。

This method might ignore an exception. In general, exceptions should be handled or reported in some way, or they should be thrown out of the method.



DMI : Don't use removeAll to clear a collection(DMI_USING_REMOVEALL_TO_CLEAR_COLLECTION)

如果你想把集合内的所有的元素都删除掉,请用集合的clear方法,而不是c.removeAll( c )方法。调用c.removeAll( c )去清空集合,会清除的不干净,容易产生错误,可能会抛出ConcurrentModificationException异常。

If you want to remove all elements from a collection c, use c.clear, not c.removeAll(c). Calling c.removeAll(c) to clear a collection is less clear, susceptible to errors from typos, less efficient and for some collections, might throw a ConcurrentModificationException.



DP : Classloaders should only be created inside doPrivileged block (DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED)

这段代码写了一个需要安全管理器的classloader。如果代码需要被授权为安全权限,但是可能被不安全的代码去调用,那么classloader就需要放在doPrivileged块内。

This code creates a classloader, which requires a security manager. If this code will be granted security permissions, but might be invoked by code that does not have security permissions, then the classloader creation needs to occur inside a doPrivileged block.



DP : Method invoked that should be only be invoked inside a doPrivileged block (DP_DO_INSIDE_DO_PRIVILEGED)

代码调用了一个需要安全权限检查的方法。如果代码需要被授权为安全权限,但是可能被不安全的代码去调用,那么classloader就需要放在doPrivileged块内。

This code invokes a method that requires a security permission check. If this code will be granted security permissions, but might be invoked by code that does not have security permissions, then the invocation needs to occur inside a doPrivileged block.



Dm : Method invokes System.exit(...)(DM_EXIT)

调用了System.exit去关闭虚拟机进程。只能在适当的时候这么用。这种调用会让你的代码很难甚至不可能被其他代码调用。用扔出RuntimeException异常来代替会比较好。

Invoking System.exit shuts down the entire Java virtual machine. This should only been done when it is appropriate. Such calls make it hard or impossible for your code to be invoked by other code. Consider throwing a RuntimeException instead.



Dm : Method invokes dangerous method runFinalizersOnExit(DM_RUN_FINALIZERS_ON_EXIT)

永远不要以任何理由调用System.runFinalizersOnExit 或者Runtime.runFinalizersOnExit,在java包里面,他们是非常危险的方法。

-- Java教父Joshua Bloch

Never call System.runFinalizersOnExit or Runtime.runFinalizersOnExit for any reason: they are among the most dangerous methods in the Java libraries. -- Joshua Bloch



ES : Comparison of String parameter using == or !=(ES_COMPARING_PARAMETER_STRING_WITH_EQ)

这段代码用 == 或者 != 来比较字符串。这种方式去比较字符串,并不是比较字符串的内容相同,而是比较是不是同一个对象。用equals方法来代替这种比较。

This code compares a java.lang.String parameter for reference equality using the == or != operators. Requiring callers to pass only String constants or interned strings to a method is unnecessarily fragile, and rarely leads to measurable performance gains. Consider using the equals(Object) method instead.



ES : Comparison of String objects using == or !=(ES_COMPARING_STRINGS_WITH_EQ)

同上

This code compares java.lang.String objects for reference equality using the == or != operators. Unless both strings are either constants in a source file, or have been interned using the String.intern() method, the same string value may be represented by two different String objects. Consider using the equals(Object) method instead.



Eq : Abstract class defines covariant equals() method(EQ_ABSTRACT_SELF)

这个类定义了equals()方法,但是参数却是Object的子类。正确覆盖equals()方法,参数必须是Object

This class defines a covariant version of equals(). To correctly override the equals() method in java.lang.Object, the parameter of equals() must have type java.lang.Object.



Eq : Equals checks for noncompatible operand (EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS)

equals方法内,对参数的类型检查的时候,检查了除了本身之外的其他类型。如:

public boolean equals(Object o) {

if (o instanceof Foo)

return name.equals(((Foo)o).name);

else if (o instanceof String)

return name.equals(o);

else return false;

这种写法是不好的习惯,它会让代码难以理解和迁移。

This equals method is checking to see if the argument is some incompatible type (i.e., a class that is neither a supertype nor subtype of the class that defines the equals method). For example, the Foo class might have an equals method that looks like:


public boolean equals(Object o) {
if (o instanceof Foo)
return name.equals(((Foo)o).name);
else if (o instanceof String)
return name.equals(o);
else return false;
This is considered bad practice, as it makes it very hard to implement an equals method that is symmetric and transitive. Without those properties, very unexpected behavoirs are possible.



Eq : Class defines compareTo(...) and uses Object.equals()(EQ_COMPARETO_USE_OBJECT_EQUALS)

这个类定义了compareTo(…)方法,但是却直接继承Object的equals()方法。通常,compareTo方法在并且只有在equals方法返回ture的时候返回0。如果没有遵守这个原则,就会出现一些奇怪和不可预测的问题。在java5中,PriorityQueue.remove方法是用了compareTo方法,但是java6中它用的却是equals方法。

不用多说了。

This class defines a compareTo(...) method but inherits its equals() method from java.lang.Object. Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue. In Java 5 the PriorityQueue.remove method uses the compareTo method, while in Java 6 it uses the equals method.
From the JavaDoc for the compareTo method in the Comparable interface:

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."



Eq : equals method fails for subtypes(EQ_GETCLASS_AND_CLASS_CONSTANT)

这个类写了自己的equals方法,但是这个方法在比较参数的对象类型的时候被定义的不可继承,如果有子类继承了这个类,那么就会出错。例如,Foo类的检查是:

if (Foo.class == o.getClass())。最好改成:

if (this.getClass() == o.getClass())

This class has an equals method that will be broken if it is inherited by subclasses. It compares a class literal with the class of the argument (e.g., in class Foo it might check if Foo.class == o.getClass()). It is better to check if this.getClass() == o.getClass().



Eq : Covariant equals() method defined(EQ_SELF_NO_OBJECT)

equals()方法的参数,最好就是Object。如果不是,会容易出问题。

This class defines a covariant version of equals(). To correctly override the equals() method in java.lang.Object, the parameter of equals() must have type java.lang.Object.

目录
相关文章
|
缓存 移动开发 网络协议
WebSocket 协议原理抓包分析
WebSocket 协议原理抓包分析
525 0
|
6月前
|
SQL Oracle 关系型数据库
实时计算 Flink版产品使用合集之Managed Memory内存的含义是什么
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
5月前
|
人工智能 算法 Java
解锁LLMs的“思考”能力:Chain-of-Thought(CoT) 技术推动复杂推理的新发展
解锁LLMs的“思考”能力:Chain-of-Thought(CoT) 技术推动复杂推理的新发展
|
6月前
|
分布式计算 API Spark
Spline部署&测试
Spline是Spark的元数据管理和血缘追踪工具,通过Docke部署。安装涉及下载docker-compose.yml和.env文件,使用`docker compose up -d`命令启动,包括rest-server(核心,处理血缘数据并存储在ArangoDB)、arangodb(多模型数据库)、ui(Web服务)等组件。测试中使用pyspark进行血缘捕获,通过spark-submit命令指定Spline相关依赖并连接到Spline服务器。成功后,血缘数据可在Spline UI中查看。未来计划在DolphinScheduler上测试Spark SQL任务并启用血缘追踪。
255 0
|
12月前
|
网络协议 安全 Linux
深入解析HTTP请求:了解请求特征与报文格式的关键秘密
这篇文章将带您深入了解HTTP请求的特征和报文格式。HTTP作为一种简单、灵活且易于扩展的协议,适用于各种操作系统和设备。我们还将探讨持久性连接如何提高请求的效率。了解HTTP报文的构成,包括起始行、头部字段和消息正文,将帮助您更好地理解HTTP的工作原理。无论您是初学者还是已经有一定了解的读者,本文都将为您提供全面的HTTP知识。
346 1
深入解析HTTP请求:了解请求特征与报文格式的关键秘密
|
12月前
|
Java API Maven
Gradle使用总结
Gradle使用总结
247 0
|
12月前
|
负载均衡 网络安全 微服务
谈谈用统一网关gate的利与弊
谈谈用统一网关gate的利与弊
102 0
|
12月前
|
JavaScript 前端开发 安全
JavaScript安全性分析:了解常见的Web安全问题和防范攻击手段
JavaScript安全性分析:了解常见的Web安全问题和防范攻击手段
|
弹性计算 人工智能 自然语言处理
【玩转AIGC系列】AIGC文本生成视频
本文介绍如何使用GPU云服务器搭建Stable Diffusion模型,并基于ModelScope框架,实现使用文本生成视频。
【玩转AIGC系列】AIGC文本生成视频
|
SQL 前端开发 Java
领域驱动系列-浅析VO、DTO、DO、PO
领域驱动系列-浅析VO、DTO、DO、PO
679 0