/*
 * Copyright 2018 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.internal.tasks.compile;

import org.gradle.internal.concurrent.CompositeStoppable;

import javax.annotation.processing.Processor;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import java.util.Locale;

public class ResourceCleaningCompilationTask implements JavaCompiler.CompilationTask {
    private final JavaCompiler.CompilationTask delegate;
    private final StandardJavaFileManager fileManager;

    public ResourceCleaningCompilationTask(JavaCompiler.CompilationTask delegate, StandardJavaFileManager fileManager) {
        this.delegate = delegate;
        this.fileManager = fileManager;
    }

    @Override
    public void setProcessors(Iterable<? extends Processor> processors) {
        delegate.setProcessors(processors);
    }

    @Override
    public void setLocale(Locale locale) {
        delegate.setLocale(locale);
    }

    @Override
    public Boolean call() {
        try {
            return delegate.call();
        } finally {
            CompositeStoppable.stoppable(fileManager).stop();
        }
    }
}

@@ -763,16 +763,55 @@
 cessor;%0A
+import javax.tools.DiagnosticListener;%0A
 import j
@@ -895,29 +895,138 @@
 ava.
-util.Locale;%0A%0Apublic 
+nio.charset.Charset;%0Aimport java.util.Locale;%0A%0A/**%0A * Cleans up resources (e.g. file handles) after compilation has finished.%0A */%0A
 clas
@@ -1218,23 +1218,16 @@
 r;%0A%0A    
-public 
 Resource
@@ -1818,24 +1818,913 @@
 er).stop();%0A
+            cleanupZipCache();%0A        %7D%0A    %7D%0A%0A    /**%0A     * The javac file manager uses a shared ZIP cache which keeps file handles open%0A     * after compilation. It's supposed to be tunable with the -XDuseOptimizedZip parameter,%0A     * but the %7B@link JavaCompiler#getStandardFileManager(DiagnosticListener, Locale, Charset)%7D%0A     * method does not take arguments, so the cache can't be turned off.%0A     * So instead we clean it ourselves using reflection.%0A     */%0A    private void cleanupZipCache() %7B%0A        try %7B%0A            Class%3C?%3E zipFileIndexCache = Class.forName(%22com.sun.tools.javac.file.ZipFileIndexCache%22);%0A            Object instance = zipFileIndexCache.getMethod(%22getSharedInstance%22).invoke(null);%0A            zipFileIndexCache.getMethod(%22clearCache%22).invoke(instance);%0A        %7D catch (Throwable e) %7B%0A            // Not an OpenJDK-compatible compiler or signature changed%0A
         %7D%0A  

Clean the javac zip file cache after compilation