Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1999, 2012, 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.TypeTags.*;
37 /** Internal symbol kinds, which distinguish between elements of
38 * different subclasses of Symbol. Symbol kinds are organized so they can be
39 * or'ed to sets.
40 *
41 * <p><b>This is NOT part of any supported API.
42 * If you write code that depends on this, you do so at your own risk.
43 * This code and its internal interfaces are subject to change or
44 * deletion without notice.</b>
45 */
46 public class Kinds {
48 private Kinds() {} // uninstantiable
50 /** The empty set of kinds.
51 */
52 public final static int NIL = 0;
54 /** The kind of package symbols.
55 */
56 public final static int PCK = 1 << 0;
58 /** The kind of type symbols (classes, interfaces and type variables).
59 */
60 public final static int TYP = 1 << 1;
62 /** The kind of variable symbols.
63 */
64 public final static int VAR = 1 << 2;
66 /** The kind of values (variables or non-variable expressions), includes VAR.
67 */
68 public final static int VAL = (1 << 3) | VAR;
70 /** The kind of methods.
71 */
72 public final static int MTH = 1 << 4;
74 /** The error kind, which includes all other kinds.
75 */
76 public final static int ERR = (1 << 5) - 1;
78 /** The set of all kinds.
79 */
80 public final static int AllKinds = ERR;
82 /** Kinds for erroneous symbols that complement the above
83 */
84 public static final int ERRONEOUS = 1 << 6;
85 public static final int AMBIGUOUS = ERRONEOUS+1; // ambiguous reference
86 public static final int HIDDEN = ERRONEOUS+2; // hidden method or field
87 public static final int STATICERR = ERRONEOUS+3; // nonstatic member from static context
88 public static final int MISSING_ENCL = ERRONEOUS+4; // missing enclosing class
89 public static final int ABSENT_VAR = ERRONEOUS+5; // missing variable
90 public static final int WRONG_MTHS = ERRONEOUS+6; // methods with wrong arguments
91 public static final int WRONG_MTH = ERRONEOUS+7; // one method with wrong arguments
92 public static final int ABSENT_MTH = ERRONEOUS+8; // missing method
93 public static final int ABSENT_TYP = ERRONEOUS+9; // missing type
95 public enum KindName implements Formattable {
96 ANNOTATION("kindname.annotation"),
97 CONSTRUCTOR("kindname.constructor"),
98 INTERFACE("kindname.interface"),
99 ENUM("kindname.enum"),
100 STATIC("kindname.static"),
101 TYPEVAR("kindname.type.variable"),
102 BOUND("kindname.type.variable.bound"),
103 VAR("kindname.variable"),
104 VAL("kindname.value"),
105 METHOD("kindname.method"),
106 CLASS("kindname.class"),
107 STATIC_INIT("kindname.static.init"),
108 INSTANCE_INIT("kindname.instance.init"),
109 PACKAGE("kindname.package");
111 private String name;
113 KindName(String name) {
114 this.name = name;
115 }
117 public String toString() {
118 return name;
119 }
121 public String getKind() {
122 return "Kindname";
123 }
125 public String toString(Locale locale, Messages messages) {
126 String s = toString();
127 return messages.getLocalizedString(locale, "compiler.misc." + s);
128 }
129 }
131 /** A KindName representing a given symbol kind
132 */
133 public static KindName kindName(int kind) {
134 switch (kind) {
135 case PCK: return KindName.PACKAGE;
136 case TYP: return KindName.CLASS;
137 case VAR: return KindName.VAR;
138 case VAL: return KindName.VAL;
139 case MTH: return KindName.METHOD;
140 default : throw new AssertionError("Unexpected kind: "+kind);
141 }
142 }
144 public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
145 switch (mode) {
146 case INVOKE: return KindName.METHOD;
147 case NEW: return KindName.CONSTRUCTOR;
148 default : throw new AssertionError("Unexpected mode: "+ mode);
149 }
150 }
152 /** A KindName representing a given symbol
153 */
154 public static KindName kindName(Symbol sym) {
155 switch (sym.getKind()) {
156 case PACKAGE:
157 return KindName.PACKAGE;
159 case ENUM:
160 return KindName.ENUM;
162 case ANNOTATION_TYPE:
163 case CLASS:
164 return KindName.CLASS;
166 case INTERFACE:
167 return KindName.INTERFACE;
169 case TYPE_PARAMETER:
170 return KindName.TYPEVAR;
172 case ENUM_CONSTANT:
173 case FIELD:
174 case PARAMETER:
175 case LOCAL_VARIABLE:
176 case EXCEPTION_PARAMETER:
177 case RESOURCE_VARIABLE:
178 return KindName.VAR;
180 case CONSTRUCTOR:
181 return KindName.CONSTRUCTOR;
183 case METHOD:
184 return KindName.METHOD;
185 case STATIC_INIT:
186 return KindName.STATIC_INIT;
187 case INSTANCE_INIT:
188 return KindName.INSTANCE_INIT;
190 default:
191 if (sym.kind == VAL)
192 // I don't think this can happen but it can't harm
193 // playing it safe --ahe
194 return KindName.VAL;
195 else
196 throw new AssertionError("Unexpected kind: "+sym.getKind());
197 }
198 }
200 /** A set of KindName(s) representing a set of symbol's kinds.
201 */
202 public static EnumSet<KindName> kindNames(int kind) {
203 EnumSet<KindName> kinds = EnumSet.noneOf(KindName.class);
204 if ((kind & VAL) != 0)
205 kinds.add(((kind & VAL) == VAR) ? KindName.VAR : KindName.VAL);
206 if ((kind & MTH) != 0) kinds.add(KindName.METHOD);
207 if ((kind & TYP) != 0) kinds.add(KindName.CLASS);
208 if ((kind & PCK) != 0) kinds.add(KindName.PACKAGE);
209 return kinds;
210 }
212 /** A KindName representing the kind of a given class/interface type.
213 */
214 public static KindName typeKindName(Type t) {
215 if (t.tag == TYPEVAR ||
216 t.tag == CLASS && (t.tsym.flags() & COMPOUND) != 0)
217 return KindName.BOUND;
218 else if (t.tag == PACKAGE)
219 return KindName.PACKAGE;
220 else if ((t.tsym.flags_field & ANNOTATION) != 0)
221 return KindName.ANNOTATION;
222 else if ((t.tsym.flags_field & INTERFACE) != 0)
223 return KindName.INTERFACE;
224 else
225 return KindName.CLASS;
226 }
228 /** A KindName representing the kind of a a missing symbol, given an
229 * error kind.
230 * */
231 public static KindName absentKind(int kind) {
232 switch (kind) {
233 case ABSENT_VAR:
234 return KindName.VAR;
235 case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH:
236 return KindName.METHOD;
237 case ABSENT_TYP:
238 return KindName.CLASS;
239 default:
240 throw new AssertionError("Unexpected kind: "+kind);
241 }
242 }
243 }