<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
I like this progression; unnamed -> named . (Also named -> unnamed.)<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Oct 20, 2022, at 9:45 AM, Dan Heidinga <<a href="mailto:heidinga@redhat.com" class="">heidinga@redhat.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class=""><br class="Apple-interchange-newline">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">
<div class="gmail_quote" style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div dir="ltr" class="gmail_attr">On Wed, Oct 19, 2022 at 6:59 PM John Rose <<a href="mailto:john.r.rose@oracle.com" class="">john.r.rose@oracle.com</a>> wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">
<u class=""></u>
<div class="">
<div style="font-family: sans-serif;" class="">
<div style="white-space: normal;" class="">
<p dir="auto" class="">On 19 Oct 2022, at 13:35, Brian Goetz wrote:</p>
</div>
<div style="white-space: normal;" class="">
<blockquote style="margin: 0px 0px 5px; padding-left: 5px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(119, 119, 119); color: rgb(119, 119, 119);" class="">
<p dir="auto" class="">Let's pull on this string.</p>
<p dir="auto" class="">When we say `javac Foo.java`, the compiler has to create a class file, and doesn't have the benefit of a declared class name. The logical output file is `Foo.class`, because otherwise the next thing the user is likely to do is `java Foo`,
and the class loader is going to look for Foo.class.</p>
</blockquote>
</div>
<div style="white-space: normal;" class="">
<p dir="auto" class="">I guess what I’m suggesting here is that what’s new (or what<span class="Apple-converted-space"> </span><em class="">should</em><span class="Apple-converted-space"> </span>be new) is a full removal of all coupling between the file name
(of the .class file) and the class’s bytecode name (in the ClassFile structure). VM anonymous classes (VMACs) are the precedent I’m looking at; IIRC only the package prefix of a VMAC is significant to any VM operation. (With minor exceptions: Reflection and
stack traces probably report a string that depends on the bytecode name.)</p>
<p dir="auto" class="">It seems reasonable to steer towards such a decoupling, because it would be (a) similar to what we have with VMACs, but with an accidental inclusion of a file system container<span class="Apple-converted-space"> </span><em class="">of
an irrelevant name</em>; it would also be (b) useful to put zero constraints on the classfile name (other than a .class suffix perhaps) so that tools have fewer irrelevant details to worry about.</p>
</div>
<div style="white-space: normal;" class="">
<blockquote style="margin: 0px 0px 5px; padding-left: 5px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(119, 119, 119); color: rgb(119, 119, 119);" class="">
<p dir="auto" class="">A .class file has a ClassFile structure, which has a `this_class` field which names the current class.</p>
</blockquote>
</div>
<div style="white-space: normal;" class="">
<p dir="auto" class="">Names it for what purposes? For a nameless class, the only purposes are informative as with VMACs. I think that not even<span class="Apple-converted-space"> </span><code style="margin: 0px; padding: 0px 0.4em; border-radius: 3px; background-color: rgb(247, 247, 247);" class="">Class.forName</code><span class="Apple-converted-space"> </span>should
be able to recover the current class, only<span class="Apple-converted-space"> </span><code style="margin: 0px; padding: 0px 0.4em; border-radius: 3px; background-color: rgb(247, 247, 247);" class="">this.getClass()</code>. Maybe that’s too much of a reach?</p>
</div>
<div style="white-space: normal;" class="">
<blockquote style="margin: 0px 0px 5px; padding-left: 5px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(119, 119, 119); color: rgb(119, 119, 119);" class="">
<p dir="auto" class="">We experimented with calling the class something like `$Foo` or $Unnamed, but this trick just garners a NoClassDefFoundError, with reason "wrong name". This error comes from the native method `ClassLoader::defineClass1`.</p>
</blockquote>
</div>
<div style="white-space: normal;" class="">
<p dir="auto" class="">That’s a superficial problem. We are defining a new path for loading classes, so we have a right to adjust the rules.</p>
<p dir="auto" class="">OTOH, and alternatively, and more in line with the gradual on-ramp: We could insist that, no, we are just defining an easier way to define a regular old class; we take care of issuing the name for you. Which is less surprising, that a
named class you didn’t declare pops up somewhere and with a name vaguely like the filename, but is “just a class you could have coded”, or that, if there is no name, none of the name-related operations work.</p>
<p dir="auto" class="">I guess part of the issue is the anonymous classes (of various sorts) are two things at once: 1. a really obscure power-user feature, and 2. a desirable default early on the onramp. Having it both ways causes tension.</p>
<p dir="auto" class="">If we embrace “it is anonymous” I think we get the cleanest experience. People won’t be tempted to predict the name and refer to it somehow (with reflection or even source code).</p>
<p dir="auto" class="">If we embrace “it has a name you didn’t pick” I think we get a simpler onramp story, but at the cost of dark corners in the UE. Users will immediately try to exploit the fact that the name is semi-predictable, and write code that works
with the names of these classes.</p>
</div>
</div>
</div>
</blockquote>
<div class="">After reading this (and the other thread), I'm more firmly convinced that using the file name is the right answer due to that "simpler onramp story". It allows the unnamed class to have a stable name which enables growing it to have proper constructors,
referencing it from other classes, etc. Users trying to predict are learning more advanced features and will be ready to upgrade their classes to have explicit names. It becomes a much smaller step to then add the opening "class Foo {" & closing "}".</div>
<div class=""><br class="">
</div>
<div class="">If the goal is an easier on ramp, guiding them to understand the connection between Class names and file names (though not 100%) will help them navigate other source bases as well. This seems like a pure win.</div>
<div class=""><br class="">
</div>
<div class="">Treating it as anonymous doesn't help users to take that next step.</div>
<div class=""> </div>
<div class="">--Dan</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">
<div class="">
<div style="font-family: sans-serif;" class="">
<div style="white-space: normal;" class=""></div>
<div style="white-space: normal;" class="">
<blockquote style="margin: 0px 0px 5px; padding-left: 5px; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(119, 119, 119); color: rgb(119, 119, 119);" class="">
<p dir="auto" class="">With inner classes, we've taken the position that class names with $ in their name are likely to be unstable names not to be counted on. So calling it $Foo sends that signal, good. But we'd have to be willing to loosen the checking
in the class loader to allow loading a class with a slightly mangled name such as $Unnamed (and then make the launcher more tolerant of that.)</p>
</blockquote>
</div>
<div style="white-space: normal;" class="">
<p dir="auto" class="">In the end, I’m totally willing to do this.</p>
<p dir="auto" class="">Idea: Have the launcher not call defineClass at all, but rather take the byte image of the *.class file, and run it into the VM as a VMAC. That is a principled position that will prevent lots of nonsense about secret names.</p>
<p dir="auto" class="">— John</p>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br class="">
</body>
</html>