groovy

"as" and "asType()" produce different results when converting Strings to primitive types

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Fixed
  • Affects Version/s: 1.6.5
  • Fix Version/s: 1.6.6, 1.7-rc-1
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

Try it out:

println "0" as int
println "0".asType(int.class)

I get this output:
0
48

This happens for all the primitives: byte, short, int, long, float, double

Using the non-primitive types yields the correct result, i.e.:
println "0" as Integer
println "0".asType(Integer.class)

gives:
0
0

Activity

Hide
Roshan Dawrani added a comment - - edited

Just wanted to confirm, if all 4 cases below are expected to produce "0" as the output:

println "0" as Integer
println "0" as int
println "0".asType(Integer.class)
println "0".asType(int.class) // currently produces "48"

Or, does anyone defend the output of the last case where it is producing "48" as the output?

I ask because DefaultTypeTransformation#castToNumber(Object, Class) tries to do return the ascii value of the first character of the string passed - Integer.valueOf(c.charAt(0)).

So the confusion is whether "48" is incorrect or "0"?

Show
Roshan Dawrani added a comment - - edited Just wanted to confirm, if all 4 cases below are expected to produce "0" as the output:
println "0" as Integer
println "0" as int
println "0".asType(Integer.class)
println "0".asType(int.class) // currently produces "48"
Or, does anyone defend the output of the last case where it is producing "48" as the output? I ask because DefaultTypeTransformation#castToNumber(Object, Class) tries to do return the ascii value of the first character of the string passed - Integer.valueOf(c.charAt(0)). So the confusion is whether "48" is incorrect or "0"?
Hide
Roshan Dawrani added a comment -

We can't solve this by making a general change that when class expressions are visited by ACG, all primitive types should be replaced by their wrapper types, right?

What about making DGM#asType(String, Class) have the same conversion for int/Integer, long/Long, as below?

public static Object asType(String self, Class c) {
.....
        } else if (c == Integer.class || c == Integer.TYPE) {
            return toInteger(self);
.....
    }

Any suggestions?

Show
Roshan Dawrani added a comment - We can't solve this by making a general change that when class expressions are visited by ACG, all primitive types should be replaced by their wrapper types, right? What about making DGM#asType(String, Class) have the same conversion for int/Integer, long/Long, as below?
public static Object asType(String self, Class c) {
.....
        } else if (c == Integer.class || c == Integer.TYPE) {
            return toInteger(self);
.....
    }
Any suggestions?
Hide
blackdrag blackdrag added a comment -

that looks good

Show
blackdrag blackdrag added a comment - that looks good
Hide
Roshan Dawrani added a comment -

Fixed

Show
Roshan Dawrani added a comment - Fixed

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: