1 /*
  2  * Copyright (c) 1999, 2021, 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.util;
 27 
 28 /**
 29  * Access to the compiler's name table.  Standard names are defined,
 30  * as well as methods to create new names.
 31  *
 32  *  <p><b>This is NOT part of any supported API.
 33  *  If you write code that depends on this, you do so at your own risk.
 34  *  This code and its internal interfaces are subject to change or
 35  *  deletion without notice.</b>
 36  */
 37 public class Names {
 38 
 39     public static final Context.Key<Names> namesKey = new Context.Key<>();
 40 
 41     public static Names instance(Context context) {
 42         Names instance = context.get(namesKey);
 43         if (instance == null) {
 44             instance = new Names(context);
 45             context.put(namesKey, instance);
 46         }
 47         return instance;
 48     }
 49 
 50     // operators and punctuation
 51     public final Name asterisk;
 52     public final Name comma;
 53     public final Name empty;
 54     public final Name hyphen;
 55     public final Name one;
 56     public final Name slash;
 57 
 58     // keywords
 59     public final Name _class;
 60     public final Name _default;
 61     public final Name _super;
 62     public final Name _this;
 63     public final Name var;
 64     public final Name exports;
 65     public final Name opens;
 66     public final Name module;
 67     public final Name provides;
 68     public final Name requires;
 69     public final Name to;
 70     public final Name transitive;
 71     public final Name uses;
 72     public final Name open;
 73     public final Name with;
 74     public final Name yield;
 75 
 76     // field and method names
 77     public final Name _name;
 78     public final Name addSuppressed;
 79     public final Name any;
 80     public final Name append;
 81     public final Name clinit;
 82     public final Name clone;
 83     public final Name close;
 84     public final Name deserializeLambda;
 85     public final Name desiredAssertionStatus;
 86     public final Name equals;
 87     public final Name error;
 88     public final Name finalize;
 89     public final Name forRemoval;
 90     public final Name reflective;
 91     public final Name getClass;
 92     public final Name hasNext;
 93     public final Name hashCode;
 94     public final Name init;
 95     public final Name iterator;
 96     public final Name length;
 97     public final Name newIdentity;
 98     public final Name next;
 99     public final Name ordinal;
100     public final Name provider;
101     public final Name serialVersionUID;
102     public final Name toString;
103     public final Name value;
104     public final Name primitive;
105     public final Name valueOf;
106     public final Name values;
107     public final Name readResolve;
108     public final Name readObject;
109 
110     // class names
111     public final Name java_io_Serializable;
112     public final Name java_lang_Class;
113     public final Name java_lang_Cloneable;
114     public final Name java_lang_Enum;
115     public final Name java_lang_Object;
116     public final Name java_lang_System;
117     public final Name __primitive__;
118     public final Name java_lang___primitive__;
119     public final Name java_lang_IdentityObject;
120 
121     // names of builtin classes
122     public final Name Array;
123     public final Name Bound;
124     public final Name Method;
125 
126     // package names
127     public final Name java;
128     public final Name java_lang;
129 
130     // module names
131     public final Name java_base;
132     public final Name jdk_unsupported;
133 
134     // attribute names
135     public final Name Annotation;
136     public final Name AnnotationDefault;
137     public final Name BootstrapMethods;
138     public final Name Bridge;
139     public final Name CharacterRangeTable;
140     public final Name Code;
141     public final Name CompilationID;
142     public final Name ConstantValue;
143     public final Name Deprecated;
144     public final Name EnclosingMethod;
145     public final Name Enum;
146     public final Name Exceptions;
147     public final Name InnerClasses;
148     public final Name JavaFlags;
149     public final Name LineNumberTable;
150     public final Name LocalVariableTable;
151     public final Name LocalVariableTypeTable;
152     public final Name MethodParameters;
153     public final Name Module;
154     public final Name ModuleResolution;
155     public final Name NestHost;
156     public final Name NestMembers;
157     public final Name Record;
158     public final Name RuntimeInvisibleAnnotations;
159     public final Name RuntimeInvisibleParameterAnnotations;
160     public final Name RuntimeInvisibleTypeAnnotations;
161     public final Name RuntimeVisibleAnnotations;
162     public final Name RuntimeVisibleParameterAnnotations;
163     public final Name RuntimeVisibleTypeAnnotations;
164     public final Name Signature;
165     public final Name SourceFile;
166     public final Name SourceID;
167     public final Name StackMap;
168     public final Name StackMapTable;
169     public final Name Synthetic;
170     public final Name Value;
171     public final Name Varargs;
172     public final Name PermittedSubclasses;
173 
174     // members of java.lang.annotation.ElementType
175     public final Name ANNOTATION_TYPE;
176     public final Name CONSTRUCTOR;
177     public final Name FIELD;
178     public final Name LOCAL_VARIABLE;
179     public final Name METHOD;
180     public final Name MODULE;
181     public final Name PACKAGE;
182     public final Name PARAMETER;
183     public final Name TYPE;
184     public final Name TYPE_PARAMETER;
185     public final Name TYPE_USE;
186     public final Name RECORD_COMPONENT;
187 
188     // members of java.lang.annotation.RetentionPolicy
189     public final Name CLASS;
190     public final Name RUNTIME;
191     public final Name SOURCE;
192 
193     // other identifiers
194     public final Name T;
195     public final Name ex;
196     public final Name module_info;
197     public final Name package_info;
198     public final Name requireNonNull;
199 
200     // lambda-related
201     public final Name lambda;
202     public final Name metafactory;
203     public final Name altMetafactory;
204     public final Name dollarThis;
205 
206     // string concat
207     public final Name makeConcat;
208     public final Name makeConcatWithConstants;
209 
210     // values
211     public final Name dollarValue;
212     public final Name ref;
213     public final Name val;
214 
215 
216     // record related
217     // members of java.lang.runtime.ObjectMethods
218     public final Name bootstrap;
219 
220     public final Name record;
221     public final Name non;
222 
223     // serialization members, used by records too
224     public final Name serialPersistentFields;
225     public final Name writeObject;
226     public final Name writeReplace;
227     public final Name readObjectNoData;
228 
229     // sealed types
230     public final Name permits;
231     public final Name sealed;
232 
233     // pattern switches
234     public final Name typeSwitch;
235     public final Name enumSwitch;
236 
237     public final Name.Table table;
238 
239     public Names(Context context) {
240         Options options = Options.instance(context);
241         table = createTable(options);
242 
243         // operators and punctuation
244         asterisk = fromString("*");
245         comma = fromString(",");
246         empty = fromString("");
247         hyphen = fromString("-");
248         one = fromString("1");
249         slash = fromString("/");
250 
251         // keywords
252         _class = fromString("class");
253         _default = fromString("default");
254         _super = fromString("super");
255         _this = fromString("this");
256         var = fromString("var");
257         exports = fromString("exports");
258         opens = fromString("opens");
259         module = fromString("module");
260         provides = fromString("provides");
261         requires = fromString("requires");
262         to = fromString("to");
263         transitive = fromString("transitive");
264         uses = fromString("uses");
265         open = fromString("open");
266         with = fromString("with");
267         yield = fromString("yield");
268 
269         // field and method names
270         _name = fromString("name");
271         addSuppressed = fromString("addSuppressed");
272         any = fromString("<any>");
273         append = fromString("append");
274         clinit = fromString("<clinit>");
275         clone = fromString("clone");
276         close = fromString("close");
277         deserializeLambda = fromString("$deserializeLambda$");
278         desiredAssertionStatus = fromString("desiredAssertionStatus");
279         equals = fromString("equals");
280         error = fromString("<error>");
281         finalize = fromString("finalize");
282         forRemoval = fromString("forRemoval");
283         reflective = fromString("reflective");
284         getClass = fromString("getClass");
285         hasNext = fromString("hasNext");
286         hashCode = fromString("hashCode");
287         init = fromString("<init>");
288         iterator = fromString("iterator");
289         length = fromString("length");
290         next = fromString("next");
291         newIdentity = fromString("newIdentity");
292         ordinal = fromString("ordinal");
293         provider = fromString("provider");
294         serialVersionUID = fromString("serialVersionUID");
295         toString = fromString("toString");
296         value = fromString("value");
297         primitive = fromString("primitive");
298         valueOf = fromString("valueOf");
299         values = fromString("values");
300         readResolve = fromString("readResolve");
301         readObject = fromString("readObject");
302         dollarThis = fromString("$this");
303 
304         // class names
305         java_io_Serializable = fromString("java.io.Serializable");
306         java_lang_Class = fromString("java.lang.Class");
307         java_lang_Cloneable = fromString("java.lang.Cloneable");
308         java_lang_Enum = fromString("java.lang.Enum");
309         java_lang_Object = fromString("java.lang.Object");
310         java_lang_System = fromString("java.lang.System");
311         __primitive__ = fromString("__primitive__");
312         java_lang___primitive__ = fromString("java.lang.__primitive__");
313         java_lang_IdentityObject = fromString("java.lang.IdentityObject");
314 
315         // names of builtin classes
316         Array = fromString("Array");
317         Bound = fromString("Bound");
318         Method = fromString("Method");
319 
320         // package names
321         java = fromString("java");
322         java_lang = fromString("java.lang");
323 
324         // module names
325         java_base = fromString("java.base");
326         jdk_unsupported = fromString("jdk.unsupported");
327 
328         // attribute names
329         Annotation = fromString("Annotation");
330         AnnotationDefault = fromString("AnnotationDefault");
331         BootstrapMethods = fromString("BootstrapMethods");
332         Bridge = fromString("Bridge");
333         CharacterRangeTable = fromString("CharacterRangeTable");
334         Code = fromString("Code");
335         CompilationID = fromString("CompilationID");
336         ConstantValue = fromString("ConstantValue");
337         Deprecated = fromString("Deprecated");
338         EnclosingMethod = fromString("EnclosingMethod");
339         Enum = fromString("Enum");
340         Exceptions = fromString("Exceptions");
341         InnerClasses = fromString("InnerClasses");
342         LineNumberTable = fromString("LineNumberTable");
343         JavaFlags = fromString("JavaFlags");
344         LocalVariableTable = fromString("LocalVariableTable");
345         LocalVariableTypeTable = fromString("LocalVariableTypeTable");
346         MethodParameters = fromString("MethodParameters");
347         Module = fromString("Module");
348         ModuleResolution = fromString("ModuleResolution");
349         NestHost = fromString("NestHost");
350         NestMembers = fromString("NestMembers");
351         Record = fromString("Record");
352         RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations");
353         RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations");
354         RuntimeInvisibleTypeAnnotations = fromString("RuntimeInvisibleTypeAnnotations");
355         RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations");
356         RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations");
357         RuntimeVisibleTypeAnnotations = fromString("RuntimeVisibleTypeAnnotations");
358         Signature = fromString("Signature");
359         SourceFile = fromString("SourceFile");
360         SourceID = fromString("SourceID");
361         StackMap = fromString("StackMap");
362         StackMapTable = fromString("StackMapTable");
363         Synthetic = fromString("Synthetic");
364         Value = fromString("Value");
365         Varargs = fromString("Varargs");
366         PermittedSubclasses = fromString("PermittedSubclasses");
367 
368         // members of java.lang.annotation.ElementType
369         ANNOTATION_TYPE = fromString("ANNOTATION_TYPE");
370         CONSTRUCTOR = fromString("CONSTRUCTOR");
371         FIELD = fromString("FIELD");
372         LOCAL_VARIABLE = fromString("LOCAL_VARIABLE");
373         METHOD = fromString("METHOD");
374         MODULE = fromString("MODULE");
375         PACKAGE = fromString("PACKAGE");
376         PARAMETER = fromString("PARAMETER");
377         TYPE = fromString("TYPE");
378         TYPE_PARAMETER = fromString("TYPE_PARAMETER");
379         TYPE_USE = fromString("TYPE_USE");
380         RECORD_COMPONENT = fromString("RECORD_COMPONENT");
381 
382         // members of java.lang.annotation.RetentionPolicy
383         CLASS = fromString("CLASS");
384         RUNTIME = fromString("RUNTIME");
385         SOURCE = fromString("SOURCE");
386 
387         // other identifiers
388         T = fromString("T");
389         ex = fromString("ex");
390         module_info = fromString("module-info");
391         package_info = fromString("package-info");
392         requireNonNull = fromString("requireNonNull");
393 
394         //lambda-related
395         lambda = fromString("lambda$");
396         metafactory = fromString("metafactory");
397         altMetafactory = fromString("altMetafactory");
398 
399         // string concat
400         makeConcat = fromString("makeConcat");
401         makeConcatWithConstants = fromString("makeConcatWithConstants");
402 
403         // primitive classes
404         dollarValue = fromString("$value");
405         ref = fromString("ref");
406         val = fromString("val");
407 
408         bootstrap = fromString("bootstrap");
409         record = fromString("record");
410         non = fromString("non");
411 
412         serialPersistentFields = fromString("serialPersistentFields");
413         writeObject = fromString("writeObject");
414         writeReplace = fromString("writeReplace");
415         readObjectNoData = fromString("readObjectNoData");
416 
417         // sealed types
418         permits = fromString("permits");
419         sealed = fromString("sealed");
420 
421         // pattern switches
422         typeSwitch = fromString("typeSwitch");
423         enumSwitch = fromString("enumSwitch");
424     }
425 
426     protected Name.Table createTable(Options options) {
427         boolean useUnsharedTable = options.isSet("useUnsharedTable");
428         if (useUnsharedTable)
429             return UnsharedNameTable.create(this);
430         else
431             return SharedNameTable.create(this);
432     }
433 
434     public void dispose() {
435         table.dispose();
436     }
437 
438     public Name fromChars(char[] cs, int start, int len) {
439         return table.fromChars(cs, start, len);
440     }
441 
442     public Name fromString(String s) {
443         return table.fromString(s);
444     }
445 
446     public Name fromUtf(byte[] cs) {
447         return table.fromUtf(cs);
448     }
449 
450     public Name fromUtf(byte[] cs, int start, int len) {
451         return table.fromUtf(cs, start, len);
452     }
453 }