2016-01-15 33 views
1

以下代碼導致錯誤肯定:在Java 3.9中「!flag始終爲真」。我現在更新了示例代碼。 Sonar說stilvallid總是假的。java插件3.9 false positive - 始終驗證爲true(squid:S2583)

public class Test { 

public static void main(String[] args) { 
    List<Entity> entities = new ArrayList<>(); 
    entities.add(new Entity()); 
    new Test().isMessageInThePast(entities); 
} 

public boolean isMessageInThePast(List<Entity> entities) { 
    Boolean stillValid = false; 
    Date now = new Date(); 
    for (Entity situation : entities) { 
     List<Record> situationRecordList = situation.getRecords(); 
     for (Record situationRecord : situationRecordList) { 
      if (situationRecord.getOverallEndTime().after(now)) { 
       stillValid = true; 
      } 
     } 
    } 
    if (stillValid) { 
     System.out.println("..."); 
    } 
    return !stillValid; 
} 

static class Entity { 
    List<Record> records = new ArrayList<>(); 

    Entity() { 
     records.add(new Record()); 
    } 

    public List<Record> getRecords() { return records; } 
} 

static class Record { 
    Date endTime = new Date(new Date().getTime()+60000); 
    public Date getOverallEndTime() { return endTime; } 
} 
+0

我猜的片段是不完整的,複製粘貼到一個方法,這將不會引發問題。這段代碼很可能被try/catch包圍了,這個代碼有更多的上下文嗎? (理想的是整個方法) – benzonico

+0

第二種情況實際上是正確的:lastUpdate!= null不是必須的,因爲||運算符短路: 如果lastupdate == null爲true,則if執行的不是右側的部分。如果lastupdate == null爲false,則lastupdate與null不同,所以lastupdate!= null始終爲真! – benzonico

+0

是的。我正在看整個表情。關於第一個:沒有嘗試抓住。我會盡力獲得更多信息。 –

回答