Class: Inline::Java

Inherits:
Object
  • Object
show all
Defined in:
lib/java_inline.rb

Overview

A Java builder for RubyInline. Provides the basic methods needed to allow assembling a set of Java methods that compile into a class and get bound to the same names in the containing module.

Constant Summary collapse

JFile =
java.io.File

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Java

Returns a new instance of Java.



19
20
21
22
23
24
# File 'lib/java_inline.rb', line 19

def initialize(mod)
  @context = mod
  @src = ""
  @imports = []
  @sigs = []
end

Instance Method Details

#buildObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/java_inline.rb', line 56

def build
  compiler = ToolProvider.system_java_compiler
  file_mgr = compiler.get_standard_file_manager(nil, nil, nil)
  file_mgr.set_location(StandardLocation::CLASS_OUTPUT, [JFile.new(Inline.directory)])
  
  if @pkg
    directory = "#{Inline.directory}/#{@pkg.gsub('.', '/')}"
    unless File.directory? directory then
      $stderr.puts "NOTE: creating #{directory} for RubyInline" if $DEBUG
      FileUtils.mkdir_p directory
    end
    
    @name = "Java#{@src.hash.abs}"
    @load_name = "#{@pkg}.#{@name}"
    filename = "#{directory}/#{@name}.java"
  
    imports = "import " + @imports.join(";\nimport ") + ";" if @imports.size > 0
    full_src = "
      package #{@pkg};
      #{imports}
      public class #{@name} {
      #{@src}
      }
    "
  else
    @load_name = @name = "Java#{@src.hash.abs}"
    filename = "#{Inline.directory}/#{@name}.java"
  
    imports = "import " + @imports.join(";\nimport ") + ";" if @imports.size > 0
    full_src = "
      #{imports}
      public class #{@name} {
      #{@src}
      }
    "
  end
  
  File.open(filename, "w") {|file| file.write(full_src)}
  file_objs = file_mgr.get_java_file_objects_from_strings([filename])
  
  compiler.get_task(nil, file_mgr, nil, nil, nil, file_objs).call
  file_mgr.close
end

#import(cls) ⇒ Object

Add an “import” line with the given class, as in builder.import “java.util.ArrayList”. The imports will be composed into an appropriate block of code and added to the top of the source.



39
40
41
42
43
44
45
# File 'lib/java_inline.rb', line 39

def import(cls)
  if cls.respond_to? :java_class
    @imports << cls.java_class.name
  else
    @imports << cls.to_s
  end
end

#java(src) ⇒ Object

Add a Java method to the built Java source. This expects the method to be public and static, so it can be called as a function.



49
50
51
52
53
54
# File 'lib/java_inline.rb', line 49

def java(src)
  @src << src << "\n"
  signature = src.match(/public static\W+(\w+)\W+([a-zA-Z0-9_]+)\((.*)\)/)
  raise "Could not parse method signature" unless signature
  @sigs << [signature[1], signature[2], signature[3]]
end

#loadObject



100
101
102
103
104
105
# File 'lib/java_inline.rb', line 100

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

#load_cacheObject



26
27
28
# File 'lib/java_inline.rb', line 26

def load_cache
  false
end

#package(pkg) ⇒ Object

Set the package to use for the Java class being generated, as in builder.package “org.foo.bar”



32
33
34
# File 'lib/java_inline.rb', line 32

def package(pkg)
  @pkg = pkg
end