01: package org.uispec4j.interception;
02:
03: import org.uispec4j.Trigger;
04: import org.uispec4j.UISpecAdapter;
05: import org.uispec4j.Window;
06:
07: import java.lang.reflect.Method;
08:
09: /**
10: * Adapter that intercepts the window displayed by the main() of a given class.<p/>
11: * This adapter keeps the reference of the intercepted window, so that main() is not called on
12: * subsequent calls. If you need to run main() again, you can either call {@link #reset()} or create a new
13: * adapter.
14: */
15: public class MainClassAdapter implements UISpecAdapter {
16: private Method main;
17: private String[] args;
18: private Window window;
19:
20: public MainClassAdapter(Class mainClass, String[] args) {
21: this .args = args;
22: try {
23: main = mainClass.getMethod("main", new Class[] { args
24: .getClass() });
25: } catch (NoSuchMethodException e) {
26: throw new RuntimeException(
27: "Class "
28: + mainClass.getName()
29: + " has no method: public static void main(String[])");
30: }
31: }
32:
33: public Window getMainWindow() {
34: if (window == null) {
35: window = intercept();
36: }
37: return window;
38: }
39:
40: private Window intercept() {
41: return WindowInterceptor.run(new Trigger() {
42: public void run() throws Exception {
43: main.invoke(null, new Object[] { args });
44: }
45: });
46: }
47:
48: public void reset() {
49: window = null;
50: }
51: }
|