edu.mit.ai.psg.jeva
Class CompileUnit

java.lang.Object
  |
  +--edu.mit.ai.psg.jeva.CompileUnit

public class CompileUnit
extends Object

CompileUnit -- compile a Java compilation unit (i.e., contents of a .java file) in a string.

By default uses access to temporary file space (via File.createTempFile). To reuse compilations between runs (e.g., while debugging), call setTempRoot(java.io.File). (This directory should be used only for compilations, as any source files will be overwritten!)

Currently uses the Sun Java Development Kit JDK1.2 sun.tools.javac compiler (preferrably by including jdk1.2/lib/tools.jar in the classpath, though if not there will try to invoke javac as a process, which requires javac in the command path).


Inner Class Summary
static interface CompileUnit.CompilerCommand
          interface for invoking compilers using command line arguments
static class CompileUnit.CompilerInOwnProcess
           
static class CompileUnit.JavaCInProcess
           
 
Field Summary
static CompileUnit.CompilerCommand[] compilers
          CompilerCommands tried in order until one returns rather than throwing an exception.
static String[] javacOptions
          command options for java compiler, e.g., new String[]{"-g"}
static boolean suppressClassFileDeletion
          Specifies whether or not to delete .class files on exit.
 
Constructor Summary
CompileUnit()
           
 
Method Summary
static Class compile(String className, String compilationUnit)
          Write compilation unit out to temp, run javac compiler on it, and load className.
static Class compile(String className, String compilationUnit, OutputStream outStream, OutputStream errStream)
          Write compilation unit out to temp, run javac compiler on it, and load className.
static Class compileFileInTemp(String className)
          Calls compileFileInTemp on class using System.out and System.err
static Class compileFileInTemp(String className, OutputStream outStream, OutputStream errStream)
          Calls java compiler on file previously written with writeCompilationUnitToTemp(String, String), adding temp file space to classpath.
static long dateClassInTemp(String className)
          may be used to see if a class compiled in a previous session may be reloaded rather than recompiled.
static Class loadClassUsingTemp(String className)
          Loads classes from files compiled in temp.
static void setTempRoot(File newTempRoot)
          set temp root directory for compilation files
static void writeCompilationUnitToTemp(String className, String compilationUnit)
          Write compilation unit to temp.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

suppressClassFileDeletion

public static boolean suppressClassFileDeletion
Specifies whether or not to delete .class files on exit. This is set to false if compilation directory is a temporary directory, and is set to true if compilation directory is specified by setTempRoot(java.io.File) so .class files may be reused by subsequent runs

javacOptions

public static String[] javacOptions
command options for java compiler, e.g., new String[]{"-g"}

compilers

public static CompileUnit.CompilerCommand[] compilers
CompilerCommands tried in order until one returns rather than throwing an exception. Default value is
. new CompilerCommand[]{new JavacInProcess(), 
.                       new CompilerInOwnProcess("javac")};
(tries in-process first [faster]).
Constructor Detail

CompileUnit

public CompileUnit()
Method Detail

compile

public static Class compile(String className,
                            String compilationUnit)
Write compilation unit out to temp, run javac compiler on it, and load className. Use System.out and System.err for compiler messages.
Parameters:
className - full class name of class (e.g., myOrg.myPkg.MyClass)
compilationUnit - java source code for class, (e.g., contents of its .java file)

compile

public static Class compile(String className,
                            String compilationUnit,
                            OutputStream outStream,
                            OutputStream errStream)
Write compilation unit out to temp, run javac compiler on it, and load className.
Parameters:
className - full class name of class (e.g., myOrg.myPkg.MyClass)
compilationUnit - java source code for class, (e.g., contents of its .java file)
outStream - output stream for compiler messages
errStream - output stream for compiler messages

writeCompilationUnitToTemp

public static void writeCompilationUnitToTemp(String className,
                                              String compilationUnit)
Write compilation unit to temp. (Temporary file space is obtained using File.createTempFile(String, String, File) unless other location is specified by setTempRoot(java.io.File).) Used for writing out multiple compilation units that depend on each other before compiling one.
Parameters:
className - full class name of class (e.g., myOrg.myPkg.MyClass)
compilationUnit - java source code for class, (e.g., contents of its .java file)

compileFileInTemp

public static Class compileFileInTemp(String className)
Calls compileFileInTemp on class using System.out and System.err

compileFileInTemp

public static Class compileFileInTemp(String className,
                                      OutputStream outStream,
                                      OutputStream errStream)
Calls java compiler on file previously written with writeCompilationUnitToTemp(String, String), adding temp file space to classpath. Tries compilers in order until one is accessible and reports success or failure. For each compiler, supplies arguments javacOptions -classpath classPath, where classPath is from System.getProperty("java.class.path") augmented with tempRoot.
Parameters:
className - full class name of previously written compilation unit to compile.
outStream - output stream for compiler messages
errStream - output stream for compiler messages

loadClassUsingTemp

public static Class loadClassUsingTemp(String className)
Loads classes from files compiled in temp. If not found, calls Class.forName(String)

dateClassInTemp

public static long dateClassInTemp(String className)
may be used to see if a class compiled in a previous session may be reloaded rather than recompiled. Returns date in milliseconds or 0L if no .class file exists or I/O error occurs.
See Also:
File.lastModified()

setTempRoot

public static void setTempRoot(File newTempRoot)
set temp root directory for compilation files

Copyright (c) 1996-1999
Massachusetts Institute of Technology

Feedback: jeva-feedback@ai.mit.edu