com.sun.tools.javac.processing
Class JavacProcessingEnvironment

java.lang.Object
  extended by com.sun.tools.javac.processing.JavacProcessingEnvironment
All Implemented Interfaces:
java.io.Closeable, ProcessingEnvironment

public class JavacProcessingEnvironment
extends java.lang.Object
implements ProcessingEnvironment, java.io.Closeable

Objects of this class hold and manage the state needed to support annotation processing.

This is NOT part of any API supported by Sun Microsystems. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.


Nested Class Summary
private  class JavacProcessingEnvironment.AnnotationCollector
           
static class JavacProcessingEnvironment.ComputeAnnotationSet
          Computes the set of annotations on the symbol in question.
(package private)  class JavacProcessingEnvironment.DiscoveredProcessors
          This class holds information about the processors that have been discoverd so far as well as the means to discover more, if necessary.
private static class JavacProcessingEnvironment.NameProcessIterator
           
(package private) static class JavacProcessingEnvironment.ProcessorState
          State about how a processor has been used by the tool.
private  class JavacProcessingEnvironment.ServiceIterator
          Use a service loader appropriate for the platform to provide an iterator over annotations processors.
 
Field Summary
private static java.util.regex.Pattern allMatches
           
private  Context context
           
private  JavacProcessingEnvironment.DiscoveredProcessors discoveredProcs
          Holds relevant state history of which processors have been used.
private  JavacElements elementUtils
           
private  boolean fatalErrors
           
private  JavacFiler filer
           
private  boolean lint
           
(package private)  Log log
          The log to be used for error reporting.
private  JavacMessager messager
           
private  JavacMessages messages
          JavacMessages object used for localization
private static java.util.regex.Pattern noMatches
           
(package private)  Options options
           
private  java.util.Set<java.lang.String> platformAnnotations
          Annotations implicitly processed and claimed by javac.
private  boolean printProcessorInfo
           
private  boolean printRounds
           
private  java.util.Map<java.lang.String,java.lang.String> processorOptions
          Map of processor-specific options.
private  boolean procOnly
           
(package private)  Source source
          Source level of the compile.
private  java.util.Set<Symbol.PackageSymbol> specifiedPackages
          Set of packages given on command line.
private static TreeScanner treeCleaner
           
private  JavacTypes typeUtils
           
private  java.util.Set<java.lang.String> unmatchedProcessorOptions
           
private  boolean verbose
           
 
Constructor Summary
JavacProcessingEnvironment(Context context, java.lang.Iterable<? extends Processor> processors)
           
 
Method Summary
 boolean atLeastOneProcessor()
           
private  boolean callProcessor(Processor proc, java.util.Set<? extends TypeElement> tes, RoundEnvironment renv)
           
private static
<T extends JCTree>
List<T>
cleanTrees(List<T> nodes)
           
 void close()
          Free resources related to annotation processing.
private  Context contextForNextRound(Context context, boolean shareNames)
           
private  void discoverAndRunProcs(Context context, java.util.Set<TypeElement> annotationsPresent, List<Symbol.ClassSymbol> topLevelClasses, List<Symbol.PackageSymbol> packageInfoFiles)
           
 JavaCompiler doProcessing(Context context, List<JCTree.JCCompilationUnit> roots, List<Symbol.ClassSymbol> classSymbols, java.lang.Iterable<? extends Symbol.PackageSymbol> pckSymbols)
           
private  ListBuffer<Symbol.ClassSymbol> enterNewClassFiles(Context currentContext)
           
private static java.net.URL fileToURL(java.io.File file)
          Returns the directory or JAR file URL corresponding to the specified local file name.
 Context getContext()
          For internal use by Sun Microsystems only.
 JavacElements getElementUtils()
          Returns an implementation of some utility methods for operating on elements
 Filer getFiler()
          Returns the filer used to create new source, class, or auxiliary files.
 java.util.Locale getLocale()
          Returns the current locale or null if no locale is in effect.
 Messager getMessager()
          Returns the messager used to report errors, warnings, and other notices.
 java.util.Map<java.lang.String,java.lang.String> getOptions()
           Command line options suitable for presenting to annotation processors.
private  List<Symbol.PackageSymbol> getPackageInfoFiles(List<? extends JCTree.JCCompilationUnit> units)
           
 SourceVersion getSourceVersion()
          Returns the source version that any generated source and class files should conform to.
 java.util.Set<Symbol.PackageSymbol> getSpecifiedPackages()
           
private  List<Symbol.ClassSymbol> getTopLevelClasses(List<? extends JCTree.JCCompilationUnit> units)
           
 JavacTypes getTypeUtils()
          Returns an implementation of some utility methods for operating on types.
private  void handleException(java.lang.String key, java.lang.Exception e)
          Handle a security exception thrown during initializing the Processor iterator.
