JRuby and static imports
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 |