@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 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()); }