在javax.activation.DataHandler.getDataContentHandler中覆盖dataContentHandler成员变量?

时间:2014-09-23 14:07:54

标签: java javax.mail javax.activation

以下代码适用于javax.activation.DataHandler.getDataContentHandler,1.41,07 / 05/14。

如果在调用方法时dataContentHandler成员变量为null,则它由[1]处的'if'子句设置(这在我的程序中会发生)。

然后立即在[2]的if子句中被覆盖。

我错过了什么,或者这不太可能是预期的行为?

   private synchronized DataContentHandler getDataContentHandler() {

     // make sure the factory didn't change
     if (factory != oldFactory) {
         oldFactory = factory;
         factoryDCH = null;
         dataContentHandler = null;
         transferFlavors = emptyFlavors;
     }

     if (dataContentHandler != null)
         return dataContentHandler;

     String simpleMT = getBaseType();

     if (factoryDCH == null && factory != null)
         factoryDCH = factory.createDataContentHandler(simpleMT);

     if (factoryDCH != null)
         dataContentHandler = factoryDCH;

     if (dataContentHandler == null) { // [1]
         if (dataSource != null)
         dataContentHandler = getCommandMap().
                createDataContentHandler(simpleMT, dataSource);
         else
         dataContentHandler = getCommandMap().
                createDataContentHandler(simpleMT);
     }

     // getDataContentHandler always uses these 'wrapper' handlers
     // to make sure it returns SOMETHING meaningful...
     if (dataSource != null) // [2]
         dataContentHandler = new DataSourceDataContentHandler(
                                  dataContentHandler,
                          dataSource);
     else
         dataContentHandler = new ObjectDataContentHandler(
                          dataContentHandler,
                          object,
                          objectMimeType);
     return dataContentHandler;
     }

1 个答案:

答案 0 :(得分:1)

  

我错过了什么,或者这不太可能是预期的行为?

看起来这是评论的预期行为:

  

// getDataContentHandler总是使用这些“包装器”处理程序    //确保它返回有意义的SOMETHING ......

如果仔细查看代码,'dataContentHandler'将作为参数传递给'new DataSourceDataContentHandler'和'new ObjectDataContentHandler'。这就是评论中提到的“包装”。