daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Inline
  2.   require 'java'
  3.  
  4.   class Java
  5.     $CLASSPATH << Inline.directory
  6.    
  7.     JFile = java.io.File
  8.     import javax.tools.ToolProvider
  9.     import javax.tools.SimpleJavaFileObject
  10.     import javax.tools.StandardLocation
  11.  
  12.     def initialize(mod)
  13.       @context = mod
  14.       @src = ""
  15.       @imports = []
  16.       @sigs = []
  17.     end
  18.  
  19.     def import(cls)
  20.       @imports << cls
  21.     end
  22.    
  23.     def load_cache
  24.       false
  25.     end
  26.  
  27.     def java(src)
  28.       @src << src << "\n"
  29.       signature = @src.match(/public static\W+(\w+)\W+([a-zA-Z0-9_]+)\((.*)\)/)
  30.       raise "Could not parse method signature" unless signature
  31.       @sigs << [signature[1], signature[2], signature[3]]
  32.     end
  33.  
  34.     def build
  35.       compiler = ToolProvider.system_java_compiler
  36.       file_mgr = compiler.get_standard_file_manager(nil, nil, nil)
  37.       file_mgr.set_location(StandardLocation::CLASS_OUTPUT, [JFile.new(Inline.directory)])
  38.      
  39.       @name = "Java#{@src.hash.abs}"
  40.      
  41.       imports = "import " + @imports.join(";\nimport ") + ";" if @imports.size > 0
  42.       full_src = "
  43.         #{imports}
  44.         public class #{@name} {
  45.         #{@src}
  46.         }
  47.       "
  48.      
  49.       filename = "#{Inline.directory}/#{@name}.java"
  50.       File.open(filename, "w") {|file| file.write(full_src)}
  51.       file_objs = file_mgr.get_java_file_objects_from_strings([filename])
  52.      
  53.       compiler.get_task(nil, file_mgr, nil, nil, nil, file_objs).call
  54.       file_mgr.close
  55.     end
  56.  
  57.     def load
  58.       @sigs.each do |sig|
  59.         @context.module_eval "const_set :#{@name}, ::Java.const_get(:#{@name}); def #{sig[1]}(*args); #{@name}.#{sig[1]}(*args); end"
  60.       end
  61.     end
  62.   end
  63. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top