标签: findbugs

Eclipse中的Findbugs ArrayIndexOutOfBounds运行时出现异常

我刚刚在Eclipse Juno中安装(并重新安装)了Findbugs,每当我尝试在任何项目中运行它时,我都会收到以下错误:

An internal error occurred during: "Finding bugs in <project name>...".
java.lang.ArrayIndexOutOfBoundsException
Run Code Online (Sandbox Code Playgroud)

谷歌搜索没有提供任何有用的结果.有任何想法吗?

编辑:我尝试(并且可能失败)在Eclipse中安装Java 8的测试版本来试用Java 8的lambdas和其他功能.也许它与Findbugs无法正常运行有关?

java findbugs java-8

26
推荐指数
1
解决办法
1万
查看次数

运行声纳转轮时超出GC开销限制

我在对项目进行声纳分析时遇到OutOfMemoryException.下面是堆栈跟踪:

14:55:55.433 DEBUG - Release semaphore on project : org.sonar.api.resources.Project@5a7b5cb8[id=1,key=myProj_web,qualifier=TRK], with key batch-myProj_web
14:55:55.711 DEBUG - To prevent a memory leak, the JDBC Driver [com.mysql.jdbc.Driver] has been forcibly deregistered

INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 12:48.979s
Final Memory: 33M/910M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
org.sonar.runner.impl.RunnerException: Unable to execute Sonar
        at org.sonar.runner.impl.BatchLauncher$1.delegateExecution(BatchLauncher.java:91)
        at org.sonar.runner.impl.BatchLauncher$1.run(BatchLauncher.java:75)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.sonar.runner.impl.BatchLauncher.doExecute(BatchLauncher.java:69)
        at org.sonar.runner.impl.BatchLauncher.execute(BatchLauncher.java:50)
        at org.sonar.runner.api.EmbeddedRunner.doExecute(EmbeddedRunner.java:102)
        at org.sonar.runner.api.Runner.execute(Runner.java:100)
        at org.sonar.runner.Main.executeTask(Main.java:70)
        at org.sonar.runner.Main.execute(Main.java:59)
        at org.sonar.runner.Main.main(Main.java:53)
Caused by: org.sonar.api.utils.SonarException: Can …
Run Code Online (Sandbox Code Playgroud)

java garbage-collection memory-management findbugs sonarqube

26
推荐指数
1
解决办法
2万
查看次数

在什么情况下,空的同步块可以实现正确的线程语义?

我正在查看关于我的代码库的Findbugs报告,其中一个触发的模式是空synchronzied块(即synchronized (var) {}).该文件说:

与大多数人认识到的相比,空的同步块更加微妙且难以正确使用,并且空的同步块几乎不是比较少设计的解决方案更好的解决方案.

在我的情况下,它发生是因为块的内容已被注释掉,但synchronized声明仍然存在.在什么情况下空synchronized块可以实现正确的线程语义?

java multithreading findbugs synchronized

25
推荐指数
3
解决办法
4541
查看次数

切换不间断

我有一些switch语句,如下所示.请注意,没有休息.Findbugs仅在第二个案例陈述中报告错误.错误是:在一个案例落入下一个案例的地方找到切换语句.

switch(x) {

    case 0:
        // code

    case 1:
        // code

    case 2:
        // code
}
Run Code Online (Sandbox Code Playgroud)

java findbugs switch-statement

24
推荐指数
3
解决办法
6万
查看次数

如何抑制字段或局部变量的FindBugs警告

我想抑制特定字段或局部变量的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

[2] 在Eclipse中抑制FindBugs警告

演示代码:

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)

java findbugs suppress-warnings

24
推荐指数
1
解决办法
3万
查看次数

使用Java将内部类设置为静态有什么好处?

我的Java类中有一个内部类.

在此输入图像描述

当我运行查找错误时,它建议(警告)使其成为静态.

在此输入图像描述

这个警告有什么意义?将内部类设为静态有什么好处?

java findbugs class

24
推荐指数
4
解决办法
1万
查看次数

依赖于默认编码,我应该使用什么以及为什么?

FindBugs报告了一个错误:

