module Inline
  require 'java'
  class Java
    import javax.tools.ToolProvider
    import javax.tools.SimpleJavaFileObject

    compiler = ToolProvider.system_java_compiler
    file_mgr = compiler.get_standard_file_manager(nil, nil, nil)

    # because we can't invoke the super constructor on SimpleJavaFileObject
    # (it's protected...GRR) I generate a little Java file and compile it
    # on the fly
    File.open("#{Inline.directory}/SourceString.java", "w") do |file|
      file.write < 0
      full_src = "
        #{imports}
        public class #{@name} {
        #{@src}
        }
      "
      string_src = SourceString.new(@name, full_src)
      @compiler.get_task(nil, @file_mgr, nil, nil, nil, [string_src]).call
    end

    def load
      @sigs.each do |sig|
        @context.module_eval "const_set :#{@name}, ::Java.const_get(:#{@name}); puts '#{sig[1]}'; def #{sig[1]}(*args); #{@name}.#{sig[1]}(*args); end"
      end
    end
  end
end