Class: Naether::Java::JRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/naether/java/jruby.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (JRuby) initialize

Creates new instance by loading the Naether jar to the parent ClassLoader and creating the internal Naether ClassLoader



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/naether/java/jruby.rb', line 20

def initialize
  require 'java'
  
  naether_jar = Naether::Configuration.naether_jar
        
  @loaded_paths = []
  
  load_paths(naether_jar)  
  @class_loader = com.tobedevoured.naether.PathClassLoader.new()
  internal_load_paths(naether_jar)  
end

Instance Attribute Details

- (Object) class_loader (readonly)

Returns the value of attribute class_loader



14
15
16
# File 'lib/naether/java/jruby.rb', line 14

def class_loader
  @class_loader
end

- (Object) loaded_paths (readonly)

Returns the value of attribute loaded_paths



14
15
16
# File 'lib/naether/java/jruby.rb', line 14

def loaded_paths
  @loaded_paths
end

Instance Method Details

- (java.util.ArrayList) convert_to_java_list(ruby_array)

Convert a Ruby Array to a java.util.ArrayList

Parameters:

  • ruby_array (Array)

    Array to convert to Java.util.ArrayList

Returns:

  • (java.util.ArrayList)


115
116
117
118
119
120
121
122
# File 'lib/naether/java/jruby.rb', line 115

def convert_to_java_list( ruby_array ) 
  list = java.util.ArrayList.new
  ruby_array.each do |item|
    list.add( item )
  end
  
  list
end

- (Array) convert_to_ruby_array(java_array, to_string = false)

Convert a java,util.List to a Ruby Array

Parameters:

  • java_array (java.util.ArrayList)
  • to_string (Boolean) (defaults to: false)

    has no affect on conversion.

Returns:

  • (Array)


131
132
133
# File 'lib/naether/java/jruby.rb', line 131

def convert_to_ruby_array( java_array, to_string = false )
  java_array.to_a
end

- (Hash) convert_to_ruby_hash(java_hash, to_string = false)

Convert a java.util.Map to a Ruby Hash

Parameters:

  • java_hash (java.util.Map)
  • to_string (Boolean) (defaults to: false)

    has no affect on conversion

Returns:

  • (Hash)


142
143
144
145
146
147
148
149
150
# File 'lib/naether/java/jruby.rb', line 142

def convert_to_ruby_hash( java_hash, to_string = false )
  hash = java_hash.to_hash
  
  hash.each do |k,v|
    if v.is_a? java.util.Map
      hash[k] = convert_to_ruby_hash(v, to_string )
    end
  end
end

- (Object) create(target_class, *args)

Create a Java Object from the Naether Class Loader

Parameters:

  • target_class (String)

    to create

  • args (Array)

    Array of constructor arguments



37
38
39
# File 'lib/naether/java/jruby.rb', line 37

def create( target_class, *args )
  @class_loader.newInstance(target_class, *args )
end

- (Object) exec_static_method(target_class, target_method, params, types = nil)

Execute a Staic method on a Java class from the Naether Class Loader

Parameters:

  • target_class (String)
  • target_method (String)
  • params (Array)

    Array of method parameters

  • types (Array) (defaults to: nil)

    if defined, a Array of String classes of params type that lines up with params one to one.

Returns:

  • (Object)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/naether/java/jruby.rb', line 49

def exec_static_method( target_class, target_method, params, types = nil ) 
  unless params.is_a? Array
    params = [params]
  end
  
  if types
    unless types.is_a? Array
       types = [types]
    end
  end
  
  @class_loader.execStaticMethod( target_class, target_method, params, types )
end

- (Object) internal_load_paths(paths)

Load a path into the internal Naether ClassLoader

Parameters:

  • paths (Array)

    as an Array of String paths or a String path



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/naether/java/jruby.rb', line 68

def internal_load_paths(paths)
  load_paths = []
  unless paths.is_a? Array
    paths = [paths]
  end
  
  paths.each do |path|
    expanded_path = File.expand_path(path)
    if File.exists?( expanded_path )
      @class_loader.addPath( expanded_path )
      
      load_paths << expanded_path
    end
  end
  
  load_paths
end

- (Object) load_paths(paths)

Load a path onto the parent ClassLoader

Parameters:

  • paths (Array)

    as an Array of String paths or a String path



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/naether/java/jruby.rb', line 91

def load_paths(paths)
  load_paths = []
  unless paths.is_a? Array
    paths = [paths]
  end
  
  paths.each do |path|
    expanded_path = File.expand_path(path)
    if !@loaded_paths.include?( expanded_path ) && File.exists?( expanded_path )
      $CLASSPATH << expanded_path
      load_paths << expanded_path
      @loaded_paths << expanded_path
    end
  end
  
  load_paths
end