Class JavaParser

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class JavaParser
    extends java.lang.Object
    implements java.io.Closeable
    Utility for parsing Java code. NOTE: Forked from Google Compile Testing Project
    Since:
    1.1
    • Constructor Summary

      Constructors 
      Constructor Description
      JavaParser()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.lang.Iterable<? extends javax.tools.JavaFileObject> generate​(java.lang.String className, java.lang.String code)
      Parses sources into com.sun.source.tree.CompilationUnitTree units.
      java.lang.Iterable<? extends javax.tools.JavaFileObject> generate​(javax.tools.JavaFileObject... sources)
      Parses sources into com.sun.source.tree.CompilationUnitTree units.
      protected io.micronaut.annotation.processing.AggregatingTypeElementVisitorProcessor getAggregatingTypeElementVisitorProcessor()
      The type element visitor processor to use.
      protected java.util.List<javax.annotation.processing.Processor> getAnnotationProcessors()
      The list of processors to use.
      protected io.micronaut.annotation.processing.BeanDefinitionInjectProcessor getBeanDefinitionInjectProcessor()
      The BeanDefinitionInjectProcessor to use.
      javax.tools.JavaCompiler getCompiler()  
      javax.tools.JavaFileManager getFileManager()  
      javax.annotation.processing.Filer getFiler()  
      com.sun.source.util.JavacTask getJavacTask​(javax.tools.JavaFileObject... sources)
      gets the javac task.
      java.util.Optional<com.sun.source.util.JavacTask> getLastTask()  
      javax.annotation.processing.ProcessingEnvironment getProcessingEnv()  
      protected io.micronaut.annotation.processing.TypeElementVisitorProcessor getTypeElementVisitorProcessor()
      The type element visitor processor to use.
      java.lang.Iterable<? extends javax.lang.model.element.Element> parse​(javax.tools.JavaFileObject... sources)
      Parses sources into com.sun.source.tree.CompilationUnitTree units.
      java.lang.Iterable<? extends javax.lang.model.element.Element> parseLines​(java.lang.String className, java.lang.String... lines)
      Parses sources into compilation units.
      java.io.Reader readGenerated​(java.lang.String filePath, java.lang.String className, java.lang.String code)
      Reads the contents of a generated file as a reader.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JavaParser

        public JavaParser()
        Default constructor.
    • Method Detail

      • getCompiler

        public javax.tools.JavaCompiler getCompiler()
        Returns:
        The compiler used
      • getFileManager

        public javax.tools.JavaFileManager getFileManager()
        Returns:
        The file manager
      • getFiler

        public javax.annotation.processing.Filer getFiler()
        Returns:
        The filer
      • getProcessingEnv

        public javax.annotation.processing.ProcessingEnvironment getProcessingEnv()
        Returns:
        Dummy processing environment
      • parseLines

        public java.lang.Iterable<? extends javax.lang.model.element.Element> parseLines​(java.lang.String className,
                                                                                         java.lang.String... lines)
        Parses sources into compilation units. This method does not compile the sources.
        Parameters:
        className - The class name
        lines - The lines to parse
        Returns:
        The elements
      • parse

        public java.lang.Iterable<? extends javax.lang.model.element.Element> parse​(javax.tools.JavaFileObject... sources)
        Parses sources into com.sun.source.tree.CompilationUnitTree units. This method does not compile the sources.
        Parameters:
        sources - The sources
        Returns:
        The elements
      • getLastTask

        public java.util.Optional<com.sun.source.util.JavacTask> getLastTask()
        Returns:
        The last task that was used
      • getJavacTask

        public com.sun.source.util.JavacTask getJavacTask​(javax.tools.JavaFileObject... sources)
        gets the javac task.
        Parameters:
        sources - The sources
        Returns:
        the task
      • generate

        public java.lang.Iterable<? extends javax.tools.JavaFileObject> generate​(java.lang.String className,
                                                                                 java.lang.String code)
        Parses sources into com.sun.source.tree.CompilationUnitTree units. This method does not compile the sources.
        Parameters:
        className - The class name
        code - the raw code
        Returns:
        The generated file objects
      • readGenerated

        @Nullable
        public java.io.Reader readGenerated​(@NonNull
                                            java.lang.String filePath,
                                            java.lang.String className,
                                            java.lang.String code)
                                     throws java.io.IOException
        Reads the contents of a generated file as a reader.
        Parameters:
        filePath - The file path
        className - The class name that produces the file
        code - The code of the class
        Returns:
        The generated file
        Throws:
        java.io.IOException - when an error occurs reading the file
      • generate

        public java.lang.Iterable<? extends javax.tools.JavaFileObject> generate​(javax.tools.JavaFileObject... sources)
        Parses sources into com.sun.source.tree.CompilationUnitTree units. This method does not compile the sources.
        Parameters:
        sources - The sources
        Returns:
        The java file objects
      • getAnnotationProcessors

        @NonNull
        protected java.util.List<javax.annotation.processing.Processor> getAnnotationProcessors()
        The list of processors to use.
        Returns:
        The processor list
      • getBeanDefinitionInjectProcessor

        @NonNull
        protected io.micronaut.annotation.processing.BeanDefinitionInjectProcessor getBeanDefinitionInjectProcessor()
        The BeanDefinitionInjectProcessor to use.
        Returns:
        The BeanDefinitionInjectProcessor
      • getTypeElementVisitorProcessor

        @NonNull
        protected io.micronaut.annotation.processing.TypeElementVisitorProcessor getTypeElementVisitorProcessor()
        The type element visitor processor to use.
        Returns:
        The type element visitor processor
      • getAggregatingTypeElementVisitorProcessor

        @NonNull
        protected io.micronaut.annotation.processing.AggregatingTypeElementVisitorProcessor getAggregatingTypeElementVisitorProcessor()
        The type element visitor processor to use.
        Returns:
        The type element visitor processor
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable