为什么从一种类型到同一种类型的分配需要检查?

Dan*_*umb 6 java static-analysis

我在类上运行IntelliJ的代码分析器(IntelliJ 11.1.4)并收到此警告:

未选中的赋值:'java.util.List'到'java.util.List'

它抱怨的代码是:

List<String> targetDocumentIds = pepperWorkflowInstance.getTargetDocumentIds();
Run Code Online (Sandbox Code Playgroud)

以供参考:

public class PepperWorkflowInstance<T extends PepperWorkflowInstanceData> implements Serializable {

   private List<String>            targetDocumentIds = new ArrayList<String>();
   ...
   public List<String> getTargetDocumentIds() {
      return targetDocumentIds;
   }
   ...
}
Run Code Online (Sandbox Code Playgroud)

所以类型匹配......那么为什么我需要"检查"作业呢?

小智 0

如果pepperWorkflowInstance 是超类,其中原始类型用作返回类型,则可能会生成该消息。

例子。

class A{
    public List getTargetDocumentIds(){
        return new ArrayList();
    }
}

class B extends A{
    public List<String> getTargetDocumentIds(){
        return new ArrayList<String>();
    }
}

public class Tester {

    public static void main(String[] args) {
        A a = new B();
        List<String> targetDocumentIds = a.getTargetDocumentIds(); 
        // above produces compiler type safety warning                        
    }
}
Run Code Online (Sandbox Code Playgroud)