コード例 #1
0
  /**
   * Attempts to compile the given InputStream into a Groovy script using the given name
   *
   * @param in The InputStream to read the Groovy code from
   * @param name The name of the class to use
   * @param pageName The page name
   * @param metaInfo
   * @return The compiled java.lang.Class, which is an instance of groovy.lang.Script
   */
  private Class<?> compileGroovyPage(
      InputStream in, String name, String pageName, GroovyPageMetaInfo metaInfo) {
    GroovyClassLoader groovyClassLoader = findOrInitGroovyClassLoader();

    // Compile the script into an object
    Class<?> scriptClass;
    try {
      scriptClass = groovyClassLoader.parseClass(DefaultGroovyMethods.getText(in), name);
    } catch (CompilationFailedException e) {
      LOG.error("Compilation error compiling GSP [" + name + "]:" + e.getMessage(), e);

      int lineNumber = GrailsExceptionResolver.extractLineNumber(e);

      final int[] lineMappings = metaInfo.getLineNumbers();
      if (lineNumber > 0 && lineNumber < lineMappings.length) {
        lineNumber = lineMappings[lineNumber - 1];
      }
      throw new GroovyPagesException(
          "Could not parse script [" + name + "]: " + e.getMessage(), e, lineNumber, pageName);
    } catch (IOException e) {
      throw new GroovyPagesException(
          "IO exception parsing script [" + name + "]: " + e.getMessage(), e);
    }
    return scriptClass;
  }