java patches for string concatonation

Anthony Green green@cygnus.com
Sun Jan 9 14:59:00 GMT 2000


Here are a pair of patches - one for the compiler and one for the
runtime.  Once reviewed they should be committed at the same time.

When gcj sees string concatonation with the "+" operator, and the
first operand (op1) is either a String reference or a string literal,
we emit a call to `new StringBuffer(op1)' and append op2.  If op1 is
null then this constructor should be throwing a NullPointerException,
however, right now it is filling the buffer with "null".

I've appended two patches below.  One is to libgcj's StringBuffer
implementation.  It removes the special case for null arguments so it
will throw a NullPointerException in those cases.

The second is a compiler patch.  It will only call `new
StringBuffer(op1)' if it knows op1 is a constant.  If there's any
chance that it may be null, we have to use `new
StringBuffer().append(op1)'.

Note the old comment in StringBuffer.java:

-      // Note: nowhere does it say that we should handle a null
-      // argument here.  In fact, the JCL implies that we should not.
-      // But this leads to an asymmetry: `null + ""' will fail, while
-      // `"" + null' will work.

The compiler change corrects this problem.  The behaviour is
symmetrical now, and the bytecode output is identical to jikes'.
Sun's javac does something slightly different, but exhibits the same
behaviour.


2000-01-09  Anthony Green  <green@cygnus.com>

	    * java/lang/StringBuffer.java (StringBuffer): Don't special case
	    null argument.

Index: libjava/java/lang/StringBuffer.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/lang/StringBuffer.java,v
retrieving revision 1.2
diff -u -r1.2 StringBuffer.java
--- StringBuffer.java	1999/04/30 09:30:53	1.2
+++ StringBuffer.java	2000/01/09 22:27:32
@@ -241,12 +241,9 @@
 
   public StringBuffer (String str)
     {
-      // Note: nowhere does it say that we should handle a null
-      // argument here.  In fact, the JCL implies that we should not.
-      // But this leads to an asymmetry: `null + ""' will fail, while
-      // `"" + null' will work.
-      if (str == null)
-      str = "null";
+      // The documentation is not clear, but experimentation with
+      // other implementations indicates that StringBuffer(null)
+      // should throw a NullPointerException.
       count = str.length();
       // JLS: The initial capacity of the string buffer is 16 plus the
       // length of the argument string.



2000-01-09  Anthony Green  <green@cygnus.com>

	    * parse.y (build_string_concatenation): Only use
	    StringBuffer(String) shortcut if String arg is constant.

Index: gcc/java/parse.y
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/parse.y,v
retrieving revision 1.128
diff -u -r1.128 parse.y
--- parse.y	2000/01/07 20:28:11	1.128
+++ parse.y	2000/01/09 22:33:04
@@ -10222,9 +10222,9 @@
   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
     {
       /* Two solutions here: 
-       1) OP1 is a string reference, we call new StringBuffer(OP1)
+	 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
	  2) OP1 is something else, we call new StringBuffer().append(OP1). */
-      if (JSTRING_TYPE_P (TREE_TYPE (op1)))
+      if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
       op1 = BUILD_STRING_BUFFER (op1);
       else
       {

-- 
Anthony Green                                               Cygnus Solutions
                                                       Sunnyvale, California


More information about the Java-patches mailing list