2014-09-23 62 views
0

下面的代碼用於javax.activation.DataHandler.getDataContentHandler,1.41,07/05/14。dataContentHandler成員變量被覆蓋在javax.activation.DataHandler.getDataContentHandler中?

如果在調用方法時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

我缺少的東西,或者是不太可能預期的行爲?

貌似是每個註釋預期的行爲:

// getDataContentHandler總是利用這些「包裝」處理 //以確保它返回一些有意義的事情......

如果仔細觀察代碼,'dataContentHandler'作爲參數傳遞給'new DataSourceDataContentHandler'和'new ObjectDataContentHandler'。這是評論中提到的'包裝'。

+0

啊,是的,謝謝,我錯過了。 – 2015-03-05 09:47:21