JDK-4096303 : $ as a legal identifier character _and_ as an internal inner class separator
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.1.4
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_95
  • CPU: x86
  • Submitted: 1997-12-02
  • Updated: 2000-01-10
  • Resolved: 2000-01-10
Related Reports
Duplicate :  
Relates :  
Description

Name: dgC58589			Date: 12/02/97


The Java people have decided to allow the `$'-character (dollar) as a
valid part of identifiers, even if Java is quite distant from perl and
shell scripts. Now they have also decided to use the same character as
an internal separator between outer and inner classes. This may give
trouble, as in this artificial example:

-FILE: Some.java----------------------------------------------------------
class Some$Clazz {  /* gives the file Some$Clazz.class */
}

/* trouble: Some.Clazz is represented internally (and on file) as
 * Some$Clazz, and strangely enough, $ is a legal identifier character
 * (as seen above). Kabom! */
class Some {        /* gives the file Some.class */
    class Clazz {   /* gives the file Some$Clazz.class too! */
    }
}

/* compilation gives:
 * Some.java:8: Class Some. Clazz already defined in Some.java.
 *     class Clazz {
 *           ^
 * 1 error */
--------------------------------------------------------------------------

Wonder if someone is interested in fixing this, and in that case, how
they will retain backward compatibility of compiled code. :-)

NOTE: I did this on Blacdown's port of JDK 1.1.3 to Linux, but
I guess the problem is valid for all implementations of
JDK 1.1.x.
(Review ID: 21187)
======================================================================

Comments
PUBLIC COMMENTS Use of '$' in Java identifiers may produce clashes with class names generated by the inner class mechanism: <pre> class Foo$Bar {} class Foo { class Bar {} // The compiler generates a class named 'Foo$Bar' here! } </pre>
10-06-2004

EVALUATION Is $ a legal character in Java identifiers? I didn't think so. todd.turnidge@Eng 1997-12-02 JLS 3.8 says: "The $ character should only be used in mechanically generated Java code...". It's legal but not to be used in the way the submitter used it. There's another bug filed against the fact that javac doesn't properly forbid collisions like this, but I can't find it in the mountain of old bugs (I also can't figure out how to escape $ in bugcrap searches.) I'm reassigning this to Bill in case he can recall enough context to find that bug and close this as a duplicate. david.stoutamire@Eng 1997-12-04 I ran into this problem myself, and sent a note to John about the problem. I don't think a bug report was previously filed, however. Note that it is technically the responsibility of writer of program generators that produce names containing '$' characters to avoid generating names that conflict with the naming conventions of inner classes, at least insofar as those naming conventions are documented in the inner classes spec. Unfortunately, this is a little rough when applied to class names, as opposed to synthetic members. I think this should be regarded as a bug, though any fix will require specification changes. william.maddox@Eng 1997-12-04 The inner classes specification states that "... bytecode-level class names like MyInnerClass$19 cannot be used by source code ..." This could be taken as forbidding any use of '$' in class names, other than by the compiler itself. Also, see 4094180 for a similar issue regarding synthetic fields. william.maddox@Eng 1997-12-04 In the case of synthetic fields, the compiler could be clever enough to avoid any names used by the user on a program-by-program basis, because these names are used only within the context of a single compilation unit. For classes, however, we may not know all of the conflicting classes at the time that a synthetic name must be chosen, due to separate compilation, thus we must rely on an a-priori partition of the namespace. See spec bug 4098167. william.maddox@Eng 2000-01-07
07-01-2000

WORK AROUND Name: dgC58589 Date: 12/02/97 Replace the internal use of $ with another character, or disallow $ in identifiers. ====================================================================== Note that '$' characters should appear only in Java source code produced by automatic program generators, and should never be included in code created by humans to be read by humans. A program generator could be coded to emit '$$' in circumstances where it would previously have emitteed '$' alone, which should avoid all conflicts with names generated by the inner classes mechanism. william.maddox@Eng 1997-12-04
04-12-1997