/*
 * 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;
  }
}

@@ -2114,123 +2114,115 @@
     
-Class%3C?%3E versionClass = Class.forName(%22java.lang.Runtime.Version%22);%0A      Method m = versionClass.getMethod(%22major%22
+Method versionMethod = Runtime.class.getMethod(%22version%22);%0A      Object version = versionMethod.invoke(null
 );%0A 
@@ -2226,23 +2226,19 @@
 ;%0A      
-I
+i
 nt
-eger
  majorVe
@@ -2250,26 +2250,58 @@
  = (
-Integer) m
+int) version.getClass().getMethod(%22major%22)
 .invoke(
 null
@@ -2288,36 +2288,39 @@
 %22major%22).invoke(
-null
+version
 );%0A      return 
@@ -2357,19 +2357,8 @@
 sion
-.intValue()
 );%0A 

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