Package kilim.tools

Class Javac


  • public class Javac
    extends java.lang.Object
    Simple utility class to invoke the java compiler.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Javac.ClassPath
      a collection of class path elements
      private static class  Javac.SourceInfo  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static java.util.regex.Pattern classNameRegexp  
      (package private) static java.util.regex.Pattern publicClassNameRegexp  
    • Constructor Summary

      Constructors 
      Constructor Description
      Javac()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static void add​(java.util.ArrayList<java.lang.String> list, java.lang.String... vals)  
      private static void addClasses​(java.util.List<ClassInfo> ret, java.lang.String pkgName, java.io.File dir)  
      static java.util.List<ClassInfo> compile​(java.util.List<java.lang.String> srcCodes)
      Given a list of file-scope java code (equivalent to a .java file, including package and import declarations), compile() invokes javac to compile them, produce classfiles and return a list of pairs.
      private static void deleteDir​(java.io.File rootDir)  
      static Javac.ClassPath getClassPath​(java.lang.Class start, java.lang.ClassLoader end)
      get the class path comprising the paths of URL class loaders ancestors
      private static Javac.SourceInfo getSourceInfo​(java.lang.String srcCode)  
      private static java.util.List<Javac.SourceInfo> getSourceInfos​(java.util.List<java.lang.String> srcCodes)  
      private static java.io.File getTmpDir()  
      private static byte[] readFile​(java.io.File f)  
      private static void writeFile​(java.io.File f, byte[] srcCode)  
      • Methods inherited from class java.lang.Object

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

      • publicClassNameRegexp

        static java.util.regex.Pattern publicClassNameRegexp
      • classNameRegexp

        static java.util.regex.Pattern classNameRegexp
    • Constructor Detail

      • Javac

        public Javac()
    • Method Detail

      • compile

        public static java.util.List<ClassInfo> compile​(java.util.List<java.lang.String> srcCodes)
                                                 throws java.io.IOException
        Given a list of file-scope java code (equivalent to a .java file, including package and import declarations), compile() invokes javac to compile them, produce classfiles and return a list of pairs. compile() dumps the source strings into their respective files, has javac compile them, then reads back the equivalent class files. The name of the source file is gleaned from the string itself; a string containing "public class Foo" is stored in tmpDir/Foo.java (where tmpDir is a temporary directory that's deleted after the compilation), and if no public class or interface is found, the name of the first class in the string is used. Note that the list of returned classes may be larger than list of sources Note: the java compiler api is ill-defined and this class should not be considered production. specifically, the classpath appears to depend on the execution environment, eg command line maven vs IDE vs the java command line
        Parameters:
        srcCodes - . List of strings.
        Returns:
        List. className is fully qualified, and byte[] contains the bytecode of the class.
        Throws:
        java.io.IOException
      • add

        static void add​(java.util.ArrayList<java.lang.String> list,
                        java.lang.String... vals)
      • getClassPath

        public static Javac.ClassPath getClassPath​(java.lang.Class start,
                                                   java.lang.ClassLoader end)
        get the class path comprising the paths of URL class loaders ancestors
        Parameters:
        start - start with the class loader that loaded this object, or null for this method's class
        end - the last classloader to consider, or null to include everything up to but not including the system class loader
        Returns:
        the URLs
      • getSourceInfos

        private static java.util.List<Javac.SourceInfo> getSourceInfos​(java.util.List<java.lang.String> srcCodes)
      • getSourceInfo

        private static Javac.SourceInfo getSourceInfo​(java.lang.String srcCode)
      • getTmpDir

        private static java.io.File getTmpDir()
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • deleteDir

        private static void deleteDir​(java.io.File rootDir)
      • addClasses

        private static void addClasses​(java.util.List<ClassInfo> ret,
                                       java.lang.String pkgName,
                                       java.io.File dir)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • readFile

        private static byte[] readFile​(java.io.File f)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFile

        private static void writeFile​(java.io.File f,
                                      byte[] srcCode)
                               throws java.io.IOException
        Throws:
        java.io.IOException