/*
 * 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 com.google.devtools.j2objc.util;

import java.lang.reflect.Method;

/**
 * Supported Java versions, used by the -source and -target flags.
 */
public enum SourceVersion {

  JAVA_9(9, "1.9"), JAVA_8(8, "1.8"), JAVA_7(7, "1.7"), JAVA_6(6, "1.6"), JAVA_5(5, "1.5");

  private final int version;
  private final String flag;

  SourceVersion(int version, String flag) {
    this.flag = flag;
    this.version = version;
  }

  public int version() {
    return version;
  }

  public String flag() {
    return flag;
  }

  public static SourceVersion parse(String flag) {
    String fullFlag = flag.length() == 1 ? "1." + flag : flag;
    for (SourceVersion sv : values()) {
      if (sv.flag.equals(fullFlag)) {
        return sv;
      }
    }
    throw new IllegalArgumentException(flag);
  }

  public static SourceVersion valueOf(int majorVersion) {
    for (SourceVersion sv : values()) {
      if (sv.version == majorVersion) {
        return sv;
      }
    }
    throw new IllegalArgumentException("Unsupported version: " + majorVersion);
  }

  public static boolean java7Minimum(SourceVersion sourceVersion) {
    return sourceVersion.version >= 7;
  }

  public static boolean java8Minimum(SourceVersion sourceVersion) {
    return sourceVersion.version >= 8;
  }

  public static boolean java9Minimum(SourceVersion sourceVersion) {
    return sourceVersion.version >= 9;
  }

  /**
   * Returns the source version value associated with the runtime currently running.
   */
  public static SourceVersion defaultVersion() {
    try {
      Class<?> versionClass = Class.forName("java.lang.Runtime.Version");
      Method m = versionClass.getMethod("major");
      Integer majorVersion = (Integer) m.invoke(null);
      return SourceVersion.valueOf(majorVersion.intValue());
    } catch (Exception e) {
      SourceVersion sysVer = SourceVersion.parse(System.getProperty("java.specification.version"));
      // TODO(tball): remove when Java 9 source is supported.
      return sysVer == JAVA_9 ? JAVA_8 : sysVer;
    }
  }

  @Override
  public String toString() {
    return flag;
  }
}

/*
 * 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 com.google.devtools.j2objc.util;

import java.lang.reflect.Method;

/**
 * Supported Java versions, used by the -source and -target flags.
 */
public enum SourceVersion {

  JAVA_9(9, "1.9"), JAVA_8(8, "1.8"), JAVA_7(7, "1.7"), JAVA_6(6, "1.6"), JAVA_5(5, "1.5");

  private final int version;
  private final String flag;

  SourceVersion(int version, String flag) {
    this.flag = flag;
    this.version = version;
  }

  public int version() {
    return version;
  }

  public String flag() {
    return flag;
  }

  public static SourceVersion parse(String flag) {
    String fullFlag = flag.length() == 1 ? "1." + flag : flag;
    for (SourceVersion sv : values()) {
      if (sv.flag.equals(fullFlag)) {
        return sv;
      }
    }
    throw new IllegalArgumentException(flag);
  }

  public static SourceVersion valueOf(int majorVersion) {
    for (SourceVersion sv : values()) {
      if (sv.version == majorVersion) {
        return sv;
      }
    }
    throw new IllegalArgumentException("Unsupported version: " + majorVersion);
  }

  public static boolean java7Minimum(SourceVersion sourceVersion) {
    return sourceVersion.version >= 7;
  }

  public static boolean java8Minimum(SourceVersion sourceVersion) {
    return sourceVersion.version >= 8;
  }

  public static boolean java9Minimum(SourceVersion sourceVersion) {
    return sourceVersion.version >= 9;
  }

  /**
   * Returns the source version value associated with the runtime currently running.
   */
  public static SourceVersion defaultVersion() {
    try {
      Method versionMethod = Runtime.class.getMethod("version");
      Object version = versionMethod.invoke(null);
      int majorVersion = (int) version.getClass().getMethod("major").invoke(version);
      return SourceVersion.valueOf(majorVersion);
    } catch (Exception e) {
      SourceVersion sysVer = SourceVersion.parse(System.getProperty("java.specification.version"));
      // TODO(tball): remove when Java 9 source is supported.
      return sysVer == JAVA_9 ? JAVA_8 : sysVer;
    }
  }

  @Override
  public String toString() {
    return flag;
  }
}

Fix reflective access of `Runtime.version().major()`