Extending JavaCompiler

Jonathan Gibbons jonathan.gibbons at oracle.com
Tue Jul 9 14:29:13 PDT 2013


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
>
>



More information about the compiler-dev mailing list