This is the mail archive of the
java-patches@sourceware.cygnus.com
mailing list for the Java project.
Patch: remove explicit null checks
- To: Java Patch List <java-patches at sourceware dot cygnus dot com>
- Subject: Patch: remove explicit null checks
- From: Tom Tromey <tromey at cygnus dot com>
- Date: 04 May 2000 09:50:57 -0600
- Reply-To: tromey at cygnus dot com
I'm checking this in. It removes explicit null pointer checks except
in the cases where they are actually needed. It occurred to me while
writing this patch that it might later be confusing to see a reference
like `arg.length' without any mention of null pointer exceptions.
However, I think we'll be ok, because at some point we'll all remember
that this is part of the coding style.
To recap the rule: you need a null pointer check:
1. In C++ code
2. In a Java method that is defined to throw NullPointerException if
an argument is null, but where the argument is not dereferenced by
the implementation.
It's possible that this patch will seem like a step backwards for
MMU-less systems. But the reality is that the current code is nowhere
near doing complete checks for null pointers. The real solution is to
fix the compiler to do runtime checks.
2000-05-04 Tom Tromey <tromey@cygnus.com>
* java/util/Locale.java (Locale): Don't explicitly check for
null.
* java/util/Hashtable.java (containsKey): Don't explicitly check
for null.
(get): Likewise.
* java/util/BitSet.java (and, or, xor): Don't explicitly check for
null.
* java/util/zip/ZipEntry.java (ZipEntry): Don't explicitly check
for null.
* java/text/StringCharacterIterator.java
(StringCharacterIterator): Don't check for null.
* java/text/ChoiceFormat.java (setChoices): Don't explicitly check
for null pointer.
* java/net/MulticastSocket.java (joinGroup): Don't explicitly
check for null pointer.
(leaveGroup): Likewise.
* java/net/DatagramPacket.java (DatagramPacket): Removed erroneous
comment.
(setData): Likewise.
* java/lang/ThreadGroup.java (ThreadGroup): Don't explicitly check
for `p==null'.
Tom
Index: java/lang/ThreadGroup.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/lang/ThreadGroup.java,v
retrieving revision 1.5
diff -u -r1.5 ThreadGroup.java
--- ThreadGroup.java 2000/03/07 19:55:26 1.5
+++ ThreadGroup.java 2000/05/04 15:01:56
@@ -1,6 +1,6 @@
// ThreadGroup.java - ThreadGroup class.
-/* Copyright (C) 1998, 1999 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -344,8 +344,6 @@
public ThreadGroup (ThreadGroup p, String n)
{
checkAccess ();
- if (p == null)
- throw new NullPointerException ();
if (p.destroyed_flag)
throw new IllegalArgumentException ();
Index: java/net/DatagramPacket.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/net/DatagramPacket.java,v
retrieving revision 1.3
diff -u -r1.3 DatagramPacket.java
--- DatagramPacket.java 2000/03/07 19:55:27 1.3
+++ DatagramPacket.java 2000/05/04 15:01:58
@@ -1,6 +1,6 @@
// DatagramPacket.java - Represents packets in a connectionless protocol.
-/* Copyright (C) 1999 Free Software Foundation
+/* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -32,8 +32,6 @@
// JDK1.2
public DatagramPacket(byte[] buf, int offset, int length)
{
- // FIXME: We can't currently rely on NullPointerException being
- // thrown when we invoke a method on a null object.
if (buf == null)
throw new NullPointerException("Null buffer");
if (offset < 0)
@@ -60,8 +58,6 @@
public DatagramPacket(byte[] buf, int offset, int length,
InetAddress address, int port)
{
- // FIXME: We can't currently rely on NullPointerException being
- // thrown when we invoke a method on a null object.
if (buf == null)
throw new NullPointerException("Null buffer");
if (offset < 0)
@@ -145,8 +141,6 @@
{
// This form of setData must be used if offset is to be changed.
- // FIXME: We can't currently rely on NullPointerException being
- // thrown when we invoke a method on a null object.
if (buf == null)
throw new NullPointerException("Null buffer");
if (offset < 0)
Index: java/net/MulticastSocket.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/net/MulticastSocket.java,v
retrieving revision 1.6
diff -u -r1.6 MulticastSocket.java
--- MulticastSocket.java 2000/04/11 09:21:53 1.6
+++ MulticastSocket.java 2000/05/04 15:01:58
@@ -1,6 +1,6 @@
// MulticastSocket.java
-/* Copyright (C) 1999 Free Software Foundation
+/* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -84,10 +84,6 @@
public void joinGroup(InetAddress mcastaddr) throws IOException
{
- // FIXME: We can't currently rely on NullPointerException being
- // thrown when we invoke a method on a null object.
- if (mcastaddr == null)
- throw new NullPointerException("Null address");
if (! mcastaddr.isMulticastAddress())
throw new IOException("Not a Multicast address");
@@ -100,10 +96,6 @@
public void leaveGroup(InetAddress mcastaddr) throws IOException
{
- // FIXME: We can't currently rely on NullPointerException being
- // thrown when we invoke a method on a null object.
- if (mcastaddr == null)
- throw new NullPointerException("Null address");
if (! mcastaddr.isMulticastAddress())
throw new IOException("Not a Multicast address");
Index: java/text/ChoiceFormat.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/text/ChoiceFormat.java,v
retrieving revision 1.4
diff -u -r1.4 ChoiceFormat.java
--- ChoiceFormat.java 2000/03/07 19:55:27 1.4
+++ ChoiceFormat.java 2000/05/04 15:01:59
@@ -1,6 +1,6 @@
// ChoiceFormat.java - Formatter for `switch'-like string substitution.
-/* Copyright (C) 1999 Free Software Foundation
+/* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -257,8 +257,6 @@
public void setChoices (double[] limits, String[] strings)
{
- if (limits == null || strings == null)
- throw new NullPointerException ();
if (limits.length != strings.length)
throw new IllegalArgumentException ();
this.strings = (String[]) strings.clone();
Index: java/text/StringCharacterIterator.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/text/StringCharacterIterator.java,v
retrieving revision 1.3
diff -u -r1.3 StringCharacterIterator.java
--- StringCharacterIterator.java 2000/03/07 19:55:27 1.3
+++ StringCharacterIterator.java 2000/05/04 15:01:59
@@ -1,6 +1,6 @@
// StringCharacterIterator.java - Iterate over string of Unicode characters.
-/* Copyright (C) 1999 Free Software Foundation
+/* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -106,20 +106,14 @@
public StringCharacterIterator (String text)
{
- // FIXME: remove check for null once we have compiler/runtime
- // support for NullPointerException.
- this (text, 0, text == null ? 0 : text.length(), 0);
+ this (text, 0, text.length(), 0);
}
public StringCharacterIterator (String text, int pos)
{
- // FIXME: remove check for null once we have compiler/runtime
- // support for NullPointerException.
- this (text, 0, text == null ? 0 : text.length(), pos);
+ this (text, 0, text.length(), pos);
}
public StringCharacterIterator (String text, int begin, int end, int pos)
{
- if (text == null)
- throw new NullPointerException ();
if (begin < 0 || begin > end || end > text.length()
// In 1.1 we would also throw if `pos == end'.
|| pos < begin || pos > end)
Index: java/util/BitSet.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/BitSet.java,v
retrieving revision 1.4
diff -u -r1.4 BitSet.java
--- BitSet.java 2000/03/07 19:55:27 1.4
+++ BitSet.java 2000/05/04 15:01:59
@@ -1,6 +1,6 @@
// BitSet - A vector of bits.
-/* Copyright (C) 1998, 1999 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -24,8 +24,6 @@
{
public void and (BitSet bs)
{
- if (bs == null)
- throw new NullPointerException ();
int max = Math.min(bits.length, bs.bits.length);
int i;
for (i = 0; i < max; ++i)
@@ -110,8 +108,6 @@
public void or (BitSet bs)
{
- if (bs == null)
- throw new NullPointerException ();
ensure (bs.bits.length - 1);
int i;
for (i = 0; i < bs.bits.length; ++i)
@@ -159,8 +155,6 @@
public void xor (BitSet bs)
{
- if (bs == null)
- throw new NullPointerException ();
ensure (bs.bits.length - 1);
int i;
for (i = 0; i < bs.bits.length; ++i)
Index: java/util/Hashtable.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/Hashtable.java,v
retrieving revision 1.4
diff -u -r1.4 Hashtable.java
--- Hashtable.java 2000/04/11 20:02:48 1.4
+++ Hashtable.java 2000/05/04 15:02:01
@@ -164,7 +164,8 @@
return newTable;
}
- public synchronized boolean contains(Object value) throws NullPointerException
+ public synchronized boolean contains(Object value)
+ throws NullPointerException
{
// An exception is thrown here according to the JDK 1.2 doc.
if (value == null)
@@ -180,10 +181,6 @@
public synchronized boolean containsKey(Object key)
{
- // The Map interface mandates that we throw this.
- if (key == null)
- throw new NullPointerException ();
-
for (HashtableEntry elem = bucket[Math.abs(key.hashCode()
% bucket.length)];
elem != null; elem = elem.nextEntry)
@@ -200,11 +197,6 @@
public synchronized Object get(Object key)
{
- // The Dictionary interface mandates that get() throw a
- // NullPointerException if key is null.
- if (key == null)
- throw new NullPointerException ();
-
for (HashtableEntry elem = bucket[Math.abs (key.hashCode()
% bucket.length)];
elem != null; elem = elem.nextEntry)
@@ -225,8 +217,10 @@
}
public synchronized Object put(Object key, Object value)
- throws NullPointerException
+ throws NullPointerException
{
+ // We could really just check `value == null', but checking both
+ // is a bit clearer.
if (key == null || value == null)
throw new NullPointerException();
Index: java/util/Locale.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/Locale.java,v
retrieving revision 1.5
diff -u -r1.5 Locale.java
--- Locale.java 2000/03/07 19:55:27 1.5
+++ Locale.java 2000/05/04 15:02:01
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -65,10 +65,6 @@
public Locale (String languageCode, String countryCode,
String variantCode)
{
- // We must explicitly check the arguments.
- if (languageCode == null || countryCode == null
- || variantCode == null)
- throw new NullPointerException ();
language = languageCode.toLowerCase();
country = countryCode.toUpperCase();
variant = variantCode.toUpperCase();
Index: java/util/zip/ZipEntry.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/zip/ZipEntry.java,v
retrieving revision 1.8
diff -u -r1.8 ZipEntry.java
--- ZipEntry.java 2000/03/07 19:55:28 1.8
+++ ZipEntry.java 2000/05/04 15:02:01
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999 Free Software Foundation
+/* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj.
@@ -39,8 +39,6 @@
public ZipEntry (String name)
{
- if (name == null)
- throw new NullPointerException ();
if (name.length() > 65535)
throw new IllegalArgumentException ();
this.name = name;