Factor out common code for setJavaScriptHost/clearJavaScriptHost, fix typo in error message.

Patch by: jat (+scottb)
Review by: scottb


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@957 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/shell/ModuleSpace.java b/dev/core/src/com/google/gwt/dev/shell/ModuleSpace.java
index dcb5fde..bd991a1 100644
--- a/dev/core/src/com/google/gwt/dev/shell/ModuleSpace.java
+++ b/dev/core/src/com/google/gwt/dev/shell/ModuleSpace.java
@@ -83,14 +83,49 @@
     return (TreeLogger) threadLocalLogger.get();
   }
 
-  private final ModuleSpaceHost host;
+  /**
+   * Tricky one, this. Reaches over into this modules's JavaScriptHost class and
+   * sets its static 'host' field to be the specified ModuleSpace instance
+   * (which will either be this ModuleSpace or null).
+   * 
+   * @param moduleSpace the ModuleSpace instance to store using
+   *          JavaScriptHost.setHost().
+   * @see JavaScriptHost
+   */
+  private static void setJavaScriptHost(ModuleSpace moduleSpace, ClassLoader cl) {
+    // Find the application's JavaScriptHost interface.
+    //
+    Throwable caught;
+    try {
+      final String jsHostClassName = JavaScriptHost.class.getName();
+      Class jsHostClass = Class.forName(jsHostClassName, true, cl);
+      final Class[] paramTypes = new Class[] {ShellJavaScriptHost.class};
+      Method setHostMethod = jsHostClass.getMethod("setHost", paramTypes);
+      setHostMethod.invoke(jsHostClass, new Object[] {moduleSpace});
+      return;
+    } catch (ClassNotFoundException e) {
+      caught = e;
+    } catch (SecurityException e) {
+      caught = e;
+    } catch (NoSuchMethodException e) {
+      caught = e;
+    } catch (IllegalArgumentException e) {
+      caught = e;
+    } catch (IllegalAccessException e) {
+      caught = e;
+    } catch (InvocationTargetException e) {
+      caught = e.getTargetException();
+    }
+    throw new RuntimeException("Error initializing JavaScriptHost", caught);
+  }
 
-  private final String moduleName;
+  private final ModuleSpaceHost host;
 
   private final Object key;
 
-  protected ModuleSpace(ModuleSpaceHost host, String moduleName,
-      Object key) {
+  private final String moduleName;
+
+  protected ModuleSpace(ModuleSpaceHost host, String moduleName, Object key) {
     this.host = host;
     this.moduleName = moduleName;
     this.key = key;
@@ -435,37 +470,10 @@
   }
 
   /**
-   * Tricky one, this. Reaches over into this modules's JavaScriptHost class and
-   * sets its static 'host' field to be null.
-   * 
-   * @see JavaScriptHost
+   * Clear the module's JavaScriptHost 'host' field.
    */
   private void clearJavaScriptHost() {
-    // Find the application's JavaScriptHost interface.
-    //
-    Throwable caught;
-    try {
-      final String jsHostClassName = JavaScriptHost.class.getName();
-      Class jsHostClass = Class.forName(jsHostClassName, true,
-          getIsolatedClassLoader());
-      final Class[] paramTypes = new Class[] {ShellJavaScriptHost.class};
-      Method setHostMethod = jsHostClass.getMethod("setHost", paramTypes);
-      setHostMethod.invoke(jsHostClass, new Object[] {null});
-      return;
-    } catch (ClassNotFoundException e) {
-      caught = e;
-    } catch (SecurityException e) {
-      caught = e;
-    } catch (NoSuchMethodException e) {
-      caught = e;
-    } catch (IllegalArgumentException e) {
-      caught = e;
-    } catch (IllegalAccessException e) {
-      caught = e;
-    } catch (InvocationTargetException e) {
-      caught = e.getTargetException();
-    }
-    throw new RuntimeException("Error unintializing JavaScriptHost", caught);
+    setJavaScriptHost(null, getIsolatedClassLoader());
   }
 
   /**
@@ -491,37 +499,10 @@
   }
 
   /**
-   * Tricky one, this. Reaches over into this modules's JavaScriptHost class and
-   * sets its static 'host' field to be this ModuleSpace instance.
-   * 
-   * @see JavaScriptHost
+   * Set the module's JavaScriptHost 'host' field to this ModuleSpace instance.
    */
   private void setJavaScriptHost() {
-    // Find the application's JavaScriptHost interface.
-    //
-    Throwable caught;
-    try {
-      final String jsHostClassName = JavaScriptHost.class.getName();
-      Class jsHostClass = Class.forName(jsHostClassName, true,
-          getIsolatedClassLoader());
-      final Class[] paramTypes = new Class[] {ShellJavaScriptHost.class};
-      Method setHostMethod = jsHostClass.getMethod("setHost", paramTypes);
-      setHostMethod.invoke(jsHostClass, new Object[] {this});
-      return;
-    } catch (ClassNotFoundException e) {
-      caught = e;
-    } catch (SecurityException e) {
-      caught = e;
-    } catch (NoSuchMethodException e) {
-      caught = e;
-    } catch (IllegalArgumentException e) {
-      caught = e;
-    } catch (IllegalAccessException e) {
-      caught = e;
-    } catch (InvocationTargetException e) {
-      caught = e.getTargetException();
-    }
-    throw new RuntimeException("Error intializing JavaScriptHost", caught);
+    setJavaScriptHost(this, getIsolatedClassLoader());
   }
 
 }