Java PMD规则数据流异常分析异常

Java PMD规则数据流异常分析异常,java,pmd,Java,Pmd,我有以下JUnit测试: @Test public void testRunLocalhost() throws IOException, InterruptedException { // Start an AnnouncerThread final AnnouncerThread announcer = new AnnouncerThread(); announcer.start(); // Create the socket and listen on t

我有以下JUnit测试:

@Test
public void testRunLocalhost() throws IOException, InterruptedException {
    // Start an AnnouncerThread
    final AnnouncerThread announcer = new AnnouncerThread();
    announcer.start();

    // Create the socket and listen on the right port.
    final DatagramSocket socket = new DatagramSocket();
    assert(socket != null);

    // Create a packet to send.
    final DatagramPacket packet = new DatagramPacket(new byte[0], 0, InetAddress.getByName(AnnouncerThread.GROUP), AnnouncerThread.PORT);
    assert(packet != null);

    // Send the packet.
    socket.send(packet);

    // Listen for the IP from the server.
    final DatagramPacket receivedPacket = new DatagramPacket(new byte[256], 256);
    socket.setSoTimeout(2000); // Only wait 2 seconds.
    socket.receive(receivedPacket);
    socket.close();

    // Get localhost's address.
    final InetAddress localhost = InetAddress.getLocalHost();
    assert(localhost != null);

    // Compare the receive IP to the localhost IP.
    final String receivedIP = new String(receivedPacket.getData());
    if (!receivedIP.startsWith(localhost.getHostAddress())) {
        fail("Received IP: '" + receivedIP + "' not the same as localhost: '" + localhost.getHostAddress() + "'");
    }

    announcer.shutdown();
    announcer.join();
}
PMD给出了以下违规行为:

Found 'UR'-anomaly for variable 'socket' (lines '36'-'36').
Found 'UR'-anomaly for variable 'localhost' (lines '36'-'36').
Found 'UR'-anomaly for variable 'packet' (lines '36'-'36').
我的文件中的第36行是定义方法的行:

public void testRunLocalhost() throws IOException, InterruptedException {

我不明白违规者在说什么。我应该在哪里定义这三个变量?为什么广播员没有在违规事件中宣读?它的声明方式与我尝试重新排序声明的方式相同,但没有任何效果。

这看起来很奇怪。有趣的是,为通过“new”分配的对象定义的所有三个变量都会出现这种情况,然后检查是否为空。我希望“new”的结果始终有效/不为null-否则应抛出
OutOfMemoryException
。我想知道这就是问题所在吗?

这看起来很奇怪。有趣的是,为通过“new”分配的对象定义的所有三个变量都会出现这种情况,然后检查是否为空。我希望“new”的结果始终有效/不为null-否则应抛出
OutOfMemoryException
。我想知道,这就是问题所在吗?

它似乎确实与您在分配完最后三个变量后立即进行的
assert
调用有关

PMD文档()说明:

UR-异常:有一个对以前未定义的变量的引用


它似乎确实与分配完最后三个变量后正在进行的
assert
调用有关

PMD文档()说明:

UR-异常:有一个对以前未定义的变量的引用


删除断言可以消除冲突。这是PMD上的错误吗?是的,
dataflowAnomalyanAnalysis
已经过时了。断言的这个特殊问题是,但仍然存在FWIW,根据,PMD计划放弃UR异常检测。删除断言消除了违规。这是PMD上的错误吗?是的,
DataflowAnomalyanAnalysis
已经过时了。这个与断言有关的特殊问题是,但仍然存在FWIW,根据PMD计划放弃UR异常检测。