1 /*
   2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.jvm;
  27 
  28 import java.util.*;
  29 
  30 import com.sun.tools.javac.code.Flags;
  31 import com.sun.tools.javac.code.Symbol;
  32 import com.sun.tools.javac.util.*;
  33 
  34 import static com.sun.tools.javac.main.Option.TARGET;
  35 
  36 /** The classfile version target.
  37  *
  38  *  <p><b>This is NOT part of any supported API.
  39  *  If you write code that depends on this, you do so at your own risk.
  40  *  This code and its internal interfaces are subject to change or
  41  *  deletion without notice.</b>
  42  */
  43 public enum Target {
  44     JDK1_1("1.1", 45, 3),
  45     JDK1_2("1.2", 46, 0),
  46     JDK1_3("1.3", 47, 0),
  47 
  48     /** J2SE1.4 = Merlin. */
  49     JDK1_4("1.4", 48, 0),
  50 
  51     /** JDK 5, codename Tiger. */
  52     JDK1_5("1.5", 49, 0),
  53 
  54     /** JDK 6. */
  55     JDK1_6("1.6", 50, 0),
  56 
  57     /** JDK 7. */
  58     JDK1_7("1.7", 51, 0),
  59 
  60     /** JDK 8. */
  61     JDK1_8("1.8", 52, 0),
  62 
  63     /** JDK 9. */
  64     JDK1_9("1.9", 53, 0),
  65 
  66     /** JDK 10. */
  67     JDK1_10("1.10", 54, 0),
  68 
  69     /** JDK 11. */
  70     JDK1_11("11", 55, 0);
  71 
  72     private static final Context.Key<Target> targetKey = new Context.Key<>();
  73 
  74     public static Target instance(Context context) {
  75         Target instance = context.get(targetKey);
  76         if (instance == null) {
  77             Options options = Options.instance(context);
  78             String targetString = options.get(TARGET);
  79             if (targetString != null) instance = lookup(targetString);
  80             if (instance == null) instance = DEFAULT;
  81             context.put(targetKey, instance);
  82         }
  83         return instance;
  84     }
  85 
  86     public static final Target MIN = Target.JDK1_6;
  87 
  88     private static final Target MAX = values()[values().length - 1];
  89 
  90     private static final Map<String,Target> tab = new HashMap<>();
  91     static {
  92         for (Target t : values()) {
  93             tab.put(t.name, t);
  94         }
  95         tab.put("5", JDK1_5);
  96         tab.put("6", JDK1_6);
  97         tab.put("7", JDK1_7);
  98         tab.put("8", JDK1_8);
  99         tab.put("9", JDK1_9);
 100         tab.put("10", JDK1_10);
 101         tab.put("11", JDK1_11);
 102     }
 103 
 104     public final String name;
 105     public final int majorVersion;
 106     public final int minorVersion;
 107     private Target(String name, int majorVersion, int minorVersion) {
 108         this.name = name;
 109         this.majorVersion = majorVersion;
 110         this.minorVersion = minorVersion;
 111     }
 112 
 113     public static final Target DEFAULT = values()[values().length - 1];
 114 
 115     public static Target lookup(String name) {
 116         return tab.get(name);
 117     }
 118 
 119     /** Return the character to be used in constructing synthetic
 120      *  identifiers, where not specified by the JLS.
 121      */
 122     public char syntheticNameChar() {
 123         return '$';
 124     }
 125 
 126     /** Does the VM support an invokedynamic instruction?
 127      */
 128     public boolean hasInvokedynamic() {
 129         return compareTo(JDK1_7) >= 0;
 130     }
 131 
 132     /** Does the target JDK contains the java.util.Objects class?
 133      */
 134     public boolean hasObjects() {
 135         return compareTo(JDK1_7) >= 0;
 136     }
 137 
 138     /** Does the VM support polymorphic method handle invocation?
 139      *  Affects the linkage information output to the classfile.
 140      *  An alias for {@code hasInvokedynamic}, since all the JSR 292 features appear together.
 141      */
 142     public boolean hasMethodHandles() {
 143         return hasInvokedynamic();
 144     }
 145 
 146     /** Does the target JDK contain StringConcatFactory class?
 147      */
 148     public boolean hasStringConcatFactory() {
 149         return compareTo(JDK1_9) >= 0;
 150     }
 151 
 152     /** Value of platform release used to access multi-release jar files
 153      */
 154     public String multiReleaseValue() {
 155         return Integer.toString(this.ordinal() - Target.JDK1_1.ordinal() + 1);
 156     }
 157 
 158     /** Does the target VM support nestmate access?
 159      */
 160     public boolean hasNestmateAccess() {
 161         return compareTo(JDK1_11) >= 0;
 162     }
 163 
 164     /** Does the target VM support virtual private invocations?
 165      */
 166     public boolean hasVirtualPrivateInvoke() {
 167         return compareTo(JDK1_11) >= 0;
 168     }
 169 
 170 }