依赖于默认编码找到对将执行字节到String(或String to byte)转换的方法的调用,并假设默认平台编码是合适的.这将导致应用程序行为在平台之间变化.使用备用API并显式指定charset名称或Charset对象.

我像这样使用FileReader(只是一段代码):

public ArrayList<String> getValuesFromFile(File file){
    String line;
    StringTokenizer token;
    ArrayList<String> list = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(file));
        list = new ArrayList<String>();
        while ((line = br.readLine())!=null){
            token = new StringTokenizer(line);
            token.nextToken();
            list.add(token.nextToken());
    ...
Run Code Online (Sandbox Code Playgroud)

要纠正我需要改变的错误

br = new BufferedReader(new FileReader(file));
Run Code Online (Sandbox Code Playgroud)

br = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.defaultCharset()));
Run Code Online (Sandbox Code Playgroud)

当我使用PrintWriter时,发生了同样的错误.所以现在我有一个问题.当我可以(应该)使用FileReader和PrintWriter时,如果不是很好的做法依赖于默认编码?第二个问题是正确使用Charset.defaultCharset()?我决定使用这种方法自动定义用户操作系统的字符集.

java encoding findbugs internationalization

23
推荐指数
2
解决办法
3万
查看次数

如何在默认情况下指示成员字段是@Nonnull?

我的问题是后续这一块.

在过去的FindBugs版本中,可以使用@DefaultAnnotation(Nonnull.class)@DefaultAnnotationForFields(Nonnull.class)表示应将包中的所有字段视为@Nonnull.在FindBugs的(2.0)的当前版本@DefaultAnnotation@DefaultAnnotationForFields已被弃用,并且我们都应该使用JSR-305来代替.但JSR-305似乎并未涵盖(现已弃用)FindBugs注释所涵盖的所有内容.

javadoc的确实表明了一些替代品:

  • @ParametersAreNonnullByDefault.这(显然)仅适用于参数,而不适用于成员字段.
  • @CheckReturnValue,当应用于类型或包时.同样,这不适用于成员字段.
  • @TypeQualifierDefault.也许这可以做我想要的,但我不明白它是如何工作的,除了一些神秘的javadoc之外,我无法找到任何有关其用法或意图的文档或示例.我认为这将有助于我创建自己的注释,但我能确定所有工具(FindBugs,Eclipse等)都能正确地解释这个新注释(甚至根本不能)吗?

javadoc中没有提供关于如何处理其弃用任何提示.

因此,使用当前版本的FindBugs和/或JSR-305,我应该如何表明特定包中(甚至某个类)中的所有成员字段都应该被视为@Nonnull?它甚至可能吗?

java findbugs jsr305

21
推荐指数
1
解决办法
4284
查看次数

要使用@SuppressFBWarnings导入什么?

要导入什么来使用SuppressFBWarnings?我通过help/install新软件安装了findbugs插件当我输入import edu.时,我无法通过ctrl空间来获取选项.

try {
  String t = null;
  @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
    value="NP_ALWAYS_NULL", 
    justification="I know what I'm doing")
  int sl = t.length();
  System.out.printf( "Length is %d", sl );
} catch (Throwable e) {
...
}
Run Code Online (Sandbox Code Playgroud)

有错误"edu无法解析为某种类型"

java findbugs include spotbugs

21
推荐指数
1
解决办法
1万
查看次数

为什么定义协变compareTo方法被认为是不好的做法?

这是我的代码中的一个例子:

基类:

abstract class AbstractBase implements Comparable<AbstractBase> {
    private int a;
    private int b;

    public int compareTo(AbstractBase other) {
        // compare using a and b
    }
}
Run Code Online (Sandbox Code Playgroud)

执行:

class Impl extends AbstractBase {
private int c;

public int compareTo(Impl other) {
    // compare using a, b and c with c having higher impact than b in AbstractBase
}
Run Code Online (Sandbox Code Playgroud)

FindBugs将此报告为一个问题.但那是为什么呢?怎么会发生什么?

我将如何正确实施解决方案?

java findbugs

20
推荐指数
1
解决办法
1555
查看次数