// (c) MIT 2003.  All rights reserved.

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	ThrowableDynamicCompiler.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	July, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:     Dynamic compiler for creating throwable classes
//
////////////////////////////////////////////////////////////////////////////////

package rec.util;

import java.util.*;

public final class ThrowableDynamicCompiler
{
  public static Class createException( String className )
  {
    return( create( className, "DynamicException" ) );
  }

  public static Class createError( String className )
  {
    return( create( className, "DynamicError" ) );
  }

  //////////////////////////////////////////////////////////////////////////////

  private static Class create( String className,
			       String superClassName )
  {
    initializeCompiler();

    Class c = null;

    try {
      c = s_Compiler.compileClass(
	createDefinition( className, superClassName ), className );
    }
    catch( CompilationException e ) {
      Assert.fatal( "compilation error in " + className );
    }

    if( c == null ) {
      Assert.fatal( "null class loaded" );
    }

    return( c );
  }

  private static String createDefinition( String className,
					  String superClassName )
  {
    return( "package rec.dynamic;\n\n"
	    + "import rec.util.*;\n\n"
	    + "public class " + className
	    + " extends " + superClassName
	    + "\n{\n  public " + className
	    + "()\n  {\n  }\n\n  public "
            + className + "( String msg )\n  {\n"
	    + "    super( msg );\n  }\n}" );
  }

  private static void initializeCompiler()
  {
    if( s_IsInitialized )
      return;

    s_Compiler.setValue( "userDirPath", "/tmp" );
    s_Compiler.setValue( "rootDirName", "JavaDynamicClasses" );

    s_IsInitialized = true;
  }

  //////////////////////////////////////////////////////////////////////////////

  private static DynamicCompiler s_Compiler
    = new DynamicCompiler();
  private static boolean s_IsInitialized = false;
}

////////////////////////////////////////////////////////////////////////////////
