You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by hi...@apache.org on 2010/10/07 23:54:48 UTC

svn commit: r1005648 - /harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/lang/String.java

Author: hindessm
Date: Thu Oct  7 21:54:48 2010
New Revision: 1005648

URL: http://svn.apache.org/viewvc?rev=1005648&view=rev
Log:
Remove explicit null checks in cases were deference occurs naturally
(I had to modify the bounds checking order in one case to get the
exception order right.)

(One of these was spotted while Tim, Oliver, Cath and I started doing
a code review of StringBuilder.  So I looked for more.  We've still to
write up the comments from the review.)

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/lang/String.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/lang/String.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/lang/String.java?rev=1005648&r1=1005647&r2=1005648&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/lang/String.java (original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/lang/String.java Thu Oct  7 21:54:48 2010
@@ -267,21 +267,18 @@ public final class String implements Ser
      */
     @Deprecated
     public String(byte[] data, int high, int start, int length) {
-        if (data != null) {
-            // start + length could overflow, start/length maybe MaxInt
-            if (start >= 0 && 0 <= length && length <= data.length - start) {
-                offset = 0;
-                value = new char[length];
-                count = length;
-                high <<= 8;
-                for (int i = 0; i < count; i++) {
-                    value[i] = (char) (high + (data[start++] & 0xff));
-                }
-            } else {
-                throw new StringIndexOutOfBoundsException();
+        // putting the data.length check first forces the NPE
+        // start + length could overflow, start/length maybe MaxInt
+        if (length <= data.length - start && start >= 0 && 0 <= length) {
+            offset = 0;
+            value = new char[length];
+            count = length;
+            high <<= 8;
+            for (int i = 0; i < count; i++) {
+                value[i] = (char) (high + (data[start++] & 0xff));
             }
         } else {
-            throw new NullPointerException();
+            throw new StringIndexOutOfBoundsException();
         }
     }
 
@@ -523,9 +520,6 @@ public final class String implements Ser
      * @since 1.5
      */
     public String(StringBuilder sb) {
-        if (sb == null) {
-            throw new NullPointerException();
-        }
         this.offset = 0;
         this.count = sb.length();
         this.value = new char[this.count];
@@ -1226,10 +1220,7 @@ public final class String implements Ser
      */
     public boolean regionMatches(int thisStart, String string, int start,
             int length) {
-        if (string == null) {
-            throw new NullPointerException();
-        }
-        if (start < 0 || string.count - start < length) {
+        if (string.count - start < length || start < 0) {
             return false;
         }
         if (thisStart < 0 || count - thisStart < length) {
@@ -1728,10 +1719,6 @@ public final class String implements Ser
      * @since 1.5
      */
     public boolean contentEquals(CharSequence cs) {
-        if (cs == null) {
-            throw new NullPointerException();
-        }
-
         int len = cs.length();
 
         if (len != count) {
@@ -1940,9 +1927,6 @@ public final class String implements Ser
      * @since 1.5
      */
     public boolean contains(CharSequence cs) {
-        if (cs == null) {
-            throw new NullPointerException();
-        }
         return indexOf(cs.toString()) >= 0;
     }