Class: Class

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

Instance Method Summary collapse

Instance Method Details

#java_impl(jar) ⇒ Object

Causes a Java class to be instantiated instead of the Ruby class when running on JRuby. This is used to test both pure Java and pure Ruby classes from the same Ruby based test suite. The Java Class must have a package name that corresponds with the Ruby class.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gherkin/java_impl.rb', line 6

def java_impl(jar)
  if defined?(JRUBY_VERSION)
    require jar
    class << self
      def javaify(arg)
        if Array === arg
          arg.map{|a| javaify(a)}
        else
          case(arg)
          when Regexp
            java.util.regex.Pattern.compile(arg.source)
          else
            arg
          end
        end
      end

      def new(*args)
        java_class.new(*javaify(args))
      end

      def ===(object)
        super || object.java_kind_of?(java_class)
      end

      def java_class
        names = self.name.split('::')
        package = Java
        names[0..-2].each do |module_name|
          package = package.__send__(module_name.downcase)
        end

        package.__send__(names[-1])
      end
    end
  end
end