Ketan's Musings

Where he blogs about his eclipse musings

JRuby and static imports

without comments

The last few weeks have been interesting working with SWT and some newly acquired (J)Ruby chops, to hack together a couple of approaches to build a recorder for SWTBot. Here is one of the interesting hacks which was cobbled together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'java'
module Recorder
  module SWTInitializer
 
      include_package 'org.eclipse.swt'
      include_package 'org.eclipse.swt.layout'
      include_package 'org.eclipse.swt.widgets'
      include_package 'org.eclipse.swt.custom'
      include_packate  'org.eclipse.swtbot.swt.finder'
 
      # loop through a few known classes to implement static imports
      def method_missing(method, *args, &block)
        klass = [
            org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory,
            org.eclipse.swtbot.swt.finder.utils.SWTUtils].find do |k|
          k.respond_to?(method)
        end
        return klass.send(method, *args, &block) if klass
        super
      end
  end
end

To use this class:

1
2
3
4
5
6
7
8
class Application
  include Recorder::SWTInitializer
  # the with_text invocation falls into the method_missing 
  # which delegates it to WidgetMatcherFactory
  def self.run
    SWTBot.new().widgets(with_text('hello'))
  end
end

Written by Ketan

September 29th, 2010 at 7:57 am

Posted in Uncategorized

Tagged with , , ,

Leave a Reply

IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)

What is 6 + 14 ?
Please leave these two fields as-is: