<commit_before>/*
 * Copyright 2020 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
 *
 *      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 org.gradle.jvm.toolchain.internal;

import org.gradle.api.Describable;
import org.gradle.api.JavaVersion;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.internal.os.OperatingSystem;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavadocTool;
import org.gradle.util.VersionNumber;

import javax.inject.Inject;
import java.io.File;

public class JavaToolchain implements Describable {

    private final JavaInstallationProbe.ProbeResult probe;
    private final JavaCompilerFactory compilerFactory;
    private final ToolchainToolFactory toolFactory;
    private final File javaHome;

    @Inject
    public JavaToolchain(JavaInstallationProbe.ProbeResult probe, JavaCompilerFactory compilerFactory, ToolchainToolFactory toolFactory) {
        this.probe = probe;
        this.javaHome = computeJavaHome(probe);
        this.compilerFactory = compilerFactory;
        this.toolFactory = toolFactory;
    }

    @Internal
    public JavaCompiler getJavaCompiler() {
        return new DefaultToolchainJavaCompiler(this, compilerFactory);
    }

    @Internal
    public JavaLauncher getJavaLauncher() {
        return new DefaultToolchainJavaLauncher(findExecutable("java"));
    }

    @Internal
    public JavadocTool getJavadocTool() {
        return toolFactory.create(JavadocTool.class, this);
    }

    @Input
    public JavaVersion getJavaMajorVersion() {
        return probe.getJavaVersion();
    }

    @Internal
    public VersionNumber getToolVersion() {
        return VersionNumber.parse(probe.getImplementationJavaVersion());
    }

    @Internal
    public File getJavaHome() {
        return probe.getJavaHome();
    }

    @Internal
    public boolean isJdk() {
        return probe.getInstallType() == JavaInstallationProbe.InstallType.IS_JDK;
    }

    @Internal
    @Override
    public String getDisplayName() {
        return javaHome.getAbsolutePath();
    }

    public File findExecutable(String toolname) {
        return new File(getJavaHome(), getBinaryPath(toolname));
    }

    private File computeJavaHome(JavaInstallationProbe.ProbeResult probe) {
        final File home = probe.getJavaHome();
        final File parentFile = home.getParentFile();
        final boolean isEmbeddedJre = home.getName().equalsIgnoreCase("jre");
        if (isEmbeddedJre && new File(parentFile, getBinaryPath("java")).exists()) {
            return parentFile;
        }
        return home;
    }

    private String getBinaryPath(String java) {
        return "bin/" + OperatingSystem.current().getExecutableName(java);
    }
}
<commit_msg>Use correct javahome to lookup tools<commit_after>/*
 * Copyright 2020 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
 *
 *      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 org.gradle.jvm.toolchain.internal;

import org.gradle.api.Describable;
import org.gradle.api.JavaVersion;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.internal.os.OperatingSystem;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavadocTool;
import org.gradle.util.VersionNumber;

import javax.inject.Inject;
import java.io.File;

public class JavaToolchain implements Describable {

    private final boolean isJdk;
    private final JavaCompilerFactory compilerFactory;
    private final ToolchainToolFactory toolFactory;
    private final File javaHome;
    private final VersionNumber implementationVersion;
    private final JavaVersion javaVersion;

    @Inject
    public JavaToolchain(JavaInstallationProbe.ProbeResult probe, JavaCompilerFactory compilerFactory, ToolchainToolFactory toolFactory) {
        this(probe.getJavaHome(), probe.getJavaVersion(), probe.getImplementationJavaVersion(), probe.getInstallType() == JavaInstallationProbe.InstallType.IS_JDK, compilerFactory, toolFactory);
    }

    JavaToolchain(File javaHome, JavaVersion version, String implementationJavaVersion, boolean isJdk, JavaCompilerFactory compilerFactory, ToolchainToolFactory toolFactory) {
        this.javaHome = computeEnclosingJavaHome(javaHome);
        this.javaVersion = version;
        this.isJdk = isJdk;
        this.compilerFactory = compilerFactory;
        this.toolFactory = toolFactory;
        this.implementationVersion = VersionNumber.parse(implementationJavaVersion);
    }

    @Internal
    public JavaCompiler getJavaCompiler() {
        return new DefaultToolchainJavaCompiler(this, compilerFactory);
    }

    @Internal
    public JavaLauncher getJavaLauncher() {
        return new DefaultToolchainJavaLauncher(findExecutable("java"));
    }

    @Internal
    public JavadocTool getJavadocTool() {
        return toolFactory.create(JavadocTool.class, this);
    }

    @Input
    public JavaVersion getJavaMajorVersion() {
        return javaVersion;
    }

    @Internal
    public VersionNumber getToolVersion() {
        return implementationVersion;
    }

    @Internal
    public File getJavaHome() {
        return javaHome;
    }

    @Internal
    public boolean isJdk() {
        return isJdk;
    }

    @Internal
    @Override
    public String getDisplayName() {
        return javaHome.getAbsolutePath();
    }

    public File findExecutable(String toolname) {
        return new File(getJavaHome(), getBinaryPath(toolname));
    }

    private File computeEnclosingJavaHome(File home) {
        final File parentFile = home.getParentFile();
        final boolean isEmbeddedJre = home.getName().equalsIgnoreCase("jre");
        if (isEmbeddedJre && new File(parentFile, getBinaryPath("java")).exists()) {
            return parentFile;
        }
        return home;
    }

    private String getBinaryPath(String java) {
        return "bin/" + OperatingSystem.current().getExecutableName(java);
    }
}

{'fraction_non_alphanumeric': 0.0824713050871475, 'fraction_numerical': 0.0022672523735298286, 'mean_word_length': 3.5980456026058634, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5034920', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1858, 'n_words': 513}