我想抑制特定字段或局部变量的FindBugs警告.FindBugs文档指出Target的edu.umd.cs.findbugs.annotations.SuppressWarning注释[1]可以是Type,Field,Method,Parameter,Constructor,Package.但是,只有当我注释警告被抑制的方法时,它才能对我进行注释.
注释整个方法似乎对我来说很广泛.有没有办法抑制特定字段的警告?还有另一个相关问题[2],但没有答案.
[1] http://findbugs.sourceforge.net/manual/annotations.html
演示代码:
public class SyncOnBoxed
{
static int counter = 0;
// The following SuppressWarnings does NOT prevent the FindBugs warning
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE")
final static Long expiringLock = new Long(System.currentTimeMillis() + 10);
public static void main(String[] args) {
while (increment(expiringLock)) {
System.out.println(counter);
}
}
// The following SuppressWarnings prevents the FindBugs warning
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE")
protected static boolean increment(Long expiringLock)
{
synchronized (expiringLock) { // <<< FindBugs warning is here: Synchronization on Long in SyncOnBoxed.increment()
counter++;
}
return …
Run Code Online (Sandbox Code Playgroud) 我一直在使用@Nonnull和@Nullable注释方法给其他程序员(和我自己!)一个方法可以返回的线索.我终于决定在一个类上实际运行Findbugs(IntelliJ - FindBugs-IDEA v1.0.1),我不明白我所看到的行为.文档也没有帮助.
假设我有以下示例代码:
import javax.annotation.Nonnull;
public class Main {
public static void main(String[] args) {
}
@Nonnull
public static String myFunc(){
return new String("foo");
}
@Nonnull
public static String myFunc2(){
return "foo";
}
}
Run Code Online (Sandbox Code Playgroud)
Findbugs将myFunc()的return语句标记为"已知为非空值的冗余nullcheck",但对myFunc2()感到满意.
是否期望findbugs看到这些不同?(赞赏文档链接)我是否完全误解了@Nonnull对方法的使用?
[编辑]
经过一些研究,我已经确定org.jetbrains @Contract注释(合同违规更改为错误)将更好地满足我的需求.谢谢Guillaume F.的帮助!
我FindBugs
用来从Ant分析Eclipse中的代码.
以下片段给出RV_RETURN_VALUE_IGNORED_BAD_PRACTICE
:
RV:方法忽略异常返回值(RV_RETURN_VALUE_IGNORED_BAD_PRACTICE)
此方法返回未检查的值.应检查返回值,因为它可以指示异常或意外的函数执行.例如,如果无法成功删除文件(而不是抛出异常),则File.delete()方法返回false.如果不检查结果,则不会注意方法调用是否通过返回非典型返回值来表示意外行为.
public void export (File file) throws IOException {
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
...
Run Code Online (Sandbox Code Playgroud)
实际上我并不关心文件存在与否,该方法应该继续执行.如果发生异常,它将被抛出export()
如何重写此代码段,以便不显示警告/错误,而不在Findbugs配置文件中禁用它?
我正在使用 FindBugs 并且此错误不断生成:
依赖默认编码:
找到了一个方法调用,该方法将执行字节到字符串(或字符串到字节)的转换,并假定默认平台编码是合适的。这将导致应用程序行为因平台而异。使用替代 API 并明确指定字符集名称或字符集对象。
我认为这与扫描仪有关,这是我的代码:
package mystack;
import java.util.*;
public class MyStack {
private int maxSize;
private int[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
public void push(int j) {
stackArray[++top] = j;
}
public int pop() {
return stackArray[top--];
}
public int peek() {
return stackArray[top];
}
public int min() {
return stackArray[0];
}
public boolean isEmpty() {
return (top == -1); …
Run Code Online (Sandbox Code Playgroud) 我通过Sonarqube在我们的代码上运行findbugs,我得到一个空指针取消引用的错误:
有一个语句分支,如果执行,则保证将取消引用空值.
错误的代码就是这样:
public static boolean isBigDecimalDifferent(BigDecimal x, BigDecimal y) {
return (x != null || y != null)
&& ((x != null && y == null) || (x == null && y != null) || x.compareTo(y) != 0);
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是怎么可能的.唯一可以实现NPE的地方是调用x.compareTo(y),但如果x = null,那么Java永远不会分析那个分支,对吗?
这是一个错误,还是我错过了Java分析这个语句的方式?
UPDATE
感谢您的投入.我最后建议他们改变它:
if (x!=null && y != null)
return x.compare(y);
else
return x!=y;
Run Code Online (Sandbox Code Playgroud)
我觉得有点清楚.如果没有人同意这个改变,我会按照建议做,只是忽略这个问题,即使我宁愿避免这个.