<commit_before>/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aot.test.generator.compile;

import java.io.PrintStream;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import org.springframework.aot.test.generator.file.ResourceFile;
import org.springframework.aot.test.generator.file.ResourceFiles;
import org.springframework.aot.test.generator.file.SourceFile;
import org.springframework.aot.test.generator.file.SourceFiles;
import org.springframework.aot.test.generator.file.WritableContent;
import org.springframework.lang.Nullable;

/**
 * Utility that can be used to dynamically compile and test Java source code.
 *
 * @author <NAME>
 * @since 6.0
 * @see #forSystem()
 */
public final class TestCompiler {

	@Nullable
	private final ClassLoader classLoader;

	private final JavaCompiler compiler;

	private final SourceFiles sourceFiles;

	private final ResourceFiles resourceFiles;


	private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler,
			SourceFiles sourceFiles, ResourceFiles resourceFiles) {
		this.classLoader = classLoader;
		this.compiler = compiler;
		this.sourceFiles = sourceFiles;
		this.resourceFiles = resourceFiles;
	}


	/**
	 * Return a new {@link TestCompiler} backed by the system java compiler.
	 * @return a new {@link TestCompiler} instance
	 */
	public static TestCompiler forSystem() {
		return forCompiler(ToolProvider.getSystemJavaCompiler());
	}

	/**
	 * Return a new {@link TestCompiler} backed by the given
	 * {@link JavaCompiler}.
	 * @param javaCompiler the java compiler to use
	 * @return a new {@link TestCompiler} instance
	 */
	public static TestCompiler forCompiler(JavaCompiler javaCompiler) {
		return new TestCompiler(null, javaCompiler, SourceFiles.none(),
				ResourceFiles.none());
	}

	/**
	 * Return a new {@link TestCompiler} instance with addition source files.
	 * @param sourceFiles the additional source files
	 * @return a new {@link TestCompiler} instance
	 */
	public TestCompiler withSources(SourceFile... sourceFiles) {
		return new TestCompiler(this.classLoader, this.compiler,
				this.sourceFiles.and(sourceFiles), this.resourceFiles);
	}

	/**
	 * Return a new {@link TestCompiler} instance with addition source files.
	 * @param sourceFiles the additional source files
	 * @return a new {@link TestCompiler} instance
	 */
	public TestCompiler withSources(Iterable<SourceFile> sourceFiles) {
		return new TestCompiler(this.classLoader, this.compiler,
				this.sourceFiles.and(sourceFiles), this.resourceFiles);
	}

	/**
	 * Return a new {@link TestCompiler} instance with addition source files.
	 * @param sourceFiles the additional source files
	 * @return a new {@link TestCompiler} instance
	 */<commit_msg>Add withFiles support to TestCompiler<commit_after>/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aot.test.generator.compile;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import org.springframework.aot.generate.GeneratedFiles.Kind;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.test.generator.file.ResourceFile;
import org.springframework.aot.test.generator.file.ResourceFiles;
import org.springframework.aot.test.generator.file.SourceFile;
import org.springframework.aot.test.generator.file.SourceFiles;
import org.springframework.aot.test.generator.file.WritableContent;
import org.springframework.lang.Nullable;

/**
 * Utility that can be used to dynamically compile and test Java source code.
 *
 * @author <NAME>
 * @since 6.0
 * @see #forSystem()
 */
public final class TestCompiler {

	@Nullable
	private final ClassLoader classLoader;

	private final JavaCompiler compiler;

	private final SourceFiles sourceFiles;

	private final ResourceFiles resourceFiles;


	private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler,
			SourceFiles sourceFiles, ResourceFiles resourceFiles) {
		this.classLoader = classLoader;
		this.compiler = compiler;
		this.sourceFiles = sourceFiles;
		this.resourceFiles = resourceFiles;
	}


	/**
	 * Return a new {@link TestCompiler} backed by the system java compiler.
	 * @return a new {@link TestCompiler} instance
	 */
	public static TestCompiler forSystem() {
		return forCompiler(ToolProvider.getSystemJavaCompiler());
	}

	/**
	 * Return a new {@link TestCompiler} backed by the given
	 * {@link JavaCompiler}.
	 * @param javaCompiler the java compiler to use
	 * @return a new {@link TestCompiler} instance
	 */
	public static TestCompiler forCompiler(JavaCompiler javaCompiler) {
		return new TestCompiler(null, javaCompiler, SourceFiles.none(),
				ResourceFiles.none());
	}

	/**
	 * Return a new {@link TestCompiler} instance with additional generated
	 * source and resource files.
	 * @param generatedFiles the generated files to add
	 * @return a new {@link TestCompiler} instance
	 */
	public TestCompiler withFiles(InMemoryGeneratedFiles generatedFiles) {
		List<SourceFile> sourceFiles = new ArrayList<>();
		generatedFiles.getGeneratedFiles(Kind.SOURCE).forEach((path,
				inputStreamSource) -> sourceFiles.add(SourceFile.of(inputStreamSource)));
		List<ResourceFile> resourceFiles = new ArrayList<>();
		generatedFiles.getGeneratedFiles(Kind.RESOURCE)
				.forEach((path, inputStreamSource) -> resourceFiles
						.add(ResourceFile.of(path, inputStreamSource)));
		return withSources(sourceFiles).withResources(resourceFiles);
	}

	/**
	 * Return a new {@link TestCompiler} instance with addition source files.
	 * @param sourceFiles the additional source files
	 * @return a new {@link TestCompiler} instance
	 */
	public TestCompiler withSources(SourceFile... sourceFiles) {
		return new TestCompiler(this.classLoader, this.compiler,
				this.sourceFiles.and(sourceFiles), this.resourceFiles);
	}

	/**
	 * Return a new {@link TestCompiler} instance with addition source files.
	 * @param sourceFiles the additional source files
	 * @return a new {@link TestCompiler} instance
	 */
	public TestCompiler withSources(Iterable<SourceFile> sourceFiles) {
		return new TestCompiler(this.classLoader, this.compiler,
				this.sourceFiles.and(sourceFiles), this.resourceFiles);
	}

	/**
	 * Return a new {@link TestCompiler} instance with addition source files.
	 * @param sourceFiles the additional source files
	 * @return a new {@link TestCompiler} instance
	 */
{'fraction_non_alphanumeric': 0.09127664788042146, 'fraction_numerical': 0.003430531732418525, 'mean_word_length': 4.809964412811388, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4049844', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2093, 'n_words': 707}