运行 NullAway 分析器,但在 NullAway 初始化时遇到断言错误

问题描述 投票:0回答:1

我正在尝试使用 NullAway 检测 Java 文件中的空指针异常。但是,由于与其初始化相关的断言错误,NullAway 无法运行:

java.lang.AssertionError: com.google.errorprone.scanner.ErrorProneInjector$ProvisionException: Failed to initialize com.uber.nullaway.NullAway
        at com.google.errorprone.ErrorProneAnalyzer.lambda$scansPlugins$0(ErrorProneAnalyzer.java:85)
        at com.google.common.base.Suppliers$NonSerializableMemoizingSupplier.get(Suppliers.java:181)
        at com.google.errorprone.ErrorProneAnalyzer.finished(ErrorProneAnalyzer.java:156)
        at jdk.compiler/com.sun.tools.javac.api.MultiTaskListener.finished(MultiTaskListener.java:133)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1436)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1383)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:963)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:319)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:178)
        at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
        at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
Caused by: com.google.errorprone.scanner.ErrorProneInjector$ProvisionException: Failed to initialize com.uber.nullaway.NullAway
        at com.google.errorprone.scanner.ErrorProneInjector.getInstance(ErrorProneInjector.java:92)
        at com.google.errorprone.scanner.ErrorProneInjector.getInstance(ErrorProneInjector.java:65)
        at com.google.errorprone.scanner.ScannerSupplierImpl.instantiateChecker(ScannerSupplierImpl.java:66)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
        at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:1024)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
        at com.google.errorprone.scanner.ScannerSupplierImpl.get(ScannerSupplierImpl.java:74)
        at com.google.errorprone.scanner.ScannerSupplierImpl.get(ScannerSupplierImpl.java:37)
        at com.google.errorprone.ErrorProneAnalyzer.lambda$scansPlugins$0(ErrorProneAnalyzer.java:80)
        ... 10 more
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:74)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
        at com.google.errorprone.scanner.ErrorProneInjector.getInstance(ErrorProneInjector.java:90)
        ... 22 more
Caused by: java.lang.NoClassDefFoundError: org/checkerframework/nullaway/dataflow/cfg/node/NodeVisitor
        at com.uber.nullaway.handlers.Handlers.buildDefault(Handlers.java:67)
        at com.uber.nullaway.NullAway.<init>(NullAway.java:291)
        at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
        ... 25 more
Caused by: java.lang.ClassNotFoundException: org.checkerframework.nullaway.dataflow.cfg.node.NodeVisitor
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:593)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
        ... 28 more

下面是我用来执行 NullAway 的脚本。我按照 Error Prone 的说明进行操作,并根据 NullAway 文档进行了一些修改:

EP_VERSION=2.23.0
DATAFLOW_VERSION=3.39.0-eisop1
NULLAWAY_VERSION=0.10.15
JSPECIFY_VERSION=1.0.0

# check if downloaded, if not, download
error_prone_core_jar=error_prone_core-${EP_VERSION?}-with-dependencies.jar
dataflow_errorprone_jar=dataflow-errorprone-${DATAFLOW_VERSION?}.jar
nullaway_jar=nullaway-${NULLAWAY_VERSION?}.jar
jspecify_jar=jspecify-${JSPECIFY_VERSION?}.jar

if [ ! -f $error_prone_core_jar ]; then
    wget https://repo1.maven.org/maven2/com/google/errorprone/error_prone_core/${EP_VERSION?}/$error_prone_core_jar
fi
if [ ! -f $dataflow_errorprone_jar ]; then
    wget https://repo1.maven.org/maven2/io/github/eisop/dataflow-errorprone/${DATAFLOW_VERSION?}/$dataflow_errorprone_jar
fi
if [ ! -f $nullaway_jar ]; then
    wget https://repo1.maven.org/maven2/com/uber/nullaway/nullaway/${NULLAWAY_VERSION?}/$nullaway_jar
fi
if [ ! -f $jspecify_jar ]; then
    wget https://repo1.maven.org/maven2/org/jspecify/jspecify/${JSPECIFY_VERSION?}/$jspecify_jar
fi

echo "Jar files downloaded"

javac \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
  -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
  -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
  -XDcompilePolicy=simple \
  -cp .:$jspecify_jar \
  -processorpath $error_prone_core_jar:$nullaway_jar:$dataflow_errorprone_jar \
  '-Xplugin:ErrorProne -XepOpt:NullAway:AnnotatedPackages=com.uber -Xep:CollectionIncompatibleType:ERROR' \
  com/uber/mylib/ShortSet.java

echo "Compilation finished"

“com/uber/mylib/ShortSet.java”的来源与 NullAway 提供的 示例相同

package com.uber.mylib;

import org.jspecify.annotations.Nullable;

/**
 * A sample class.
 */
public class ShortSet {

  static void log(Object x) {
    System.out.println(x.toString());
  }

  static void foo() {
    log(null);
  }
}

我的java版本是:

$ java --version
openjdk 21.0.4 2024-07-16
OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu222.04, mixed mode, sharing)

我正在寻求帮助以了解为什么 NullAway 无法成功初始化。谢谢您的帮助!

NullAway可以成功运行并检测代码中的NPE。

java nullpointerexception errorprone nullaway
1个回答
0
投票

您需要添加

<dependency>
    <groupId>org.checkerframework</groupId>
    <artifactId>dataflow-nullaway</artifactId>
</dependency>

到你的类路径或处理器路径(我不确定是哪一个)。

© www.soinside.com 2019 - 2024. All rights reserved.