class lookup in javac

Jonathan Gibbons jonathan.gibbons at oracle.com
Fri Aug 8 18:47:29 UTC 2014


On 08/08/2014 05:43 AM, Jochen Theodorou wrote:
> sorry, looks like my last reply did go to you personally.. Anyway.. 
> thanks for the help, I am starting to feel more at home in javac 
> source code now.
>
> I successfully used javac to compile a class that extends another 
> class, which exists only programatically.
>
> If I wanted to get all the trees of the currently to be compiled 
> sources... or I should maybe say all compilation units... is there a 
> way to do that?

Use com.sun.source.util.JavacTask.parse()

>
> And one question for the compile states... does the compiler try to 
> complete the compilation for a compile unit first, or does the 
> compiler let all compilation units progress through all states at the 
> same time? Like "for all compileUnits do process" then "for all 
> compileUnits do attr" and so on. I am aware that this is simplified, 
> since they can't all be in sync like that all the time, but I am 
> interested in the general approach here.

It is not possible to compile mutually referential files one file at a time.

-- Jon

>
> bye Jochen
>
> Am 07.08.2014 19:52, schrieb Maurizio Cimadamore:
>> Hi Jochen,
>> I would say that the root of all magic lies in Resolve.loadClass - i.e.
>> that method is called whenever a class needs to be resolved given its
>> qualified name. This triggers a lookup using ClassFinder (which is a new
>> addition in JDK 9 compiler - if you are using older codebase you might
>> find analogous routines under ClassReader). ClassFinder is responsible
>> for resolving a qualified name into:
>>
>> *) a source file (i.e. if the file being compiled references another
>> file that is part of same compilation)
>> *) a class file (i.e. if the file being compiled references a class in
>> the classpath)
>> *) both a class and a source file (if both the above case apply)
>>
>> In the last case, ClassFinder will decide as to whether the source or
>> the class file should be used by preferring the 'freshest' file (see
>> ClassFinder.includeClassFile).
>>
>> After ClassFinder has established where the bits should be coming from,
>> javac will create a new ClassSymbol corresponding to the 'shell' of the
>> newly found class. Whenever javac needs to access to members of this
>> class, it will use a completer to do so - initially this completer will
>> point to ClassFinder.complete() itself, which will then route completion
>> to either JavaCompiler (source completion) or ClassReader (classfile
>> completion).
>>
>> I hope this helps.
>>
>> Maurizio
>>
>> On 07/08/14 13:50, Jochen Theodorou wrote:
>>> Hi,
>>>
>>> I am trying to understand javac internals a bit more and I am
>>> currently trying to find out where exactly javac finds that it doesn't
>>> know a class. I am also trying to identify where javac looks for a
>>> precompiled class, and how that class is represented.
>>>
>>> Can I get some pointers from you guys?
>>>
>>> bye Jochen
>>
>
>



More information about the compiler-dev mailing list