private  java.util.Iterator<Processor> handleServiceLoaderUnavailability(java.lang.String key, java.lang.Exception e)
          Returns an empty processor iterator if no processors are on the relevant path, otherwise if processors are present, logs an error.
static java.util.regex.Pattern importStringToPattern(java.lang.String s, Processor p, Log log)
          Convert import-style string to regex matching that string.
private  java.util.Set<java.lang.String> initPlatformAnnotations()
           
private  void initProcessorIterator(Context context, java.lang.Iterable<? extends Processor> processors)
           
private  java.util.Map<java.lang.String,java.lang.String> initProcessorOptions(Context context)
           
private  java.util.Set<java.lang.String> initUnmatchedProcessorOptions()
           
static boolean isValidOptionName(java.lang.String optionName)
           
private  boolean moreToDo()
           
private  boolean needClassLoader(java.lang.String procNames, java.lang.Iterable<? extends java.io.File> workingpath)
           
static java.net.URL[] pathToURLs(java.lang.String path)
          Utility method for converting a search path string to an array of directory and JAR file URLs.
private  void printRoundInfo(java.io.PrintWriter xout, int roundNumber, List<Symbol.ClassSymbol> topLevelClasses, java.util.Set<TypeElement> annotationsPresent, boolean lastRound)
           
private  void runLastRound(java.io.PrintWriter xout, int roundNumber, boolean errorStatus, TaskListener taskListener)
           
 java.lang.String toString()
           
private  void updateProcessingState(Context currentContext, boolean lastRound)
           
private  void warnIfUnmatchedOptions()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

options

Options options

printProcessorInfo

private final boolean printProcessorInfo

printRounds

private final boolean printRounds

verbose

private final boolean verbose

lint

private final boolean lint

procOnly

private final boolean procOnly

fatalErrors

private final boolean fatalErrors

filer

private final JavacFiler filer

messager

private final JavacMessager messager

elementUtils

private final JavacElements elementUtils

typeUtils

private final JavacTypes typeUtils

discoveredProcs

private JavacProcessingEnvironment.DiscoveredProcessors discoveredProcs
Holds relevant state history of which processors have been used.


processorOptions

private final java.util.Map<java.lang.String,java.lang.String> processorOptions
Map of processor-specific options.


unmatchedProcessorOptions

private final java.util.Set<java.lang.String> unmatchedProcessorOptions

platformAnnotations

private final java.util.Set<java.lang.String> platformAnnotations
Annotations implicitly processed and claimed by javac.


specifiedPackages

private java.util.Set<Symbol.PackageSymbol> specifiedPackages
Set of packages given on command line.


log

Log log
The log to be used for error reporting.


source

Source source
Source level of the compile.


messages

private JavacMessages messages
JavacMessages object used for localization


context

private Context context

treeCleaner

private static TreeScanner treeCleaner

allMatches

private static final java.util.regex.Pattern allMatches

noMatches

private static final java.util.regex.Pattern noMatches
Constructor Detail

JavacProcessingEnvironment

public JavacProcessingEnvironment(Context context,
                                  java.lang.Iterable<? extends Processor> processors)
Method Detail

initPlatformAnnotations

private java.util.Set<java.lang.String> initPlatformAnnotations()

initProcessorIterator

private void initProcessorIterator(Context context,
                                   java.lang.Iterable<? extends Processor> processors)

handleServiceLoaderUnavailability

private java.util.Iterator<Processor> handleServiceLoaderUnavailability(java.lang.String key,
                                                                        java.lang.Exception e)
Returns an empty processor iterator if no processors are on the relevant path, otherwise if processors are present, logs an error. Called when a service loader is unavailable for some reason, either because a service loader class cannot be found or because a security policy prevents class loaders from being created.

Parameters:
key - The resource key to use to log an error message
e - If non-null, pass this exception to Abort

handleException

private void handleException(java.lang.String key,
                             java.lang.Exception e)
Handle a security exception thrown during initializing the Processor iterator.


atLeastOneProcessor

public boolean atLeastOneProcessor()

initProcessorOptions

private java.util.Map<java.lang.String,java.lang.String> initProcessorOptions(Context context)

initUnmatchedProcessorOptions

private java.util.Set<java.lang.String> initUnmatchedProcessorOptions()

discoverAndRunProcs

private void discoverAndRunProcs(Context context,
                                 java.util.Set<TypeElement> annotationsPresent,
                                 List<Symbol.ClassSymbol> topLevelClasses,
                                 List<Symbol.PackageSymbol> packageInfoFiles)

callProcessor

private boolean callProcessor(Processor proc,
                              java.util.Set<? extends TypeElement> tes,
                              RoundEnvironment renv)

doProcessing

