Issue Details (XML | Word | Printable)

Key: XALANJ-2217
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Trivial Trivial
Assignee: Brian Minchau
Reporter: Dave Brosius
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
XalanJ2

[PATCH] cleaup some String usage sillyness

Created: 21/Oct/05 01:52 PM   Updated: 09/Jan/08 04:14 AM
Return to search
Component/s: None
Affects Version/s: 2.7
Fix Version/s: 2.7.1
Security Level: No security risk; visible to anyone (Ordinary problems in Xalan projects. Anybody can view the issue.)

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works silly_string_stuff.diff 2005-10-21 01:53 PM Dave Brosius 5 kB
File Licensed for inclusion in ASF works silly_string_stuff_2.diff 2005-10-23 12:22 PM Libor Valenta 13 kB
Environment: n/a

Xalan info: PatchAvailable
Resolution Date: 23/Oct/05 01:10 AM


 Description  « Hide
This patch cleans up some String class sillyness like creating an object just to convert to string, or calling toString on a String, etc.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Brian Minchau added a comment - 22/Oct/05 06:02 AM
Dave, I'm ready to commit the patch, but have one issue I'm not sure of.

-----------------------------------------------
Most are trivial .toString() methods called on a String, object, so the call is useless, and could even
create an additional String for no reason at all. I'm fine with these.
------------------------------------------------
OpMap has this change
    new Integer(stepType).toString()
                -------------> String.valueOf(stepType)
where stepType is an int. I looked up the Javadoc on String.valueOf(int) at
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#valueOf(int)
and it said this:
    "The representation is exactly the one returned by the Integer.toString method of one argument. "
so I'm OK with that one.

------------------------------------------------

The only change in the whole patch that has me concerned is the one in ElemNumber

    (new Character(table.getChar((int)val - 1))).toString();
                   -----------> String.valueOf(table.getChar((int)val - 1));
table.getChar(int index) returns a char. I'm not sure that for a given char, ch, that
    String.valueOf(ch)
is the same as
    (new Character(ch)).toString();

The javadoc at:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#valueOf(char)
says this:
    >> Returns the string representation of the char argument.

The javadoc for Charcter.toString() at:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html#toString()
says this:
    >> Returns a String object representing this Character's value.
    >>The result is a string of length 1 whose sole component is the primitive
    >> char value represented by this Character object.

So are these always the same? I'm worried about some subtle differences between char and Character.
Perhaps the current locale can leak in somehow? I don't know for sure, just have my worries about this one.

These are always the same, then I'm ready to commit the patch to the code base.
- Brian





Brian Minchau added a comment - 23/Oct/05 01:10 AM
The patch supplied by Dave was reviewed/approved/applied by myself. It was applied with one exception. The change to ElemNumber was not applied.

I was not convinced that these were the same for a given char, ch:
> String.valueOf(ch)
> (new Character(ch)).toString();

Perhaps they result in the same String, or perhaps in the given context for the limited range of ch values that would be used they are the same, but I was not convinced. Also, I checked if this code path was ever taken for the "build smoketest" or "build alltest.conf" test buckets. The code path was not exercised, so it also lowered my confidence.

Libor Valenta added a comment - 23/Oct/05 12:22 PM
(new Character(ch)).toString() and String.valueOf(ch) both produce one character String, only first one creates unnecessary Character object, char[] and calls native method System.arraycopy().

I used IntelliJ inspection to scan code for other String silliness, apply patch as you see fit.

It's pointless to createStringBuffers and then concatenate strings by + (compiler will create another StringBuffer/StringBuilder)


Brian Minchau added a comment - 11/Dec/07 03:16 PM
This issue is no longer relevant for 2.7.1, it is fixed in 2.7.1.
Changing the affected version to 2.7.

Brian Minchau added a comment - 11/Dec/07 04:57 PM
Would the originator of this issue please verify that this issue is fixed in the 2.7.1 release, by adding a comment to this issue, so that we can close this issue.

A lack of response by February 1, 2008 will be taken as consent that we can close this resolved issue.

Regards,
Brian Minchau

Dave Brosius added a comment - 09/Jan/08 04:14 AM
Fixed

Dave Brosius added a comment - 09/Jan/08 04:14 AM
Patch applied correctly (with noted missing String.valueOf(ch) change)