Class: Naether::Java::Ruby
- Inherits:
-
Object
- Object
- Naether::Java::Ruby
- Defined in:
- lib/naether/java/ruby.rb
Instance Attribute Summary (collapse)
-
- (Object) class_loader
readonly
Returns the value of attribute class_loader.
-
- (Object) loaded_paths
readonly
Returns the value of attribute loaded_paths.
Instance Method Summary (collapse)
-
- (java.util.ArrayList) convert_to_java_list(ruby_array)
Convert a Ruby Array to a java.util.ArrayList.
-
- (Array) convert_to_ruby_array(java_array, to_string = false)
Convert a java,util.List to a Ruby Array.
-
- (Hash) convert_to_ruby_hash(java_hash, to_string = false)
Convert a java.util.Map to a Ruby Hash.
-
- (Object) create(target_class, *args)
Create a Java Object from the Naether Class Loader.
-
- (Object) exec_static_method(target_class, target_method, params, types = nil)
Execute a Staic method on a Java class from the Naether Class Loader.
-
- (Ruby) initialize
constructor
Creates new instance by loading the Naether jar to the parent ClassLoader and creating the internal Naether ClassLoader.
-
- (Object) internal_load_paths(paths)
Load a path into the internal Naether ClassLoader.
-
- (Object) load_paths(paths)
Load a path onto the parent ClassLoader.
Constructor Details
- (Ruby) initialize
Creates new instance by loading the Naether jar to the parent ClassLoader and creating the internal Naether ClassLoader
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/naether/java/ruby.rb', line 19 def initialize require 'rjb' naether_jar = Naether::Configuration.naether_jar # Call Rjb::load with an empty classpath, incase Rjb::load has already been called java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split Rjb::load("", java_opts) @loaded_paths = [] load_paths( naether_jar ) class_loader_class = Rjb::import( "com.tobedevoured.naether.PathClassLoader" ) @class_loader = class_loader_class.new() internal_load_paths( naether_jar ) end |
Instance Attribute Details
- (Object) class_loader (readonly)
Returns the value of attribute class_loader
13 14 15 |
# File 'lib/naether/java/ruby.rb', line 13 def class_loader @class_loader end |
- (Object) loaded_paths (readonly)
Returns the value of attribute loaded_paths
13 14 15 |
# File 'lib/naether/java/ruby.rb', line 13 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
140 141 142 143 144 145 146 147 |
# File 'lib/naether/java/ruby.rb', line 140 def convert_to_java_list( ruby_array ) list = Rjb::import("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
156 157 158 159 160 161 162 163 164 |
# File 'lib/naether/java/ruby.rb', line 156 def convert_to_ruby_array( java_array, to_string = false ) ruby_array = java_array.toArray() if to_string ruby_array = ruby_array.map { |x| x.toString()} end ruby_array end |
- (Hash) convert_to_ruby_hash(java_hash, to_string = false)
Convert a java.util.Map to a Ruby Hash
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/naether/java/ruby.rb', line 173 def convert_to_ruby_hash( java_hash, to_string = false ) hash = {} unless java_hash.is_a? Hash keys = java_hash.keySet() iterator = keys.iterator() if to_string while iterator.hasNext() key = iterator.next().toString() hash[key] = java_hash.get( key ).toString() end else while iterator.hasNext() key = iterator.next() hash[key] = java_hash.get( key ) end end else if to_string java_hash.each do |k,v| hash[k.toString()] = v.toString() end else hash = java_hash end end hash end |
- (Object) create(target_class, *args)
Create a Java Object from the Naether Class Loader
43 44 45 46 47 48 49 50 |
# File 'lib/naether/java/ruby.rb', line 43 def create( target_class, *args ) #@class_loader.newInstance(target_class, *args ) if args.size > 0 @class_loader._invoke('newInstance', 'Ljava.lang.String;[Ljava.lang.Object;', target_class, args ) else @class_loader._invoke('newInstance', 'Ljava.lang.String;', target_class ) end 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
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 |
# File 'lib/naether/java/ruby.rb', line 60 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 result = nil if params.nil? result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;', target_class, target_method ) elsif types.nil? result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;Ljava.util.List;', target_class, target_method, convert_to_java_list(params) ) else result = @class_loader._invoke('execStaticMethod','Ljava.lang.String;Ljava.lang.String;Ljava.util.List;Ljava.util.List;', target_class, target_method, convert_to_java_list(params), convert_to_java_list(types) ) end unless result.nil? # Force toString on java.lang.String otherwise the result will be a Rjb::Proxy if result.getClass().getName() == 'java.lang.String' result.toString() else result end end end |
- (Object) internal_load_paths(paths)
Load a path into the internal Naether ClassLoader
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/naether/java/ruby.rb', line 94 def internal_load_paths(paths) loadable_paths = [] unless paths.is_a? Array paths = [paths] end paths.each do |path| = File.(path) if File.exists?( ) @class_loader.addPath( path ) loadable_paths << end end loadable_paths end |
- (Object) load_paths(paths)
Load a path onto the parent ClassLoader
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/naether/java/ruby.rb', line 116 def load_paths(paths) loadable_paths = [] unless paths.is_a? Array paths = [paths] end paths.each do |path| = File.(path) if !@loaded_paths.include?() && File.exists?( ) @loaded_paths << Rjb::add_jar( ) loadable_paths << end end loadable_paths end |