class AbstractCompilesUtil {

    @NonNull
    @VisibleForTesting
    public static JavaVersion getDefaultJavaVersion(@NonNull String compileSdkVersion) {
        String currentJdkVersion = System.getProperty("java.specification.version");
        final AndroidVersion hash = AndroidTargetHash.getVersionFromHash(compileSdkVersion);
        Integer compileSdkLevel = (hash == null ? null : hash.getFeatureLevel());

        JavaVersion javaVersionToUse;
        if (compileSdkLevel == null) {
            javaVersionToUse = JavaVersion.VERSION_1_6;
        } else {
            if (0 < compileSdkLevel && compileSdkLevel <= 20) {
                javaVersionToUse = JavaVersion.VERSION_1_6;
            } else if (21 <= compileSdkLevel && compileSdkLevel < 24) {
                javaVersionToUse = JavaVersion.VERSION_1_7;
            } else {
                javaVersionToUse = JavaVersion.VERSION_1_7;
            }
        }

        JavaVersion jdkVersion = JavaVersion.toVersion(currentJdkVersion);

        if (jdkVersion.compareTo(javaVersionToUse) < 0) {
            Logging.getLogger(AbstractCompilesUtil.class).warn(
                    "Default language level for compileSdkVersion '{}' is " +
                            "{}, but the JDK used is {}, so the JDK language level will be used.",
                    compileSdkVersion,
                    javaVersionToUse,
                    jdkVersion);
            javaVersionToUse = jdkVersion;
        }
        return javaVersionToUse;
    }

}