yf0994 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Objects.toStringHelper(Object) gives an incorrect class name if class name contains a dollar sign #1133

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Examples of classes (see my comments in each main(...) method):

public class Test {
    public static void main(String[] args) {
        String result = Objects.toStringHelper(new Test()).toString();
        // OK; result equals "Test{}"
    }
}

public class Te$t {
    public static void main(String[] args) {
        String result = Objects.toStringHelper(new Te$t()).toString();
        // BUG: result equals "t{}"
    }
}

public class $ {
    public static void main(String[] args) {
        String result = Objects.toStringHelper(new $()).toString();
        // BUG: result equals "{}"
    }
}

Original issue reported on code.google.com by berra...@gmail.com on 2 Sep 2012 at 2:10

GoogleCodeExporter commented 9 years ago
This is by design, since inner classes use a dollar sign to separate the outer 
class name from the inner. I'd strongly recommend not using a dollar sign in 
your class names, but if you do you can always use the overload of 
toStringHelper that takes a String.

Original comment by cgdec...@gmail.com on 3 Sep 2012 at 3:54

GoogleCodeExporter commented 9 years ago
Note that Class.getSimpleName() returns correctly simple name in this case 
instead of private static String simpleName(Class<?> clazz) of Guava's Objects 
class.

It is very exceptional case when the character of dollar is used in class name, 
but The Java Language Specification ALLOWS to do it in "mechanically generated 
source code or, rarely, to access pre-existing names on legacy systems." (see 
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8)

Thus, it's good idea that the method's design formally came to compliance of 
the Specification (if it is possible), isn't it?

Original comment by berra...@gmail.com on 3 Sep 2012 at 4:37

GoogleCodeExporter commented 9 years ago
The problem, as usual, is GWT, where getSimpleName() isn't available.  One 
possibility is to use Class.getSimpleName() under "normal Java" and our own 
simpleName(Class) under GWT, but this doesn't seem worth the trouble.

Original comment by cpov...@google.com on 3 Sep 2012 at 5:08

GoogleCodeExporter commented 9 years ago
I would feel worse than this if toStringHelper weren't just aimed at helping 
debugging by making it easier to write quick-and-dirty toString() 
implementations.  It's a convenience, but I'm not sure I can imagine 
toStringHelper being a genuine necessity?

Original comment by wasserman.louis on 3 Sep 2012 at 9:07

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08