Example usage for com.google.gwt.eclipse.core.runtime GWTRuntime createClassLoader

List of usage examples for com.google.gwt.eclipse.core.runtime GWTRuntime createClassLoader

Introduction

In this page you can find the example usage for com.google.gwt.eclipse.core.runtime GWTRuntime createClassLoader.

Prototype

public abstract URLClassLoader createClassLoader() throws SdkException, MalformedURLException;

Source Link

Usage

From source file:com.google.gdt.eclipse.suite.wizards.WebAppProjectCreator.java

License:Open Source License

private void createGWTProject(IProgressMonitor monitor, String packageName, String outDirPath)
          throws MalformedURLException, SdkException, JavaModelException, ClassNotFoundException, CoreException {

      /*/*from   w ww  . j a v a2 s . c  o m*/
       * The project name will be used as the entry point name. When invoking
       * GWT's WebAppCreator, the entry point name will be passed in as the last
       * component in the module name. The WebAppCreator will use the last
       * component of the module name as the generated Eclipse project name, which
       * gives us the desired effect.
       *
       * FIXME: The project name may not be a valid entry point name. We need to
       * scan the project name token-by-token, and translate its tokens into a
       * valid Java identifier name. Some examples:
       *
       * If the first token in the project name is a lower-case letter, then the
       * translated character should be made upper case.
       *
       * If the first token in the project name is a non-alpha character it should
       * be deleted in the translation.
       */

      final String entryPoint = generateClassName(projectName);
      final String qualifiedModuleName = packageName + "." + entryPoint;

      IPath gwtContainerPath = findContainerPath(GWTRuntimeContainer.CONTAINER_ID);
      assert (gwtContainerPath != null);

      GWTRuntime runtime = GWTPreferences.getSdkManager().findSdkForPath(gwtContainerPath);
      assert (runtime != null);

      // Get a reference to the gwt-dev-<platform>.jar
      File gwtDevJar = runtime.getDevJar();

      // Need to set gwt.devjar property before calling projectCreator and
      // applicationCreator
      System.setProperty("gwt.devjar", gwtDevJar.toString());

      File outDir = new File(outDirPath, projectName);

      IPath startupUrl = new Path(entryPoint + ".html");
      try {
          if (!runtime.containsSCL()) {
              // TODO: Consider using a WebAppProjectCreatorRunner-style solution
              // here.
              URLClassLoader cl = runtime.createClassLoader();
              Class<?> projectCreatorClass = cl.loadClass("com.google.gwt.user.tools.ProjectCreator");
              Method m = projectCreatorClass.getDeclaredMethod("createProject", String.class, String.class,
                      File.class, boolean.class, boolean.class);
              m.setAccessible(true);
              m.invoke(null, projectName, null, outDir, false, false);
              monitor.worked(1);

              Class<?> applicationCreatorClass = cl.loadClass("com.google.gwt.user.tools.ApplicationCreator");
              m = applicationCreatorClass.getDeclaredMethod("createApplication", String.class, File.class,
                      String.class, boolean.class, boolean.class);
              m.setAccessible(true);

              if (!packageName.endsWith("client")) {
                  packageName = packageName + ".client";
              }
              m.invoke(null, packageName + "." + entryPoint, outDir, projectName, false, false);

              startupUrl = new Path(qualifiedModuleName).append(startupUrl);
          } else {
              WebAppProjectCreatorRunner.createProject(qualifiedModuleName, outDir.getAbsolutePath(), runtime,
                      monitor, templateSources, templates);
          }

          reformatJavaFiles(outDir);
          String version = runtime.getVersion();
          if (!SdkUtils.isInternal(version) && SdkUtils.compareVersionStrings(version,
                  WebAppProjectCreatorRunner.GWT_VERSION_WITH_TEMPLATES) <= -1) {
              deleteAllLaunchConfigurations(outDir);
              deleteAllShellScripts(projectName, outDir);
              deleteBuildScript(outDir);
          }
      } catch (NoSuchMethodException e) {
          GdtPlugin.getLogger().logError(e);
          throw new SdkException(
                  "Unable to invoke methods for project creation or application creation. using runtime "
                          + runtime.getName() + " " + e);
      } catch (IllegalAccessException e) {
          throw new SdkException(
                  "Unable to access methods for project creation or application creation. using runtime "
                          + runtime.getName() + " " + e);
      } catch (InvocationTargetException e) {
          // Rethrow exception thrown by creator class
          Throwable cause = e.getCause();
          if (cause != null && cause instanceof Exception) {
              GdtPlugin.getLogger().logError(cause);
          } else {
              GdtPlugin.getLogger().logError(e);
          }

          throw new SdkException(
                  "Exception occured while attempting to create project and application, using runtime  "
                          + runtime.getName() + " " + e);
      }
  }