@Override
 public Class<? extends T> loadClass() {
   if (scriptClass == null) {
     if (isEmpty && !hasMethods) {
       throw new UnsupportedOperationException("Cannot load script that does nothing.");
     }
     try {
       ClassLoader loader =
           classLoaderCache.get(
               classLoaderId, new DefaultClassPath(scriptCacheDir), classLoader, null);
       scriptClass = loader.loadClass(source.getClassName()).asSubclass(scriptBaseClass);
     } catch (Exception e) {
       File expectedClassFile = new File(scriptCacheDir, source.getClassName() + ".class");
       if (!expectedClassFile.exists()) {
         throw new GradleException(
             String.format(
                 "Could not load compiled classes for %s from cache. Expected class file %s does not exist.",
                 source.getDisplayName(), expectedClassFile.getAbsolutePath()),
             e);
       }
       throw new GradleException(
           String.format(
               "Could not load compiled classes for %s from cache.", source.getDisplayName()),
           e);
     }
   }
   return scriptClass;
 }
 private <M> void serializeMetadata(
     ScriptSource scriptSource,
     CompileOperation<M> extractingTransformer,
     File metadataDir,
     boolean emptyScript,
     boolean hasMethods) {
   File metadataFile = new File(metadataDir, METADATA_FILE_NAME);
   try {
     GFileUtils.mkdirs(metadataDir);
     KryoBackedEncoder encoder = new KryoBackedEncoder(new FileOutputStream(metadataFile));
     try {
       byte flags = (byte) ((emptyScript ? EMPTY_FLAG : 0) | (hasMethods ? HAS_METHODS_FLAG : 0));
       encoder.writeByte(flags);
       if (extractingTransformer != null && extractingTransformer.getDataSerializer() != null) {
         Serializer<M> serializer = extractingTransformer.getDataSerializer();
         serializer.write(encoder, extractingTransformer.getExtractedData());
       }
     } finally {
       encoder.close();
     }
   } catch (Exception e) {
     throw new GradleException(
         String.format(
             "Failed to serialize script metadata extracted for %s",
             scriptSource.getDisplayName()),
         e);
   }
 }
 /**
  * Returns a description of the location of where this exception occurred.
  *
  * @return The location description. May return null.
  */
 public String getLocation() {
   if (source == null) {
     return null;
   }
   String sourceMsg = StringUtils.capitalize(source.getDisplayName());
   if (lineNumber == null) {
     return sourceMsg;
   }
   return String.format("%s line: %d", sourceMsg, lineNumber);
 }
  private void wrapCompilationFailure(ScriptSource source, MultipleCompilationErrorsException e) {
    // Fix the source file name displayed in the error messages
    for (Object message : e.getErrorCollector().getErrors()) {
      if (message instanceof SyntaxErrorMessage) {
        try {
          SyntaxErrorMessage syntaxErrorMessage = (SyntaxErrorMessage) message;
          Field sourceField = SyntaxErrorMessage.class.getDeclaredField("source");
          sourceField.setAccessible(true);
          SourceUnit sourceUnit = (SourceUnit) sourceField.get(syntaxErrorMessage);
          Field nameField = SourceUnit.class.getDeclaredField("name");
          nameField.setAccessible(true);
          nameField.set(sourceUnit, source.getDisplayName());
        } catch (Exception failure) {
          throw UncheckedException.throwAsUncheckedException(failure);
        }
      }
    }

    SyntaxException syntaxError = e.getErrorCollector().getSyntaxError(0);
    Integer lineNumber = syntaxError == null ? null : syntaxError.getLine();
    throw new ScriptCompilationException(
        String.format("Could not compile %s.", source.getDisplayName()), e, source, lineNumber);
  }
 public <T extends Script, M> CompiledScript<T, M> loadFromDir(
     ScriptSource source,
     ClassLoader classLoader,
     File scriptCacheDir,
     File metadataCacheDir,
     CompileOperation<M> transformer,
     Class<T> scriptBaseClass,
     ClassLoaderId classLoaderId) {
   File metadataFile = new File(metadataCacheDir, METADATA_FILE_NAME);
   try {
     KryoBackedDecoder decoder = new KryoBackedDecoder(new FileInputStream(metadataFile));
     try {
       byte flags = decoder.readByte();
       boolean isEmpty = (flags & EMPTY_FLAG) != 0;
       boolean hasMethods = (flags & HAS_METHODS_FLAG) != 0;
       if (isEmpty) {
         classLoaderCache.remove(classLoaderId);
       }
       M data;
       if (transformer != null && transformer.getDataSerializer() != null) {
         data = transformer.getDataSerializer().read(decoder);
       } else {
         data = null;
       }
       return new ClassesDirCompiledScript<T, M>(
           isEmpty,
           hasMethods,
           classLoaderId,
           scriptBaseClass,
           scriptCacheDir,
           classLoader,
           source,
           data);
     } finally {
       decoder.close();
     }
   } catch (Exception e) {
     throw new IllegalStateException(
         String.format(
             "Failed to deserialize script metadata extracted for %s", source.getDisplayName()),
         e);
   }
 }
  private void compileScript(
      final ScriptSource source,
      ClassLoader classLoader,
      CompilerConfiguration configuration,
      File classesDir,
      File metadataDir,
      final CompileOperation<?> extractingTransformer,
      final Action<? super ClassNode> customVerifier) {
    final Transformer transformer =
        extractingTransformer != null ? extractingTransformer.getTransformer() : null;
    logger.info(
        "Compiling {} using {}.",
        source.getDisplayName(),
        transformer != null ? transformer.getClass().getSimpleName() : "no transformer");

    final EmptyScriptDetector emptyScriptDetector = new EmptyScriptDetector();
    final PackageStatementDetector packageDetector = new PackageStatementDetector();
    GroovyClassLoader groovyClassLoader =
        new GroovyClassLoader(classLoader, configuration, false) {
          @Override
          protected CompilationUnit createCompilationUnit(
              CompilerConfiguration compilerConfiguration, CodeSource codeSource) {
            ImportCustomizer customizer = new ImportCustomizer();
            customizer.addStarImports(defaultImportPackages);
            compilerConfiguration.addCompilationCustomizers(customizer);

            CompilationUnit compilationUnit =
                new CustomCompilationUnit(
                    compilerConfiguration, codeSource, customVerifier, source, this);

            if (transformer != null) {
              transformer.register(compilationUnit);
            }

            compilationUnit.addPhaseOperation(packageDetector, Phases.CANONICALIZATION);
            compilationUnit.addPhaseOperation(emptyScriptDetector, Phases.CANONICALIZATION);
            return compilationUnit;
          }
        };

    groovyClassLoader.setResourceLoader(NO_OP_GROOVY_RESOURCE_LOADER);
    String scriptText = source.getResource().getText();
    String scriptName = source.getClassName();
    GroovyCodeSource codeSource =
        new GroovyCodeSource(scriptText == null ? "" : scriptText, scriptName, "/groovy/script");
    try {
      groovyClassLoader.parseClass(codeSource, false);
    } catch (MultipleCompilationErrorsException e) {
      wrapCompilationFailure(source, e);
    } catch (CompilationFailedException e) {
      throw new GradleException(String.format("Could not compile %s.", source.getDisplayName()), e);
    }

    if (packageDetector.hasPackageStatement) {
      throw new UnsupportedOperationException(
          String.format(
              "%s should not contain a package statement.",
              StringUtils.capitalize(source.getDisplayName())));
    }
    serializeMetadata(
        source,
        extractingTransformer,
        metadataDir,
        emptyScriptDetector.isEmptyScript(),
        emptyScriptDetector.getHasMethods());
  }
Exemplo n.º 7
0
 public URI getSourceURI() {
   return scriptSource.getResource().getURI();
 }
Exemplo n.º 8
0
 public File getSourceFile() {
   return scriptSource.getResource().getFile();
 }
Exemplo n.º 9
0
 public void beforeScript(Script script) {
   ScriptSource source = script.getScriptSource();
   scripts.put(source.getFileName(), source);
 }