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

时间:2012-11-20 16:29:33

标签: java static-analysis

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

  

未选中的作业:'java.util.List'为'java.util.List'

它抱怨的代码是:

List<String> targetDocumentIds = pepperWorkflowInstance.getTargetDocumentIds();

供参考:

public class PepperWorkflowInstance<T extends PepperWorkflowInstanceData> implements Serializable {

   private List<String>            targetDocumentIds = new ArrayList<String>();
   ...
   public List<String> getTargetDocumentIds() {
      return targetDocumentIds;
   }
   ...
}

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

2 个答案:

答案 0 :(得分:1)

确保pepperWorkflowInstance有参数:

pepperWorkflowInstance = new PepperWorkflowInstance<SomeClass>();

请参阅IDEA-6254

答案 1 :(得分:0)

如果pepperWorkflowInstance是超类,其中raw类型用作返回类型, 然后那可能会产生那条消息。

实施例

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                        
    }
}