langtools/src/share/classes/com/sun/tools/javac/code/Source.java
author mcimadamore
Tue, 16 Jun 2009 10:46:37 +0100
changeset 3140 15a274b13051
parent 2982 090c71af18f6
child 3149 0cd06d598d6f
permissions -rw-r--r--
6638712: Inference with wildcard types causes selection of inapplicable method Summary: Added global sanity check in order to make sure that return type inference does not violate bounds constraints Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.code;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.jvm.Target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import static javax.lang.model.SourceVersion.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
/** The source language version accepted.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
public enum Source {
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    /** 1.0 had no inner classes, and so could not pass the JCK. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    // public static final Source JDK1_0 =              new Source("1.0");
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    /** 1.1 did not have strictfp, and so could not pass the JCK. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    // public static final Source JDK1_1 =              new Source("1.1");
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    /** 1.2 introduced strictfp. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    JDK1_2("1.2"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    /** 1.3 is the same language as 1.2. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    JDK1_3("1.3"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /** 1.4 introduced assert. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    JDK1_4("1.4"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    /** 1.5 introduced generics, attributes, foreach, boxing, static import,
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     *  covariant return, enums, varargs, et al. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    JDK1_5("1.5"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /** 1.6 reports encoding problems as errors instead of warnings. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    JDK1_6("1.6"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    /** 1.7 covers the to be determined language features that will be added in JDK 7. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    JDK1_7("1.7");
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    private static final Context.Key<Source> sourceKey
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        = new Context.Key<Source>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public static Source instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        Source instance = context.get(sourceKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        if (instance == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            String sourceString = options.get("-source");
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            if (sourceString != null) instance = lookup(sourceString);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            if (instance == null) instance = DEFAULT;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            context.put(sourceKey, instance);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    public final String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    private static Map<String,Source> tab = new HashMap<String,Source>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    static {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        for (Source s : values()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            tab.put(s.name, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        tab.put("5", JDK1_5); // Make 5 an alias for 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        tab.put("6", JDK1_6); // Make 6 an alias for 1.6
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        tab.put("7", JDK1_7); // Make 7 an alias for 1.7
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    private Source(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
2982
090c71af18f6 6827026: Change javac source and target default to 7
jjg
parents: 10
diff changeset
    98
    public static final Source DEFAULT = JDK1_7;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    public static Source lookup(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        return tab.get(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public Target requiredTarget() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        return Target.JDK1_1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    /** Allow encoding errors, giving only warnings. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public boolean allowEncodingErrors() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        return compareTo(JDK1_6) < 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    public boolean allowAsserts() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        return compareTo(JDK1_4) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    public boolean allowCovariantReturns() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public boolean allowGenerics() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    public boolean allowEnums() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    public boolean allowForeach() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    public boolean allowStaticImport() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public boolean allowBoxing() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    public boolean allowVarargs() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    public boolean allowAnnotations() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    // hex floating-point literals supported?
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    public boolean allowHexFloats() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    public boolean allowAnonOuterThis() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    public boolean addBridges() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    public boolean enforceMandatoryWarnings() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        return compareTo(JDK1_5) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    public static SourceVersion toSourceVersion(Source source) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        switch(source) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        case JDK1_2:
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            return RELEASE_2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        case JDK1_3:
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            return RELEASE_3;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        case JDK1_4:
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            return RELEASE_4;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        case JDK1_5:
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            return RELEASE_5;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        case JDK1_6:
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            return RELEASE_6;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        case JDK1_7:
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            return RELEASE_7;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
}