// This is a custom classloader impl to allow loading classes with
// interdependencies by having findClass retrieve classes as needed from the
// collection of all classes generated by the target script.
package org.mirah;

import java.security.SecureClassLoader;
import java.lang.ClassLoader;
import java.util.Map;
import java.nio.charset.Charset;

public class MirahClassLoader extends SecureClassLoader {
  private Map<String, String> class_map;

  public MirahClassLoader(ClassLoader parent, Map class_map) {
    super(parent);
    this.class_map = class_map;
  }

  public Class findClass(String name) throws ClassNotFoundException {
    if (class_map.containsKey(name)) {
      try {
        Object value = class_map.get(name);
        byte[] bytes = null;
        if (value instanceof byte[]) {
          bytes = (byte[])value;
        } else {
          bytes = ((String)value).getBytes("ISO-8859-1");
        }
        return defineClass(name.replace('/', '.'), bytes, 0, bytes.length);
      } catch (java.io.UnsupportedEncodingException ex) {
          throw new ClassNotFoundException(name, ex);
      }
    }
    throw new ClassNotFoundException(name);
  }

  public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
    Class cls = findLoadedClass(name);
    if (cls == null) {
      if (class_map.containsKey(name)) {
        cls = findClass(name);
      } else {
        cls = super.loadClass(name, false);
      }
    }
    if (resolve) {
      resolveClass(cls);
    }
    return cls;
  }
}

/*
# This is a custom classloader impl to allow loading classes with
# interdependencies by having findClass retrieve classes as needed from the
# collection of all classes generated by the target script.
import java.security.SecureClassLoader
import java.lang.ClassLoader
import java.util.Map
import java.nio.charset.Charset

class MirahClassLoader < SecureClassLoader
  # parent - the upstream classloader
  # class_map - a map of class names to strings containing the bytes of a .class file.
  def initialize(parent:ClassLoader, class_map:Map)
    super(parent)
    @class_map = class_map
  end

  def findClass(name)
    if @class_map[name]
      bytes = String(@class_map[name]).getBytes "ISO-8859-1"
      defineClass(name, bytes, 0, bytes.length)
    else
      raise ClassNotFoundException.new(name)
    end
  end

  def loadClass(name, resolve)
    cls = findLoadedClass(name)
    if cls.nil?
      if @class_map[name]
        cls = findClass(name)
      else
        cls = super(name, false)
      end
    end

    resolveClass(cls) if resolve

    cls
  end
end

*/

// This is a custom classloader impl to allow loading classes with
// interdependencies by having findClass retrieve classes as needed from the
// collection of all classes generated by the target script.
package org.mirah;

import java.security.SecureClassLoader;
import java.lang.ClassLoader;
import java.util.Map;
import java.nio.charset.Charset;

public class MirahClassLoader extends SecureClassLoader {
  private Map<String, String> class_map;

  public MirahClassLoader(ClassLoader parent, Map class_map) {
    super(parent);
    this.class_map = class_map;
  }

  public Class findClass(String name) throws ClassNotFoundException {
    if (class_map.containsKey(name)) {
      try {
        Object value = class_map.get(name);
        byte[] bytes = null;
        if (value instanceof byte[]) {
          bytes = (byte[])value;
        } else {
          bytes = ((String)value).getBytes("ISO-8859-1");
        }
        return defineClass(name.replace('/', '.'), bytes, 0, bytes.length);
      } catch (java.io.UnsupportedEncodingException ex) {
          throw new ClassNotFoundException(name, ex);
      }
    }
    throw new ClassNotFoundException(name);
  }

  public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
    Class cls = findLoadedClass(name.replace('/', '.'));
    if (cls == null) {
      if (class_map.containsKey(name)) {
        cls = findClass(name);
      } else {
        cls = super.loadClass(name, false);
      }
    }
    if (resolve) {
      resolveClass(cls);
    }
    return cls;
  }
}

/*
# This is a custom classloader impl to allow loading classes with
# interdependencies by having findClass retrieve classes as needed from the
# collection of all classes generated by the target script.
import java.security.SecureClassLoader
import java.lang.ClassLoader
import java.util.Map
import java.nio.charset.Charset

class MirahClassLoader < SecureClassLoader
  # parent - the upstream classloader
  # class_map - a map of class names to strings containing the bytes of a .class file.
  def initialize(parent:ClassLoader, class_map:Map)
    super(parent)
    @class_map = class_map
  end

  def findClass(name)
    if @class_map[name]
      bytes = String(@class_map[name]).getBytes "ISO-8859-1"
      defineClass(name, bytes, 0, bytes.length)
    else
      raise ClassNotFoundException.new(name)
    end
  end

  def loadClass(name, resolve)
    cls = findLoadedClass(name)
    if cls.nil?
      if @class_map[name]
        cls = findClass(name)
      else
        cls = super(name, false)
      end
    end

    resolveClass(cls) if resolve

    cls
  end
end

*/

Fix MirahClassLoader bug, where the binary name of the class (as of the Java Language Specification) was not properly deduced.