Extending JavaCompiler

Antonio Tancredi nazareno.tancredi at gmail.com
Wed Jul 10 10:15:31 PDT 2013


This is exactly what I need. I can use the factories and the context also
for the parser and the lexer.

Thank you.


2013/7/9 Jonathan Gibbons <jonathan.gibbons at oracle.com>

> You need to preregister Context.Factory objects in the context for any
> javac classes you want to override.
>
> -- Jon
>
>
> On 07/09/2013 07:45 PM, Antonio Tancredi wrote:
>
>> Good morning,
>> I hope you can help me with a tricky situation with JDK7 langtools.
>>
>> I'm trying to extend the compiler behaviour in order to collect some
>> information:
>>
>> * the text of literals (for istance, I want to know if in the source code
>> there's 'long a = 10l' or 'long a = 10L')
>> * the attributed AST of each parsed source file
>>
>> It was quite easy to extend the scanner and override the metod "public
>> void nextToken()" to get the raw characters of the literal token. After
>> this, I extended the parser, in order to get the lexer.
>> Also, I created a class that extends JavaCompiler with a metod that
>> override:
>>
>> "Queue<Env<AttrContext>> attribute(Queue<Env<**AttrContext>>)"
>>
>> and saves the results (here, I have every attributed AST created by the
>> compiler).
>> I overrid the parse metod so I can save, in a map, the parser that parsed
>> that java file object (in this way, later, for each sourcefile, I can
>> retrieve the lexer).
>>
>> Everything works fine until something in the classpath need to process
>> annotations.
>> In this method:
>>
>> JavaCompiler @ initProcessAnnotations(**Iterable<? extends Processor>)
>>
>> the variable "procEnvImpl" is initialized and "processAnnotations" become
>> true.
>> Inside this method:
>>
>> JavaCompiler @ processAnnotations(List<**JCCompilationUnit>,
>> List<String>)
>>
>> there's this statement "procEnvImpl.doProcessing" that creates a new
>> JavaCompiler (using the Round object), that is returned as a delegate
>> compiler. This delegate compiler (which is an istance of JavaCompiler) go
>> ahead with the other phases (using ATTR_ONLY policy, only with attribute
>> phase).
>>
>> The workaround is this one:
>>
>> void initProcessAnnotations(**Iterable<? extends Processor>) {}
>>
>> in my compiler class, but it's too dirty to keep it.
>>
>> There's any way I can have an istance of my compiler class everytime?
>>
>>
>> Thanks,
>>
>>
>> Antonio
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20130710/0bc74695/attachment.html 


More information about the compiler-dev mailing list