Question about "sourcefile"

Archie Cobbs archie.cobbs at gmail.com
Sun Feb 2 21:08:28 UTC 2025


Hi Joe,

Thanks for the hint! Looking further it looks like "sourcefile" can derive
from a class file, so what you're saying can actually happen.

But it also looks like for classes defined in actual source files being
compiled, the two things are always the same. That happens here
<https://github.com/openjdk/jdk/blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java#L507>
.

Thanks,
-Archie


On Sun, Feb 2, 2025 at 4:52 AM Joseph D. Darcy <joe.darcy at oracle.com> wrote:

> Hi Archie,
>
> Without actually checking if this is the case, if "sourcefile" could also
> mean the class file a type was constructed from, then nested types would
> have a different sourcefile field than the top-level type.
>
> HTH,
>
> -Joe
> On 1/30/2025 8:18 AM, Archie Cobbs wrote:
>
> In JavaCompiler.java, there is code that looks like this:
>
>         JavaFileObject prev = log.useSource(
>
> *env.enclClass.sym.sourcefile != null ?
>           env.enclClass.sym.sourcefile :
>           env.toplevel.sourcefile*);
>         try {
>             ... do something ...
>         } finally {
>             log.useSource(prev);
>         }
>
> I don't understand why the boldface part is needed. How would it ever be
> possible for a class A to have an enclosing class B that exists in a
> different source file?
>
> Put another way, isn't env.toplevel.sourcefile always the source file
> that corresponds to any class being compiled?
>
> Thanks for any clarifications.
>
> -Archie
>
> --
> Archie L. Cobbs
>
>

-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20250202/8155d98b/attachment.htm>


More information about the compiler-dev mailing list