/*
 * Copyright 2015 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.classloading;

import org.gradle.api.GradleException;
import org.gradle.util.VersionNumber;

import java.lang.reflect.Method;

public class GroovySystemLoaderFactory {
    private final static boolean HAS_CLASS_VALUE;
    public static final NoOpGroovySystemLoader NOT_BROKEN = new NoOpGroovySystemLoader();

    static {
        boolean cv = true;
        try {
            Class.forName("java.lang.ClassValue");
        } catch (ClassNotFoundException e) {
            cv = false;
        }
        HAS_CLASS_VALUE = cv;
    }

    public GroovySystemLoader forClassLoader(ClassLoader classLoader) {
        if (!HAS_CLASS_VALUE) {
            return NOT_BROKEN;
        }
        try {
            Class<?> groovySystem;
            try {
                groovySystem = classLoader.loadClass("groovy.lang.GroovySystem");
            } catch (ClassNotFoundException e) {
                // Not a Groovy implementation, or not an implementation that we need to deal with
                return NOT_BROKEN;
            }
            if (groovySystem.getClassLoader() != classLoader) {
                // Groovy implementation visible from somewhere else
                return NOT_BROKEN;
            }

            String versionString;
            try {
                Method getVersion = groovySystem.getDeclaredMethod("getVersion");
                versionString = (String) getVersion.invoke(null);
            } catch (NoSuchMethodException ex) {
                return NOT_BROKEN;
            }
            VersionNumber groovyVersion = VersionNumber.parse(versionString);
            boolean isFaultyGroovy = groovyVersion.getMajor() == 2 && groovyVersion.getMinor() == 4 && groovyVersion.getMicro() < 5;
            return isFaultyGroovy ? new LeakyOnJava7GroovySystemLoader(classLoader) : NOT_BROKEN;
        } catch (Exception e) {
            throw new GradleException("Could not inspect the Groovy system for ClassLoader " + classLoader, e);
        }
    }
}

@@ -814,58 +814,8 @@
 y %7B%0A
-    private final static boolean HAS_CLASS_VALUE;%0A
     
@@ -909,369 +909,75 @@
     
-static %7B%0A        boolean cv = true;%0A        try %7B%0A            Class.forName(%22java.lang.ClassValue%22);%0A        %7D catch (ClassNotFoundException e) %7B%0A            cv = false;%0A        %7D%0A        HAS_CLASS_VALUE = cv;%0A    %7D%0A%0A    public GroovySystemLoader forClassLoader(ClassLoader classLoader) %7B%0A        if (!HAS_CLASS_VALUE) %7B%0A            return NOT_BROKEN;%0A        %7D
+public GroovySystemLoader forClassLoader(ClassLoader classLoader) %7B
 %0A   
@@ -1981,40 +1981,8 @@
 == 4
- && groovyVersion.getMicro() %3C 5
 ;%0A  

Fix Groovy version check: all versions of Groovy 2.4+ are affected, regardless of ClassValue