/*
 * Copyright 2010 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.api.tasks.compile;

import org.gradle.api.Incubating;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.provider.AbstractMinimalProvider;
import org.gradle.api.model.ReplacedBy;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.SourceTask;

import java.io.File;

/**
 * The base class for all JVM-based language compilation tasks.
 */
public abstract class AbstractCompile extends SourceTask {
    private final DirectoryProperty destinationDirectory;
    private FileCollection classpath;
    private String sourceCompatibility;
    private String targetCompatibility;

    public AbstractCompile() {
        this.destinationDirectory = getProject().getObjects().directoryProperty();
        this.destinationDirectory.convention(new BackwardCompatibilityOutputDirectoryConvention());
    }

    /**
     * Returns the classpath to use to compile the source files.
     *
     * @return The classpath.
     */
    @Classpath
    public FileCollection getClasspath() {
        return classpath;
    }

    /**
     * Sets the classpath to use to compile the source files.
     *
     * @param configuration The classpath. Must not be null, but may be empty.
     */
    public void setClasspath(FileCollection configuration) {
        this.classpath = configuration;
    }

    /**
     * Returns the directory property that represents the directory to generate the {@code .class} files into.
     *
     * @return The destination directory property.
     * @since 6.1
     */
    @Incubating
    @OutputDirectory
    public DirectoryProperty getDestinationDirectory() {
        return destinationDirectory;
    }

    /**
     * Returns the directory to generate the {@code .class} files into.
     *
     * @return The destination directory.
     */
    @ReplacedBy("destinationDirectory")
    public File getDestinationDir() {
        return destinationDirectory.getAsFile().getOrNull();
    }

    /**
     * Sets the directory to generate the {@code .class} files into.
     *
     * @param destinationDir The destination directory. Must not be null.
     */
    public void setDestinationDir(File destinationDir) {
        this.destinationDirectory.set(destinationDir);
    }

    /**
     * Sets the directory to generate the {@code .class} files into.
     *
     * @param destinationDir The destination directory. Must not be null.
     * @since 4.0
     */
    public void setDestinationDir(Provider<File> destinationDir) {
        this.destinationDirectory.set(getProject().getLayout().dir(destinationDir));
    }

    /**
     * Returns the Java language level to use to compile the source files.
     *
     * @return The source language level.
     */
    @Input
    public String getSourceCompatibility() {
        return sourceCompatibility;
    }

    /**
     * Sets the Java language level to use to compile the source files.
     *
     * @param sourceCompatibility The source language level. Must not be null.
     */
    public void setSourceCompatibility(String sourceCompatibility) {
        this.sourceCompatibility = sourceCompatibility;
    }

    /**
     * Returns the target JVM to generate the {@code .class} files for.
     *
     * @return The target JVM.
     */
    @Input
    public String getTargetCompatibility() {
        return targetCompatibility;
    }

    /**
     * Sets the target JVM to generate the {@code .class} files for.
     *
     * @param targetCompatibility The target JVM. Must not be null.
     */
    public void setTargetCompatibility(String targetCompatibility) {
        this.targetCompatibility = targetCompatibility;
    }

    /**
     * Convention to fall back to the 'destinationDir' output for backwards compatibility with plugins that extend AbstractCompile and override the deprecated methods.
     */
    private class BackwardCompatibilityOutputDirectoryConvention extends AbstractMinimalProvider<Directory> {
        private boolean recursiveCall;

        @Override
        public Class<Directory> getType() {
            return Directory.class;
        }

        @Override
        protected Value<Directory> calculateOwnValue() {
            if (recursiveCall) {
                // Already quering AbstractCompile.getDestinationDirectory() and not by a subclass implementation of that method.
                // In that case, this convention should not be used.
                return Value.missing();
            }
            recursiveCall = true;
            File legacyValue;
            try {
                // If we are not in an error case, this will most likely call a subclass implementation of getDestinationDir().
                // In the Kotlin plugin, the subclass manages it's own field which will be used here.
                legacyValue = getDestinationDir();
            } finally {
                recursiveCall = false;
            }
            if (legacyValue == null) {
                return Value.missing();
            } else {
                return Value.of(getProject().getLayout().getProjectDirectory().dir(legacyValue.getAbsolutePath()));
            }
        }
    }
}

@@ -813,73 +813,8 @@
 on;%0A
-import org.gradle.api.internal.provider.AbstractMinimalProvider;%0A
 impo
@@ -1070,16 +1070,54 @@
 io.File;
+%0Aimport java.util.concurrent.Callable;
 %0A%0A/**%0A *
@@ -1577,16 +1577,53 @@
 vention(
+getProject().getProviders().provider(
 new Back
@@ -1667,16 +1667,17 @@
 ntion())
+)
 ;%0A    %7D%0A
@@ -4725,39 +4725,27 @@
 ion 
-extends AbstractMinimalProvider
+implements Callable
 %3CDir
@@ -4831,14 +4831,8 @@
 lic 
-Class%3C
 Dire
@@ -4840,141 +4840,32 @@
 tory
-%3E getType() %7B%0A            return Directory.class;%0A        %7D%0A%0A        @Override%0A        protected Value%3CDirectory%3E calculateOwnValue()
+ call() throws Exception
  %7B%0A 
@@ -5114,39 +5114,28 @@
      return 
-Value.missing()
+null
 ;%0A          
@@ -5644,23 +5644,12 @@
 urn 
-Value.missing()
+null
 ;%0A  
@@ -5694,17 +5694,8 @@
 urn 
-Value.of(
 getP
@@ -5771,17 +5771,16 @@
 ePath())
-)
 ;%0A      

Replace a usage of an internal API with a public one in the `AbstractCompile` task.