]> source.dussan.org Git - poi.git/commitdiff
FindBugs fix
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 16 Feb 2014 19:35:16 +0000 (19:35 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 16 Feb 2014 19:35:16 +0000 (19:35 +0000)
- fixed "equals() method does not check for null argument"
- see http://findbugs.sourceforge.net/bugDescriptions.html#NP_EQUALS_SHOULD_HANDLE_NULL_ARGUMENT

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1568812 13f79535-47bb-0310-9956-ffa450edef68

15 files changed:
src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java
src/scratchpad/src/org/apache/poi/hwpf/model/Ffn.java
src/scratchpad/src/org/apache/poi/hwpf/model/FontTable.java
src/scratchpad/src/org/apache/poi/hwpf/model/ParagraphHeight.java
src/scratchpad/src/org/apache/poi/hwpf/model/PropertyNode.java
src/scratchpad/src/org/apache/poi/hwpf/model/SectionDescriptor.java
src/scratchpad/src/org/apache/poi/hwpf/model/StyleSheet.java
src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java
src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java
src/scratchpad/src/org/apache/poi/hwpf/model/UPX.java
src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/BorderCode.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/DateAndTime.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/LineSpacingDescriptor.java

index de889d9df1117a8d6d3d9ca7f52d0298e7a8a9b7..7f4ee8ce33753c337e27f08ba8d72fc1de1b4b96 100644 (file)
@@ -284,7 +284,7 @@ public class POIXMLDocumentPart {
 
     @Override
     public String toString(){
-        return packagePart == null ? null : packagePart.toString();
+        return packagePart == null ? "" : packagePart.toString();
     }
 
     /**
index c6d2bd70583a7b725fcd8bbd530a2b18c4c69fd3..3aca24f36ca610f20cd88ab303a66b275d50c1e3 100644 (file)
@@ -170,7 +170,7 @@ public final class TestPOIXMLDocument extends TestCase {
         assertNull(part.getRelationId(null));
         assertFalse(part.removeRelation(null, true));
         part.removeRelation(null);
-        assertNull(part.toString());
+        assertEquals("",part.toString());
         part.onDocumentCreate();
         //part.getTargetPart(null);
     }
index 6c8666f7aa50eee0457eff5db946f883ff293ac6..4f469dad09d9b0cc94fc890b4e9413b5dcc21962 100644 (file)
@@ -182,49 +182,20 @@ public final class Ffn
   }
 
   @Override
-  public boolean equals(Object o)
-    {
-    boolean retVal = true;
-
-    if (((Ffn)o).get_cbFfnM1() == _cbFfnM1)
-    {
-      if(((Ffn)o)._info == _info)
-      {
-      if(((Ffn)o)._wWeight == _wWeight)
-      {
-        if(((Ffn)o)._chs == _chs)
-        {
-        if(((Ffn)o)._ixchSzAlt == _ixchSzAlt)
-        {
-          if(Arrays.equals(((Ffn)o)._panose,_panose))
-          {
-          if(Arrays.equals(((Ffn)o)._fontSig,_fontSig))
-          {
-                  if(!(Arrays.equals(((Ffn)o)._xszFfn,_xszFfn)))
-                    retVal = false;
-          }
-          else
-            retVal = false;
-          }
-          else
-          retVal = false;
-        }
-        else
-          retVal = false;
-        }
-        else
-        retVal = false;
-      }
-      else
-        retVal = false;
-      }
-      else
-      retVal = false;
-    }
-    else
-      retVal = false;
-
-    return retVal;
+  public boolean equals(Object other) {
+      if (!(other instanceof Ffn)) return false;
+      Ffn o = (Ffn)other;
+      
+      return (
+             o._cbFfnM1 == this._cbFfnM1
+          && o._info == this._info
+          && o._wWeight == _wWeight
+          && o._chs == _chs
+          && o._ixchSzAlt == _ixchSzAlt
+          && Arrays.equals(o._panose,_panose)
+          && Arrays.equals(o._fontSig,_fontSig)
+          && Arrays.equals(o._xszFfn,_xszFfn)
+      );
   }
 
 
index 0abd236a0766ff8b3ee7f1d68f64864304b91625..bedf1e59fed79d77e9d3ed0d8b229060034e61f1 100644 (file)
@@ -138,29 +138,20 @@ public final class FontTable
   }
 
   @Override
-  public boolean equals(Object o)
-  {
-       boolean retVal = true;
-
-    if(((FontTable)o).getStringCount() == _stringCount)
-    {
-      if(((FontTable)o).getExtraDataSz() == _extraDataSz)
-      {
-        Ffn[] fontNamesNew = ((FontTable)o).getFontNames();
-        for(int i = 0;i<_stringCount; i++)
-        {
-          if(!(_fontNames[i].equals(fontNamesNew[i])))
-            retVal = false;
-        }
+  public boolean equals(Object other) {
+      if (!(other instanceof FontTable)) return false;
+      FontTable o = (FontTable)other;
+
+      if (o._stringCount != this._stringCount
+          || o._extraDataSz != this._extraDataSz
+          || o._fontNames.length != this._fontNames.length
+      ) return false;
+      
+      for (int i=0; i<o._fontNames.length; i++) {
+          if (!o._fontNames[i].equals(this._fontNames[i])) return false;
       }
-      else
-        retVal = false;
-    }
-    else
-           retVal = false;
-
-
-         return retVal;
+      
+      return true;
   }
 
   @Override
index 8cff9a5c6437a654dc35a247d186e502c0be6c6f..747d0c137528d96611a3359888ab4794828f49c2 100644 (file)
@@ -76,6 +76,7 @@ public final class ParagraphHeight
 
   public boolean equals(Object o)
   {
+    if (!(o instanceof ParagraphHeight)) return false;
     ParagraphHeight ph = (ParagraphHeight)o;
 
     return infoField == ph.infoField && reserved == ph.reserved &&
index 74b77bf54e2a83944807f90e36da393324d3c886..08dbf9e85da0275faa9940fb79d7b007a2de0efb 100644 (file)
@@ -163,6 +163,8 @@ public abstract class PropertyNode<T extends PropertyNode<T>>  implements Compar
 
   public boolean equals(Object o)
   {
+    if (!(o instanceof PropertyNode)) return false;
+
     if (limitsAreEqual(o))
     {
       Object testBuf = ((PropertyNode<?>)o)._buf;
index bdddac7f3e6cadefa60ec586fb874591b1a6ebd1..fead9ebcc704c5046f6ed12db70db4d9fe95c985 100644 (file)
@@ -80,6 +80,7 @@ public final class SectionDescriptor
   @Override
   public boolean equals(Object o)
   {
+    if (!(o instanceof SectionDescriptor)) return false;
     SectionDescriptor sed = (SectionDescriptor)o;
     return sed.fn == fn && sed.fnMpr == fnMpr;
   }
index 98df05f956c92346c6ce859a45a950a7c3cbb88d..e780a486afc5afc584a703135241fa59c9567f69 100644 (file)
@@ -178,28 +178,22 @@ public final class StyleSheet implements HDFType {
   @Override
   public boolean equals(Object o)
   {
+    if (!(o instanceof StyleSheet)) return false;
     StyleSheet ss = (StyleSheet)o;
 
-    if (ss._stshif.equals( this._stshif ) && ss._cbStshi == _cbStshi)
-    {
-      if (ss._styleDescriptions.length == _styleDescriptions.length)
-      {
-        for (int x = 0; x < _styleDescriptions.length; x++)
-        {
-          // check for null
-          if (ss._styleDescriptions[x] != _styleDescriptions[x])
-          {
-            // check for equality
-            if (!ss._styleDescriptions[x].equals(_styleDescriptions[x]))
-            {
-              return false;
-            }
-          }
-        }
-        return true;
-      }
+    if (!ss._stshif.equals( this._stshif )
+        || ss._cbStshi != this._cbStshi
+        || ss._styleDescriptions.length != this._styleDescriptions.length
+    ) return false;
+    
+    for (int i=0; i<_styleDescriptions.length; i++) {
+        StyleDescription tsd = this._styleDescriptions[i];
+        StyleDescription osd = ss._styleDescriptions[i];
+        if (tsd == null && osd == null) continue;
+        if (tsd == null || osd == null || !osd.equals(tsd)) return false;
     }
-    return false;
+
+    return true;
   }
 
   @Override
index 0c3e8c37252b214daf287c7cdfb22b8eccf5e338..22a55c53f6e884b1878735a0d8e74cca6825cab1 100644 (file)
@@ -204,13 +204,16 @@ public class TextPiece extends PropertyNode<TextPiece>
    @Override
    public boolean equals(Object o)
    {
-     if (limitsAreEqual(o))
-     {
-       TextPiece tp = (TextPiece)o;
-       return getStringBuilder().toString().equals(tp.getStringBuilder().toString()) &&
-              tp._usesUnicode == _usesUnicode && _pd.equals(tp._pd);
-     }
-     return false;
+     if (!(o instanceof TextPiece)) return false;
+     TextPiece tp = (TextPiece)o;
+     assert(_buf != null && tp._buf != null && _pd != null && tp._pd != null);
+     
+     return (
+            limitsAreEqual(o)
+         && tp._usesUnicode == this._usesUnicode
+         && tp._buf.toString().equals(this._buf.toString())
+         && tp._pd.equals(this._pd)
+     );
    }
 
    @Override
index facb4e472cd00080d7ea4febc3c56a62e9df90e2..11a8d604d92ae6ce044551b6d9fb6a4fe070a5e1 100644 (file)
@@ -160,6 +160,7 @@ public class TextPieceTable implements CharIndexTranslator
 
     public boolean equals( Object o )
     {
+        if (!(o instanceof TextPieceTable)) return false;
         TextPieceTable tpt = (TextPieceTable) o;
 
         int size = tpt._textPieces.size();
index 95dd00bc30281f31ab0b697dd20b64813d3ffc8f..5de2fe5248ad63e9a797e355d113e575b5801ac2 100644 (file)
@@ -43,6 +43,7 @@ public final class UPX
   @Override
   public boolean equals(Object o)
   {
+    if (!(o instanceof UPX)) return false;
     UPX upx = (UPX)o;
     return Arrays.equals(_upx, upx._upx);
   }
index 440c666403e79ae416318adf3179d54617dfeed1..73120db367d48aca8b5b0ffa3b81c7b6349d233d 100644 (file)
@@ -154,6 +154,7 @@ public final class SprmBuffer implements Cloneable
   @Override
   public boolean equals(Object obj)
   {
+    if (!(obj instanceof SprmBuffer)) return false;
     SprmBuffer sprmBuf = (SprmBuffer)obj;
     return (Arrays.equals(_buf, sprmBuf._buf));
   }
index 47e3b352f9d4de3d4bb3e6a2a62525114a9bff8b..a61db1bd24f740295b1d3e4507e60e93c49dac3f 100644 (file)
@@ -71,6 +71,7 @@ public final class BorderCode implements Cloneable {
   @Override
   public boolean equals(Object o)
   {
+    if (!(o instanceof BorderCode)) return false;
     BorderCode brc = (BorderCode)o;
     return _info == brc._info && _info2 == brc._info2;
   }
index 9000cc1ec839a62aec78ba48d845eda47b9e74f0..a9b2fd9da8bae3356a28f9c68a737a2b628f7ec4 100644 (file)
@@ -75,6 +75,7 @@ public final class DateAndTime
   @Override
   public boolean equals(Object o)
   {
+    if (!(o instanceof DateAndTime)) return false;
     DateAndTime dttm = (DateAndTime)o;
     return _info == dttm._info && _info2 == dttm._info2;
   }
index e327f039ce8863311ff03c9e3da570a6328485df..6e6b8020ad4ef290d6333ae6e951949decbc704e 100644 (file)
@@ -75,6 +75,7 @@ public final class LineSpacingDescriptor
   @Override
   public boolean equals(Object o)
   {
+    if (!(o instanceof LineSpacingDescriptor)) return false;
     LineSpacingDescriptor lspd = (LineSpacingDescriptor)o;
 
     return _dyaLine == lspd._dyaLine && _fMultiLinespace == lspd._fMultiLinespace;