JDK-4741726 : allow Object += String
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: solaris_8
  • CPU: generic
  • Submitted: 2002-09-04
  • Updated: 2008-03-22
  • Resolved: 2011-05-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b25Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
javac used to allow expressions of the form

	o += s

where o is a variable of type Object and s is an expression of type String.
We fixed that recently (4642850) and this caused a build failure (4741702).
Perhaps this is common enough that we should relax the spec instead of
fixing the compiler?

Comments
SUGGESTED FIX Changeset: f09d6a3521b1 Author: jjg Date: 2008-03-06 10:07 -0800 URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/f09d6a3521b1 4741726: allow Object += String Summary: remove code in line with restriction removed from JLS Reviewed-by: mcimadamore Contributed-by: ###@###.### ! src/share/classes/com/sun/tools/javac/comp/Attr.java + test/tools/javac/StringConversion2.java - test/tools/javac/expression/ObjectAppend.java
06-03-2008

SUGGESTED FIX From ###@###.### Index: langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java =================================================================== --- langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java (revision 258) +++ langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java (working copy) @@ -1609,17 +1609,10 @@ tree.getTag() - JCTree.ASGOffset, owntype, operand); - if (types.isSameType(operator.type.getReturnType(), syms.stringType)) { - // String assignment; make sure the lhs is a string - chk.checkType(tree.lhs.pos(), - owntype, - syms.stringType); - } else { - chk.checkDivZero(tree.rhs.pos(), operator, operand); - chk.checkCastable(tree.rhs.pos(), - operator.type.getReturnType(), - owntype); - } + chk.checkDivZero(tree.rhs.pos(), operator, operand); + chk.checkCastable(tree.rhs.pos(), + operator.type.getReturnType(), + owntype); } result = check(tree, owntype, VAL, pkind, pt); }
31-01-2008

EVALUATION JLS3 permits Object+=String because the '+' means string concatenation and that is able to concatenate an Object with a String as easily as a String with an Object.
31-01-2008

EVALUATION I'm inclined to relax the spec, though we'd have to know what other implementations do before making a final call on this. ###@###.### 2002-09-04
04-09-2002