private Class<?>[] compile() throws IOException { Class<?>[] compiledSources = this.compiler.compile(this.sources); if (compiledSources.length == 0) { throw new RuntimeException( "No classes found in '" + Arrays.toString(this.sources) + "'"); } return compiledSources; }
@SuppressWarnings({ "rawtypes", "unchecked" }) private void processConversionOperations(LinkedList conversionOperations) { int index = getIndexOfASTTransformationVisitor(conversionOperations); conversionOperations.add(index, new CompilationUnit.SourceUnitOperation() { @Override public void call(SourceUnit source) throws CompilationFailedException { ASTNode[] nodes = new ASTNode[] { source.getAST() }; for (ASTTransformation transformation : GroovyCompiler.this.transformations) { transformation.visit(nodes, source); } } }); }
private List<URL> getClassPathUrls(GroovyCompiler compiler) { return new ArrayList<>(Arrays.asList(compiler.getLoader().getURLs())); }
@SuppressWarnings("rawtypes") private void addAstTransformations(CompilationUnit compilationUnit) { LinkedList[] phaseOperations = getPhaseOperations(compilationUnit); processConversionOperations(phaseOperations[Phases.CONVERSION]); }
@Override protected ExitStatus run(OptionSet options) throws Exception { SourceOptions sourceOptions = new SourceOptions(options); List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory .createDefaultRepositoryConfiguration(); GroovyCompilerConfiguration configuration = new OptionSetGroovyCompilerConfiguration( options, this, repositoryConfiguration); if (System.getProperty("grape.root") == null) { System.setProperty("grape.root", "."); } GroovyCompiler groovyCompiler = new GroovyCompiler(configuration); groovyCompiler.compile(sourceOptions.getSourcesArray()); return ExitStatus.OK; }
private GroovyCompiler createCompiler(OptionSet options) { List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory .createDefaultRepositoryConfiguration(); GroovyCompilerConfiguration configuration = new OptionSetGroovyCompilerConfiguration( options, this, repositoryConfiguration); GroovyCompiler groovyCompiler = new GroovyCompiler(configuration); groovyCompiler.getAstTransformations().add(0, new GrabAnnotationTransform()); return groovyCompiler; }
/** * Create a new {@link SpringApplicationRunner} instance. * @param configuration the configuration * @param sources the files to compile/watch * @param args input arguments */ SpringApplicationRunner(SpringApplicationRunnerConfiguration configuration, String[] sources, String... args) { this.configuration = configuration; this.sources = sources.clone(); this.args = args.clone(); this.compiler = new GroovyCompiler(configuration); int level = configuration.getLogLevel().intValue(); if (level <= Level.FINER.intValue()) { System.setProperty( "org.springframework.boot.cli.compiler.grape.ProgressReporter", "detail"); System.setProperty("trace", "true"); } else if (level <= Level.FINE.intValue()) { System.setProperty("debug", "true"); } else if (level == Level.OFF.intValue()) { System.setProperty("spring.main.banner-mode", "OFF"); System.setProperty("logging.level.ROOT", "OFF"); System.setProperty( "org.springframework.boot.cli.compiler.grape.ProgressReporter", "none"); } }
private ExtendedGroovyClassLoader createLoader( GroovyCompilerConfiguration configuration) { ExtendedGroovyClassLoader loader = new ExtendedGroovyClassLoader( configuration.getScope()); for (URL url : getExistingUrls()) { loader.addURL(url); } for (String classpath : configuration.getClasspath()) { loader.addClasspath(classpath); } return loader; }
addAstTransformations(compilationUnit);
this.loader = createLoader(configuration);
@Override public List<File> resolve(List<String> artifactIdentifiers) throws CompilationFailedException, IOException { GroovyCompiler groovyCompiler = new GroovyCompiler(this.configuration); List<File> artifactFiles = new ArrayList<>(); if (!artifactIdentifiers.isEmpty()) { List<URL> initialUrls = getClassPathUrls(groovyCompiler); groovyCompiler.compile(createSources(artifactIdentifiers)); List<URL> artifactUrls = getClassPathUrls(groovyCompiler); artifactUrls.removeAll(initialUrls); for (URL artifactUrl : artifactUrls) { artifactFiles.add(toFile(artifactUrl)); } } return artifactFiles; }
private List<URL> getClassPathUrls(GroovyCompiler compiler) { return new ArrayList<>(Arrays.asList(compiler.getLoader().getURLs())); }
@Override protected ExitStatus run(OptionSet options) throws Exception { List<?> nonOptionArguments = new ArrayList<Object>( options.nonOptionArguments()); Assert.isTrue(nonOptionArguments.size() >= 2, () -> "The name of the " + "resulting " + this.type + " and at least one source file must be " + "specified"); File output = new File((String) nonOptionArguments.remove(0)); Assert.isTrue( output.getName().toLowerCase(Locale.ENGLISH) .endsWith("." + this.type), () -> "The output '" + output + "' is not a " + this.type.toUpperCase(Locale.ENGLISH) + " file."); deleteIfExists(output); GroovyCompiler compiler = createCompiler(options); List<URL> classpath = getClassPathUrls(compiler); List<MatchedResource> classpathEntries = findMatchingClasspathEntries( classpath, options); String[] sources = new SourceOptions(nonOptionArguments).getSourcesArray(); Class<?>[] compiledClasses = compiler.compile(sources); List<URL> dependencies = getClassPathUrls(compiler); dependencies.removeAll(classpath); writeJar(output, compiledClasses, classpathEntries, dependencies); return ExitStatus.OK; }
private List<File> getSourceFiles() { List<File> sources = new ArrayList<>(); for (String source : SpringApplicationRunner.this.sources) { List<String> paths = ResourceUtils.getUrls(source, SpringApplicationRunner.this.compiler.getLoader()); for (String path : paths) { try { URL url = new URL(path); if ("file".equals(url.getProtocol())) { sources.add(new File(url.getFile())); } } catch (MalformedURLException ex) { // Ignore } } } return sources; }