src/share/classes/com/sun/tools/javac/code/Kinds.java

Thu, 20 Jun 2013 08:45:43 +0100

author
vromero
date
Thu, 20 Jun 2013 08:45:43 +0100
changeset 1850
6debfa63a4a1
parent 1442
fcf89720ae71
child 1853
831467c4c6a7
permissions
-rw-r--r--

8016613: javac should avoid source 8 only analysis when compiling for source 7
Reviewed-by: jjg
Contributed-by: maurizio.cimadamore@oracle.com

     1 /*
     2  * Copyright (c) 1999, 2013, 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  */
    26 package com.sun.tools.javac.code;
    28 import java.util.EnumSet;
    29 import java.util.Locale;
    31 import com.sun.source.tree.MemberReferenceTree;
    32 import com.sun.tools.javac.api.Formattable;
    33 import com.sun.tools.javac.api.Messages;
    34 import static com.sun.tools.javac.code.Flags.*;
    35 import static com.sun.tools.javac.code.TypeTag.CLASS;
    36 import static com.sun.tools.javac.code.TypeTag.PACKAGE;
    37 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
    39 /** Internal symbol kinds, which distinguish between elements of
    40  *  different subclasses of Symbol. Symbol kinds are organized so they can be
    41  *  or'ed to sets.
    42  *
    43  *  <p><b>This is NOT part of any supported API.
    44  *  If you write code that depends on this, you do so at your own risk.
    45  *  This code and its internal interfaces are subject to change or
    46  *  deletion without notice.</b>
    47  */
    48 public class Kinds {
    50     private Kinds() {} // uninstantiable
    52     /** The empty set of kinds.
    53      */
    54     public final static int NIL = 0;
    56     /** The kind of package symbols.
    57      */
    58     public final static int PCK = 1 << 0;
    60     /** The kind of type symbols (classes, interfaces and type variables).
    61      */
    62     public final static int TYP = 1 << 1;
    64     /** The kind of variable symbols.
    65      */
    66     public final static int VAR = 1 << 2;
    68     /** The kind of values (variables or non-variable expressions), includes VAR.
    69      */
    70     public final static int VAL = (1 << 3) | VAR;
    72     /** The kind of methods.
    73      */
    74     public final static int MTH = 1 << 4;
    76     /** Poly kind, for deferred types.
    77      */
    78     public final static int POLY = 1 << 5;
    80     /** The error kind, which includes all other kinds.
    81      */
    82     public final static int ERR = (1 << 6) - 1;
    84     /** The set of all kinds.
    85      */
    86     public final static int AllKinds = ERR;
    88     /** Kinds for erroneous symbols that complement the above
    89      */
    90     public static final int ERRONEOUS = 1 << 7;
    91     public static final int AMBIGUOUS    = ERRONEOUS+1; // ambiguous reference
    92     public static final int HIDDEN       = ERRONEOUS+2; // hidden method or field
    93     public static final int STATICERR    = ERRONEOUS+3; // nonstatic member from static context
    94     public static final int MISSING_ENCL = ERRONEOUS+4; // missing enclosing class
    95     public static final int ABSENT_VAR   = ERRONEOUS+5; // missing variable
    96     public static final int WRONG_MTHS   = ERRONEOUS+6; // methods with wrong arguments
    97     public static final int WRONG_MTH    = ERRONEOUS+7; // one method with wrong arguments
    98     public static final int ABSENT_MTH   = ERRONEOUS+8; // missing method
    99     public static final int ABSENT_TYP   = ERRONEOUS+9; // missing type
   101     public enum KindName implements Formattable {
   102         ANNOTATION("kindname.annotation"),
   103         CONSTRUCTOR("kindname.constructor"),
   104         INTERFACE("kindname.interface"),
   105         ENUM("kindname.enum"),
   106         STATIC("kindname.static"),
   107         TYPEVAR("kindname.type.variable"),
   108         BOUND("kindname.type.variable.bound"),
   109         VAR("kindname.variable"),
   110         VAL("kindname.value"),
   111         METHOD("kindname.method"),
   112         CLASS("kindname.class"),
   113         STATIC_INIT("kindname.static.init"),
   114         INSTANCE_INIT("kindname.instance.init"),
   115         PACKAGE("kindname.package");
   117         private final String name;
   119         KindName(String name) {
   120             this.name = name;
   121         }
   123         public String toString() {
   124             return name;
   125         }
   127         public String getKind() {
   128             return "Kindname";
   129         }
   131         public String toString(Locale locale, Messages messages) {
   132             String s = toString();
   133             return messages.getLocalizedString(locale, "compiler.misc." + s);
   134         }
   135     }
   137     /** A KindName representing a given symbol kind
   138      */
   139     public static KindName kindName(int kind) {
   140         switch (kind) {
   141         case PCK: return KindName.PACKAGE;
   142         case TYP: return KindName.CLASS;
   143         case VAR: return KindName.VAR;
   144         case VAL: return KindName.VAL;
   145         case MTH: return KindName.METHOD;
   146             default : throw new AssertionError("Unexpected kind: "+kind);
   147         }
   148     }
   150     public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
   151         switch (mode) {
   152             case INVOKE: return KindName.METHOD;
   153             case NEW: return KindName.CONSTRUCTOR;
   154             default : throw new AssertionError("Unexpected mode: "+ mode);
   155         }
   156     }
   158     /** A KindName representing a given symbol
   159      */
   160     public static KindName kindName(Symbol sym) {
   161         switch (sym.getKind()) {
   162         case PACKAGE:
   163             return KindName.PACKAGE;
   165         case ENUM:
   166             return KindName.ENUM;
   168         case ANNOTATION_TYPE:
   169         case CLASS:
   170             return KindName.CLASS;
   172         case INTERFACE:
   173             return KindName.INTERFACE;
   175         case TYPE_PARAMETER:
   176             return KindName.TYPEVAR;
   178         case ENUM_CONSTANT:
   179         case FIELD:
   180         case PARAMETER:
   181         case LOCAL_VARIABLE:
   182         case EXCEPTION_PARAMETER:
   183         case RESOURCE_VARIABLE:
   184             return KindName.VAR;
   186         case CONSTRUCTOR:
   187             return KindName.CONSTRUCTOR;
   189         case METHOD:
   190             return KindName.METHOD;
   191         case STATIC_INIT:
   192             return KindName.STATIC_INIT;
   193         case INSTANCE_INIT:
   194             return KindName.INSTANCE_INIT;
   196         default:
   197             if (sym.kind == VAL)
   198                 // I don't think this can happen but it can't harm
   199                 // playing it safe --ahe
   200                 return KindName.VAL;
   201             else
   202                 throw new AssertionError("Unexpected kind: "+sym.getKind());
   203         }
   204     }
   206     /** A set of KindName(s) representing a set of symbol's kinds.
   207      */
   208     public static EnumSet<KindName> kindNames(int kind) {
   209         EnumSet<KindName> kinds = EnumSet.noneOf(KindName.class);
   210         if ((kind & VAL) != 0)
   211             kinds.add(((kind & VAL) == VAR) ? KindName.VAR : KindName.VAL);
   212         if ((kind & MTH) != 0) kinds.add(KindName.METHOD);
   213         if ((kind & TYP) != 0) kinds.add(KindName.CLASS);
   214         if ((kind & PCK) != 0) kinds.add(KindName.PACKAGE);
   215         return kinds;
   216     }
   218     /** A KindName representing the kind of a given class/interface type.
   219      */
   220     public static KindName typeKindName(Type t) {
   221         if (t.tag == TYPEVAR ||
   222             t.tag == CLASS && (t.tsym.flags() & COMPOUND) != 0)
   223             return KindName.BOUND;
   224         else if (t.tag == PACKAGE)
   225             return KindName.PACKAGE;
   226         else if ((t.tsym.flags_field & ANNOTATION) != 0)
   227             return KindName.ANNOTATION;
   228         else if ((t.tsym.flags_field & INTERFACE) != 0)
   229             return KindName.INTERFACE;
   230         else
   231             return KindName.CLASS;
   232     }
   234     /** A KindName representing the kind of a a missing symbol, given an
   235      *  error kind.
   236      * */
   237     public static KindName absentKind(int kind) {
   238         switch (kind) {
   239         case ABSENT_VAR:
   240             return KindName.VAR;
   241         case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH:
   242             return KindName.METHOD;
   243         case ABSENT_TYP:
   244             return KindName.CLASS;
   245         default:
   246             throw new AssertionError("Unexpected kind: "+kind);
   247         }
   248     }
   249 }

mercurial