langtools/src/share/classes/com/sun/tools/javac/code/Kinds.java
changeset 939 38e24969c7e9
parent 10 06bc494ca11e
child 1264 076a3cde30d5
equal deleted inserted replaced
938:13aae74ca013 939:38e24969c7e9
    23  * have any questions.
    23  * have any questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.tools.javac.code;
    26 package com.sun.tools.javac.code;
    27 
    27 
       
    28 import java.util.EnumSet;
       
    29 import java.util.ResourceBundle;
       
    30 
       
    31 import com.sun.tools.javac.api.Formattable;
       
    32 
       
    33 import static com.sun.tools.javac.code.TypeTags.*;
       
    34 import static com.sun.tools.javac.code.Flags.*;
    28 
    35 
    29 /** Internal symbol kinds, which distinguish between elements of
    36 /** Internal symbol kinds, which distinguish between elements of
    30  *  different subclasses of Symbol. Symbol kinds are organized so they can be
    37  *  different subclasses of Symbol. Symbol kinds are organized so they can be
    31  *  or'ed to sets.
    38  *  or'ed to sets.
    32  *
    39  *
    80     public static final int ABSENT_VAR   = ERRONEOUS+4; // missing variable
    87     public static final int ABSENT_VAR   = ERRONEOUS+4; // missing variable
    81     public static final int WRONG_MTHS   = ERRONEOUS+5; // methods with wrong arguments
    88     public static final int WRONG_MTHS   = ERRONEOUS+5; // methods with wrong arguments
    82     public static final int WRONG_MTH    = ERRONEOUS+6; // one method with wrong arguments
    89     public static final int WRONG_MTH    = ERRONEOUS+6; // one method with wrong arguments
    83     public static final int ABSENT_MTH   = ERRONEOUS+7; // missing method
    90     public static final int ABSENT_MTH   = ERRONEOUS+7; // missing method
    84     public static final int ABSENT_TYP   = ERRONEOUS+8; // missing type
    91     public static final int ABSENT_TYP   = ERRONEOUS+8; // missing type
       
    92 
       
    93     public enum KindName implements Formattable {
       
    94         ANNOTATION("kindname.interface"),
       
    95         CONSTRUCTOR("kindname.constructor"),
       
    96         INTERFACE("kindname.interface"),
       
    97         STATIC("kindname.static"),
       
    98         TYPEVAR("kindname.type.variable"),
       
    99         BOUND("kindname.type.variable.bound"),
       
   100         VAR("kindname.variable"),
       
   101         VAL("kindname.value"),
       
   102         METHOD("kindname.method"),
       
   103         CLASS("kindname.class"),
       
   104         PACKAGE("kindname.package");
       
   105 
       
   106         private String name;
       
   107 
       
   108         KindName(String name) {
       
   109             this.name = name;
       
   110         }
       
   111 
       
   112         public String toString() {
       
   113             return name;
       
   114         }
       
   115 
       
   116         public String getKind() {
       
   117             return "Kindname";
       
   118         }
       
   119 
       
   120         public String toString(ResourceBundle bundle) {
       
   121             String s = toString();
       
   122             return bundle.getString("compiler.misc." + s);
       
   123         }
       
   124     }
       
   125 
       
   126     /** A KindName representing a given symbol kind
       
   127      */
       
   128     public static KindName kindName(int kind) {
       
   129         switch (kind) {
       
   130         case PCK: return KindName.PACKAGE;
       
   131         case TYP: return KindName.CLASS;
       
   132         case VAR: return KindName.VAR;
       
   133         case VAL: return KindName.VAL;
       
   134         case MTH: return KindName.METHOD;
       
   135             default : throw new AssertionError("Unexpected kind: "+kind);
       
   136         }
       
   137     }
       
   138 
       
   139     /** A KindName representing a given symbol
       
   140      */
       
   141     public static KindName kindName(Symbol sym) {
       
   142         switch (sym.getKind()) {
       
   143         case PACKAGE:
       
   144             return KindName.PACKAGE;
       
   145 
       
   146         case ENUM:
       
   147         case ANNOTATION_TYPE:
       
   148         case INTERFACE:
       
   149         case CLASS:
       
   150             return KindName.CLASS;
       
   151 
       
   152         case TYPE_PARAMETER:
       
   153             return KindName.TYPEVAR;
       
   154 
       
   155         case ENUM_CONSTANT:
       
   156         case FIELD:
       
   157         case PARAMETER:
       
   158         case LOCAL_VARIABLE:
       
   159         case EXCEPTION_PARAMETER:
       
   160             return KindName.VAR;
       
   161 
       
   162         case METHOD:
       
   163         case CONSTRUCTOR:
       
   164         case STATIC_INIT:
       
   165         case INSTANCE_INIT:
       
   166             return KindName.METHOD;
       
   167 
       
   168         default:
       
   169             if (sym.kind == VAL)
       
   170                 // I don't think this can happen but it can't harm
       
   171                 // playing it safe --ahe
       
   172                 return KindName.VAL;
       
   173             else
       
   174                 throw new AssertionError("Unexpected kind: "+sym.getKind());
       
   175         }
       
   176     }
       
   177 
       
   178     /** A set of KindName(s) representing a set of symbol's kinds.
       
   179      */
       
   180     public static EnumSet<KindName> kindNames(int kind) {
       
   181         EnumSet<KindName> kinds = EnumSet.noneOf(KindName.class);
       
   182         if ((kind & VAL) != 0)
       
   183             kinds.add(((kind & VAL) == VAR) ? KindName.VAR : KindName.VAL);
       
   184         if ((kind & MTH) != 0) kinds.add(KindName.METHOD);
       
   185         if ((kind & TYP) != 0) kinds.add(KindName.CLASS);
       
   186         if ((kind & PCK) != 0) kinds.add(KindName.PACKAGE);
       
   187         return kinds;
       
   188     }
       
   189 
       
   190     /** A KindName representing the kind of a given class/interface type.
       
   191      */
       
   192     public static KindName typeKindName(Type t) {
       
   193         if (t.tag == TYPEVAR ||
       
   194             t.tag == CLASS && (t.tsym.flags() & COMPOUND) != 0)
       
   195             return KindName.BOUND;
       
   196         else if (t.tag == PACKAGE)
       
   197             return KindName.PACKAGE;
       
   198         else if ((t.tsym.flags_field & ANNOTATION) != 0)
       
   199             return KindName.ANNOTATION;
       
   200         else if ((t.tsym.flags_field & INTERFACE) != 0)
       
   201             return KindName.INTERFACE;
       
   202         else
       
   203             return KindName.CLASS;
       
   204     }
       
   205 
       
   206     /** A KindName representing the kind of a a missing symbol, given an
       
   207      *  error kind.
       
   208      * */
       
   209     public static KindName absentKind(int kind) {
       
   210         switch (kind) {
       
   211         case ABSENT_VAR:
       
   212             return KindName.VAR;
       
   213         case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH:
       
   214             return KindName.METHOD;
       
   215         case ABSENT_TYP:
       
   216             return KindName.CLASS;
       
   217         default:
       
   218             throw new AssertionError("Unexpected kind: "+kind);
       
   219         }
       
   220     }
    85 }
   221 }