Class ClassFileLoader

  • All Implemented Interfaces:
    ClassInfoLoader
    Direct Known Subclasses:
    JarFileCommitter

    public class ClassFileLoader
    extends java.lang.Object
    implements ClassInfoLoader
    ClassFileLoder provides an interface for loading classes from files. The actual loading is done by the ClassFile itself.

    Classes may be specified by their full package name (java.lang.String), or by the name of their class file (myclasses/Test.class). The class path may contain directories or Zip or Jar files. Any classes that are written back to disk ("committed") are placed in the output directory.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean DEBUG  
      static boolean USE_SYSTEM_CLASSES  
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassFileLoader()
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void appendClassPath​(java.lang.String morePath)
      Adds to the classpath (CLASSPATH = CLASSPATH + morePath).
      void done()
      Signifies that we are done with this ClassFileLoader
      java.lang.String getClassPath()
      Returns the path used to search for class files.
      ClassInfo loadClass​(java.lang.String name)
      Loads the class with the given name.
      ClassInfo[] loadClassesFromZipFile​(java.util.zip.ZipFile zipFile)
      Loads all of the classes that are contained in a zip (or jar) file.
      ClassInfo newClass​(int modifiers, int classIndex, int superClassIndex, int[] interfaceIndexes, java.util.List constants)
      Creates a new class or interface.
      java.io.File outputDir()
      Get the directory into which commited class files should be written.
      java.io.OutputStream outputStreamFor​(ClassInfo info)
      Returns an OutputStream to which a class file should be written.
      protected java.io.OutputStream outputStreamFor​(java.lang.String name)
      Returns an OutputStream to which somed named entity is written.
      void prependClassPath​(java.lang.String morePath)
      Adds to the classpath (CLASSPATH = morePath + CLASSPATH).
      void setClassPath​(java.lang.String classpath)
      Sets the classpath.
      void setOutputDir​(java.io.File dir)
      Set the directory into which commited class files should be written.
      void setVerbose​(boolean verbose)  
      void writeEntry​(byte[] bytes, java.lang.String name)
      Writes a bunch of bytes to an output entry with the given name.
      • Methods inherited from class java.lang.Object

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

      • DEBUG

        public static boolean DEBUG
      • USE_SYSTEM_CLASSES

        public static boolean USE_SYSTEM_CLASSES
    • Constructor Detail

      • ClassFileLoader

        public ClassFileLoader()
        Constructor. The classpath initially consists of the contents of the java.class.path and sun.boot.class.path system properties.
    • Method Detail

      • setVerbose

        public void setVerbose​(boolean verbose)
      • setClassPath

        public void setClassPath​(java.lang.String classpath)
        Sets the classpath.
      • appendClassPath

        public void appendClassPath​(java.lang.String morePath)
        Adds to the classpath (CLASSPATH = CLASSPATH + morePath).
      • prependClassPath

        public void prependClassPath​(java.lang.String morePath)
        Adds to the classpath (CLASSPATH = morePath + CLASSPATH).
      • getClassPath

        public java.lang.String getClassPath()
        Returns the path used to search for class files.
      • loadClassesFromZipFile

        public ClassInfo[] loadClassesFromZipFile​(java.util.zip.ZipFile zipFile)
                                           throws java.lang.ClassNotFoundException
        Loads all of the classes that are contained in a zip (or jar) file. Returns an array of the ClassInfos for the classes in the zip file.
        Throws:
        java.lang.ClassNotFoundException
      • newClass

        public ClassInfo newClass​(int modifiers,
                                  int classIndex,
                                  int superClassIndex,
                                  int[] interfaceIndexes,
                                  java.util.List constants)
        Description copied from interface: ClassInfoLoader
        Creates a new class or interface.
        Specified by:
        newClass in interface ClassInfoLoader
        Parameters:
        modifiers - The modifiers describing the newly-created class
        classIndex - The index of the name of the newly-created class in its constant pool
        superClassIndex - The index of the name of the newly-created class's superclass in its constant pool
        interfaceIndexes - The indexes of the names of the interfaces that the newly-created class implements
        constants - The constant pool for the newly created class (a list of Constants).
      • loadClass

        public ClassInfo loadClass​(java.lang.String name)
                            throws java.lang.ClassNotFoundException
        Loads the class with the given name. Searches the class path, including zip files, for the class and then returns a data stream for the class file.
        Specified by:
        loadClass in interface ClassInfoLoader
        Parameters:
        name - The name of the class to load, including the package name.
        Returns:
        A ClassInfo for the class.
        Throws:
        java.lang.ClassNotFoundException - The class cannot be found in the class path.
        See Also:
        ClassInfo
      • setOutputDir

        public void setOutputDir​(java.io.File dir)
        Set the directory into which commited class files should be written.
        Parameters:
        dir - The directory.
      • outputDir

        public java.io.File outputDir()
        Get the directory into which commited class files should be written.
      • writeEntry

        public void writeEntry​(byte[] bytes,
                               java.lang.String name)
                        throws java.io.IOException
        Writes a bunch of bytes to an output entry with the given name.
        Throws:
        java.io.IOException
      • outputStreamFor

        public java.io.OutputStream outputStreamFor​(ClassInfo info)
                                             throws java.io.IOException
        Returns an OutputStream to which a class file should be written.
        Specified by:
        outputStreamFor in interface ClassInfoLoader
        Throws:
        java.io.IOException
      • outputStreamFor

        protected java.io.OutputStream outputStreamFor​(java.lang.String name)
                                                throws java.io.IOException
        Returns an OutputStream to which somed named entity is written. Any forward slashes in the name are replaced by File.separatorChar.
        Throws:
        java.io.IOException
      • done

        public void done()
                  throws java.io.IOException
        Signifies that we are done with this ClassFileLoader
        Throws:
        java.io.IOException