<commit_before>/*
 * Copyright 2020-2021 DiffPlug
 *
 * 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
 *
 *     http://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 com.diffplug.gradle.spotless;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import javax.annotation.Nullable;

import org.eclipse.jgit.lib.ObjectId;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.work.Incremental;

import com.diffplug.spotless.FormatExceptionPolicy;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;

public abstract class SpotlessTask extends DefaultTask {
	@Internal
	abstract Property<SpotlessTaskService> getTaskService();

	// set by SpotlessExtension, but possibly overridden by FormatExtension
	protected String encoding = "UTF-8";

	@Input
	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = Objects.requireNonNull(encoding);
	}

	protected transient LineEnding.Policy lineEndingsPolicy;

	@Input
	public LineEnding.Policy getLineEndingsPolicy() {
		return lineEndingsPolicy;
	}

	public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
		this.lineEndingsPolicy = Objects.requireNonNull(lineEndingsPolicy);
	}

	/*** API which performs git up-to-date tasks. */
	@Nullable
	private transient GitRatchetGradle ratchet;
	/** The sha of the tree at repository root, used for determining if an individual *file* is clean according to git. */
	private transient ObjectId rootTreeSha;
	/**
	 * The sha of the tree at the root of *this project*, used to determine if the git baseline has changed within this folder.
	 * Using a more fine-grained tree (rather than the project root) allows Gradle to mark more subprojects as up-to-date
	 * compared to using the project root.
	 */
	private transient ObjectId subtreeSha = ObjectId.zeroId();

	public void setupRatchet(String ratchetFrom) {
		ratchet = getTaskService().get().getRatchet();
		File projectDir = getProjectDir().get().getAsFile();
		rootTreeSha = ratchet.rootTreeShaOf(projectDir, ratchetFrom);
		subtreeSha = ratchet.subtreeShaOf(projectDir, rootTreeSha);
	}

	@Internal
	abstract DirectoryProperty getProjectDir();

	@Internal
	GitRatchetGradle getRatchet() {
		return ratchet;
	}

	@Internal
	ObjectId getRootTreeSha() {
		return rootTreeSha;
	}

	@Input
	public ObjectId getRatchetSha() {
		return subtreeSha;
	}

	protected FormatExceptionPolicy exceptionPolicy = new FormatExceptionPolicyStrict();

	public void setExceptionPolicy(FormatExceptionPolicy exceptionPolicy) {
		this.exceptionPolicy = Objects.requireNonNull(exceptionPolicy);
	}

	@Input
	public FormatExceptionPolicy getExceptionPolicy() {
		return exceptionPolicy;
	}

	protected FileCollection target;

	@PathSensitive(PathSensitivity.RELATIVE)
	@Incremental
	@InputFiles
	public FileCollection getTarget() {
		return target;
	}

	public void setTarget(Iterable<File> target) {
		if (target instanceof FileCollection) {
			this.target = (FileCollection) target;
		} else {
			this.target = getProject().files(target);
		}
	}

	protected File outputDirectory = new File(getProject().getBuildDir(), "spotless/" + getName());

	@OutputDirectory
	public File getOutputDirectory() {
		return outputDirectory;
	}

	protected transient List<FormatterStep> steps = new ArrayList<>();

	@Input
	public List<FormatterStep> getSteps() {
		return Collections.unmodifiableList(steps);
	}

	public void setSteps(List<FormatterStep> steps) {
		this.steps = PluginGradlePreconditions.requireElementsNonNull(steps);
	}

	public boolean addStep(FormatterStep step) {
		return this.steps.add(Objects.requireNonNull(step));
	}

	/** Returns the name of this format. */
	String formatName() {
		String name = getName();
		if (name.startsWith(SpotlessExtension.EXTENSION)) {
			return name.substring(SpotlessExtension.EXTENSION.length()).toLowerCase(Locale.ROOT);
		} else {
			return name;
		}
	}

	Formatter buildFormatter() {
		return Formatter.builder()
				.lineEndingsPolicy(lineEndingsPolicy)
				.encoding(Charset.forName(encoding))
				.rootDir(getProjectDir().get().getAsFile().toPath())
				.steps(steps)
				.exceptionPolicy(exceptionPolicy)
				.build();
	}
}
<commit_msg>And we guard every transient getter of SpotlessTask with `hydrateIfNull`.<commit_after>/*
 * Copyright 2020-2021 DiffPlug
 *
 * 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
 *
 *     http://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 com.diffplug.gradle.spotless;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import javax.annotation.Nullable;

import org.eclipse.jgit.lib.ObjectId;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.work.Incremental;

import com.diffplug.spotless.FormatExceptionPolicy;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;

public abstract class SpotlessTask extends DefaultTask {
	@Internal
	abstract Property<SpotlessTaskService> getTaskService();

	private void hydrateIfNull(Object o) {
		if (o == null) {
			getTaskService().get().hydrate(this);
		}
	}

	// set by SpotlessExtension, but possibly overridden by FormatExtension
	protected String encoding = "UTF-8";

	@Input
	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = Objects.requireNonNull(encoding);
	}

	protected transient LineEnding.Policy lineEndingsPolicy;

	@Input
	public LineEnding.Policy getLineEndingsPolicy() {
		hydrateIfNull(lineEndingsPolicy);
		return lineEndingsPolicy;
	}

	public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
		this.lineEndingsPolicy = Objects.requireNonNull(lineEndingsPolicy);
	}

	/*** API which performs git up-to-date tasks. */
	@Nullable
	private transient GitRatchetGradle ratchet;
	/** The sha of the tree at repository root, used for determining if an individual *file* is clean according to git. */
	private transient ObjectId rootTreeSha;
	/**
	 * The sha of the tree at the root of *this project*, used to determine if the git baseline has changed within this folder.
	 * Using a more fine-grained tree (rather than the project root) allows Gradle to mark more subprojects as up-to-date
	 * compared to using the project root.
	 */
	private transient ObjectId subtreeSha = ObjectId.zeroId();
	/** Stored so that the configuration cache can recreate the GitRatchetGradle state. */
	protected transient String ratchetFrom;

	public void setupRatchet(String ratchetFrom) {
		this.ratchetFrom = ratchetFrom;
		if (!ratchetFrom.isEmpty()) {
			ratchet = getTaskService().get().getRatchet();
			File projectDir = getProjectDir().get().getAsFile();
			rootTreeSha = ratchet.rootTreeShaOf(projectDir, ratchetFrom);
			subtreeSha = ratchet.subtreeShaOf(projectDir, rootTreeSha);
		} else {
			subtreeSha = ObjectId.zeroId();
		}
	}

	@Internal
	abstract DirectoryProperty getProjectDir();

	@Internal
	GitRatchetGradle getRatchet() {
		return ratchet;
	}

	@Internal
	ObjectId getRootTreeSha() {
		return rootTreeSha;
	}

	@Input
	public ObjectId getRatchetSha() {
		hydrateIfNull(subtreeSha);
		return subtreeSha;
	}

	protected FormatExceptionPolicy exceptionPolicy = new FormatExceptionPolicyStrict();

	public void setExceptionPolicy(FormatExceptionPolicy exceptionPolicy) {
		this.exceptionPolicy = Objects.requireNonNull(exceptionPolicy);
	}

	@Input
	public FormatExceptionPolicy getExceptionPolicy() {
		return exceptionPolicy;
	}

	protected FileCollection target;

	@PathSensitive(PathSensitivity.RELATIVE)
	@Incremental
	@InputFiles
	public FileCollection getTarget() {
		return target;
	}

	public void setTarget(Iterable<File> target) {
		if (target instanceof FileCollection) {
			this.target = (FileCollection) target;
		} else {
			this.target = getProject().files(target);
		}
	}

	protected File outputDirectory = new File(getProject().getBuildDir(), "spotless/" + getName());

	@OutputDirectory
	public File getOutputDirectory() {
		return outputDirectory;
	}

	protected transient List<FormatterStep> steps = new ArrayList<>();

	@Input
	public List<FormatterStep> getSteps() {
		hydrateIfNull(steps);
		return Collections.unmodifiableList(steps);
	}

	public void setSteps(List<FormatterStep> steps) {
		this.steps = PluginGradlePreconditions.requireElementsNonNull(steps);
	}

	public boolean addStep(FormatterStep step) {
		return this.steps.add(Objects.requireNonNull(step));
	}

	/** Returns the name of this format. */
	String formatName() {
		String name = getName();
		if (name.startsWith(SpotlessExtension.EXTENSION)) {
			return name.substring(SpotlessExtension.EXTENSION.length()).toLowerCase(Locale.ROOT);
		} else {
			return name;
		}
	}

	Formatter buildFormatter() {
		return Formatter.builder()
				.lineEndingsPolicy(lineEndingsPolicy)
				.encoding(Charset.forName(encoding))
				.rootDir(getProjectDir().get().getAsFile().toPath())
				.steps(steps)
				.exceptionPolicy(exceptionPolicy)
				.build();
	}
}

{'fraction_non_alphanumeric': 0.09650552236103567, 'fraction_numerical': 0.002353793228317943, 'mean_word_length': 4.971351351351351, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '4266393', 'n_tokens_mistral': 3468, 'n_tokens_neox': 3197, 'n_words': 868}