public JavaCompiler doProcessing(Context context,
                                 List<JCTree.JCCompilationUnit> roots,
                                 List<Symbol.ClassSymbol> classSymbols,
                                 java.lang.Iterable<? extends Symbol.PackageSymbol> pckSymbols)
                          throws java.io.IOException
Throws:
java.io.IOException

runLastRound

private void runLastRound(java.io.PrintWriter xout,
                          int roundNumber,
                          boolean errorStatus,
                          TaskListener taskListener)
                   throws java.io.IOException
Throws:
java.io.IOException

updateProcessingState

private void updateProcessingState(Context currentContext,
                                   boolean lastRound)

warnIfUnmatchedOptions

private void warnIfUnmatchedOptions()

printRoundInfo

private void printRoundInfo(java.io.PrintWriter xout,
                            int roundNumber,
                            List<Symbol.ClassSymbol> topLevelClasses,
                            java.util.Set<TypeElement> annotationsPresent,
                            boolean lastRound)

enterNewClassFiles

private ListBuffer<Symbol.ClassSymbol> enterNewClassFiles(Context currentContext)

close

public void close()
Free resources related to annotation processing.

Specified by:
close in interface java.io.Closeable

getTopLevelClasses

private List<Symbol.ClassSymbol> getTopLevelClasses(List<? extends JCTree.JCCompilationUnit> units)

getPackageInfoFiles

private List<Symbol.PackageSymbol> getPackageInfoFiles(List<? extends JCTree.JCCompilationUnit> units)

contextForNextRound

private Context contextForNextRound(Context context,
                                    boolean shareNames)
                             throws java.io.IOException
Throws:
java.io.IOException

needClassLoader

private boolean needClassLoader(java.lang.String procNames,
                                java.lang.Iterable<? extends java.io.File> workingpath)

cleanTrees

private static <T extends JCTree> List<T> cleanTrees(List<T> nodes)

moreToDo

private boolean moreToDo()

getOptions

public java.util.Map<java.lang.String,java.lang.String> getOptions()
Command line options suitable for presenting to annotation processors. "-Afoo=bar" should be "-Afoo" => "bar".

Specified by:
getOptions in interface ProcessingEnvironment
Returns:
the processor-specific options passed to the tool

getMessager

public Messager getMessager()
Description copied from interface: ProcessingEnvironment
Returns the messager used to report errors, warnings, and other notices.

Specified by:
getMessager in interface ProcessingEnvironment
Returns:
the messager

getFiler

public Filer getFiler()
Description copied from interface: ProcessingEnvironment
Returns the filer used to create new source, class, or auxiliary files.

Specified by:
getFiler in interface ProcessingEnvironment
Returns:
the filer

getElementUtils

public JavacElements getElementUtils()
Description copied from interface: ProcessingEnvironment
Returns an implementation of some utility methods for operating on elements

Specified by:
getElementUtils in interface ProcessingEnvironment
Returns:
element utilities

getTypeUtils

public JavacTypes getTypeUtils()
Description copied from interface: ProcessingEnvironment
Returns an implementation of some utility methods for operating on types.

Specified by:
getTypeUtils in interface ProcessingEnvironment
Returns:
type utilities

getSourceVersion

public SourceVersion getSourceVersion()
Description copied from interface: ProcessingEnvironment
Returns the source version that any generated source and class files should conform to.

Specified by:
getSourceVersion in interface ProcessingEnvironment
Returns:
the source version to which generated source and class files should conform to
See Also:
Processor.getSupportedSourceVersion()

getLocale

public java.util.Locale getLocale()
Description copied from interface: ProcessingEnvironment
Returns the current locale or null if no locale is in effect. The locale can be be used to provide localized messages.

Specified by:
getLocale in interface ProcessingEnvironment
Returns:
the current locale or null if no locale is in effect

getSpecifiedPackages

public java.util.Set<Symbol.PackageSymbol> getSpecifiedPackages()

pathToURLs

public static java.net.URL[] pathToURLs(java.lang.String path)
Utility method for converting a search path string to an array of directory and JAR file URLs.

Parameters:
path - the search path string
Returns:
the resulting array of directory and JAR file URLs

fileToURL

private static java.net.URL fileToURL(java.io.File file)
Returns the directory or JAR file URL corresponding to the specified local file name.

Parameters:
file - the File object
Returns:
the resulting directory or JAR file URL, or null if unknown

importStringToPattern

public static java.util.regex.Pattern importStringToPattern(java.lang.String s,
                                                            Processor p,
                                                            Log log)
Convert import-style string to regex matching that string. If the string is a valid import-style string, return a regex that won't match anything.


getContext

public Context getContext()
For internal use by Sun Microsystems only. This method will be removed without warning.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

isValidOptionName

public static boolean isValidOptionName(java.lang.String optionName)