Sha256: b9b94fa02000f51d0b38a5517afdda888172ecaf72c799228fac0d38884b1770

Contents?: true

Size: 1.58 KB

Versions: 33

Compression:

Stored size: 1.58 KB

Contents

class Class
  class IOWriter < Java.java.io.Writer
    def initialize(io)
      @io = io
    end
    
    def write(cbuf, off, len)
      @io.write(cbuf.unpack("U*")[off..len].pack("U*"))
    end

    def flush
      @io.flush
    end

    def close
      @io.close
    end
  end

  # 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.
  def native_impl(lib)
    require "#{lib}.jar"

    class << self
      def new(*args)
        begin
          java_class.new(*javaify(args))
        rescue ArgumentError => e
          e.message << "\n#{java_class.name}"
          raise e
        rescue NameError => e
          e.message << "\n args: #{args.inspect}" 
          raise e
        end
      end

      def javaify(arg)
        if Array === arg
          arg.map{|a| javaify(a)}
        else
          case(arg)
          when Regexp
            java.util.regex.Pattern.compile(arg.source)
          when Symbol
            arg.to_s
          when IO
            IOWriter.new(arg)
          else
            arg
          end
        end
      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

Version data entries

33 entries across 31 versions & 4 rubygems

Version Path
candlepin-api-0.4.0 bundle/ruby/gems/gherkin-2.11.1/lib/gherkin/native/java.rb
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/gherkin-2.11.1/lib/gherkin/native/java.rb
candlepin-api-0.4.0 bundle/ruby/1.8/gems/gherkin-2.11.1/lib/gherkin/native/java.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/gherkin-2.11.6/lib/gherkin/native/java.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/gherkin-2.11.6/lib/gherkin/native/java.rb
gherkin-2.11.6 lib/gherkin/native/java.rb
gherkin-2.11.6-x86-mswin32 lib/gherkin/native/java.rb
gherkin-2.11.6-x86-mingw32 lib/gherkin/native/java.rb
gherkin-2.11.6-java lib/gherkin/native/java.rb
gherkin-2.11.5 lib/gherkin/native/java.rb
gherkin-2.11.5-x86-mswin32 lib/gherkin/native/java.rb
gherkin-2.11.5-x86-mingw32 lib/gherkin/native/java.rb
gherkin-2.11.5-java lib/gherkin/native/java.rb
gherkin-2.11.4 lib/gherkin/native/java.rb
gherkin-2.11.4-x86-mswin32 lib/gherkin/native/java.rb
gherkin-2.11.4-x86-mingw32 lib/gherkin/native/java.rb
gherkin-2.11.4-java lib/gherkin/native/java.rb
gherkin-2.11.3 lib/gherkin/native/java.rb
gherkin-2.11.3-x86-mswin32 lib/gherkin/native/java.rb
gherkin-2.11.3-x86-mingw32 lib/gherkin/native/java.rb