1 /*
  2  * Copyright (c) 1999, 2024, 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 _super;
 61     public final Name _this;
 62     public final Name var;
 63     public final Name exports;
 64     public final Name opens;
 65     public final Name module;
 66     public final Name provides;
 67     public final Name requires;
 68     public final Name to;
 69     public final Name transitive;
 70     public final Name uses;
 71     public final Name open;
 72     public final Name underscore;
 73     public final Name when;
 74     public final Name with;
 75     public final Name yield;
 76 
 77     // field and method names
 78     public final Name _name;
 79     public final Name addSuppressed;
 80     public final Name any;
 81     public final Name append;
 82     public final Name clinit;
 83     public final Name clone;
 84     public final Name close;
 85     public final Name deserializeLambda;
 86     public final Name desiredAssertionStatus;
 87     public final Name equals;
 88     public final Name error;
 89     public final Name finalize;
 90     public final Name forRemoval;
 91     public final Name reflective;
 92     public final Name getClass;
 93     public final Name hasNext;
 94     public final Name hashCode;
 95     public final Name init;
 96     public final Name invoke;
 97     public final Name iterator;
 98     public final Name length;
 99     public final Name next;
100     public final Name of;
101     public final Name ordinal;
102     public final Name provider;
103     public final Name serialVersionUID;
104     public final Name toString;
105     public final Name value;
106     public final Name valueOf;
107     public final Name values;
108     public final Name readResolve;
109     public final Name readObject;
110 
111     // class names
112     public final Name java_io_Serializable;
113     public final Name java_lang_Class;
114     public final Name java_lang_Cloneable;
115     public final Name java_lang_Enum;
116     public final Name java_lang_Object;
117 
118     // names of builtin classes
119     public final Name Array;
120     public final Name Bound;
121     public final Name Method;
122 
123     // package names
124     public final Name java;
125     public final Name java_lang;
126     public final Name jdk_internal_javac;
127 
128     // module names
129     public final Name java_base;
130     public final Name jdk_unsupported;
131 
132     // attribute names
133     public final Name Annotation;
134     public final Name AnnotationDefault;
135     public final Name BootstrapMethods;
136     public final Name Bridge;
137     public final Name CharacterRangeTable;
138     public final Name Code;
139     public final Name CompilationID;
140     public final Name ConstantValue;
141     public final Name Deprecated;
142     public final Name EnclosingMethod;
143     public final Name Enum;
144     public final Name Exceptions;
145     public final Name InnerClasses;
146     public final Name LineNumberTable;
147     public final Name LocalVariableTable;
148     public final Name LocalVariableTypeTable;
149     public final Name MethodParameters;
150     public final Name Module;
151     public final Name ModuleResolution;
152     public final Name NestHost;
153     public final Name NestMembers;
154     public final Name Record;
155     public final Name RuntimeInvisibleAnnotations;
156     public final Name RuntimeInvisibleParameterAnnotations;
157     public final Name RuntimeInvisibleTypeAnnotations;
158     public final Name RuntimeVisibleAnnotations;
159     public final Name RuntimeVisibleParameterAnnotations;
160     public final Name RuntimeVisibleTypeAnnotations;
161     public final Name Signature;
162     public final Name SourceFile;
163     public final Name SourceID;
164     public final Name StackMap;
165     public final Name StackMapTable;
166     public final Name Synthetic;
167     public final Name Value;
168     public final Name Varargs;
169     public final Name PermittedSubclasses;
170 
171     // members of java.lang.annotation.ElementType
172     public final Name ANNOTATION_TYPE;
173     public final Name CONSTRUCTOR;
174     public final Name FIELD;
175     public final Name LOCAL_VARIABLE;
176     public final Name METHOD;
177     public final Name MODULE;
178     public final Name PACKAGE;
179     public final Name PARAMETER;
180     public final Name TYPE;
181     public final Name TYPE_PARAMETER;
182     public final Name TYPE_USE;
183     public final Name RECORD_COMPONENT;
184 
185     // members of java.lang.annotation.RetentionPolicy
186     public final Name CLASS;
187     public final Name RUNTIME;
188     public final Name SOURCE;
189 
190     // other identifiers
191     public final Name T;
192     public final Name ex;
193     public final Name module_info;
194     public final Name package_info;
195     public final Name requireNonNull;
196     public final Name main;
197 
198     // lambda-related
199     public final Name lambda;
200     public final Name metafactory;
201     public final Name altMetafactory;
202     public final Name dollarThis;
203 
204     // string concat
205     public final Name makeConcat;
206     public final Name makeConcatWithConstants;
207 
208     // record related
209     // members of java.lang.runtime.ObjectMethods
210     public final Name bootstrap;
211 
212     public final Name record;
213     public final Name non;
214 
215     // serialization members, used by records too
216     public final Name serialPersistentFields;
217     public final Name writeObject;
218     public final Name writeReplace;
219     public final Name readObjectNoData;
220 
221     // sealed types
222     public final Name permits;
223     public final Name sealed;
224 
225     // pattern switches
226     public final Name typeSwitch;
227     public final Name enumSwitch;
228     public final Name enumConstant;
229 
230     // code reflection
231     public final Name jdk_incubator_code;
232     public final Name quoted;
233     public final Name quotable;
234 
235     public final Name.Table table;
236 
237     @SuppressWarnings("this-escape")
238     public Names(Context context) {
239         Options options = Options.instance(context);
240         table = createTable(options);
241 
242         // operators and punctuation
243         asterisk = fromString("*");
244         comma = fromString(",");
245         empty = fromString("");
246         hyphen = fromString("-");
247         one = fromString("1");
248         slash = fromString("/");
249 
250         // keywords
251         _class = fromString("class");
252         _super = fromString("super");
253         _this = fromString("this");
254         var = fromString("var");
255         exports = fromString("exports");
256         opens = fromString("opens");
257         module = fromString("module");
258         provides = fromString("provides");
259         requires = fromString("requires");
260         to = fromString("to");
261         transitive = fromString("transitive");
262         uses = fromString("uses");
263         open = fromString("open");
264         underscore = fromString("_");
265         when = fromString("when");
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         invoke = fromString("invoke");
289         iterator = fromString("iterator");
290         length = fromString("length");
291         next = fromString("next");
292         of = fromString("of");
293         ordinal = fromString("ordinal");
294         provider = fromString("provider");
295         serialVersionUID = fromString("serialVersionUID");
296         toString = fromString("toString");
297         value = fromString("value");
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 
311         // names of builtin classes
312         Array = fromString("Array");
313         Bound = fromString("Bound");
314         Method = fromString("Method");
315 
316         // package names
317         java = fromString("java");
318         java_lang = fromString("java.lang");
319         jdk_internal_javac = fromString("jdk.internal.javac");
320 
321         // module names
322         java_base = fromString("java.base");
323         jdk_unsupported = fromString("jdk.unsupported");
324 
325         // attribute names
326         Annotation = fromString("Annotation");
327         AnnotationDefault = fromString("AnnotationDefault");
328         BootstrapMethods = fromString("BootstrapMethods");
329         Bridge = fromString("Bridge");
330         CharacterRangeTable = fromString("CharacterRangeTable");
331         Code = fromString("Code");
332         CompilationID = fromString("CompilationID");
333         ConstantValue = fromString("ConstantValue");
334         Deprecated = fromString("Deprecated");
335         EnclosingMethod = fromString("EnclosingMethod");
336         Enum = fromString("Enum");
337         Exceptions = fromString("Exceptions");
338         InnerClasses = fromString("InnerClasses");
339         LineNumberTable = fromString("LineNumberTable");
340         LocalVariableTable = fromString("LocalVariableTable");
341         LocalVariableTypeTable = fromString("LocalVariableTypeTable");
342         MethodParameters = fromString("MethodParameters");
343         Module = fromString("Module");
344         ModuleResolution = fromString("ModuleResolution");
345         NestHost = fromString("NestHost");
346         NestMembers = fromString("NestMembers");
347         Record = fromString("Record");
348         RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations");
349         RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations");
350         RuntimeInvisibleTypeAnnotations = fromString("RuntimeInvisibleTypeAnnotations");
351         RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations");
352         RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations");
353         RuntimeVisibleTypeAnnotations = fromString("RuntimeVisibleTypeAnnotations");
354         Signature = fromString("Signature");
355         SourceFile = fromString("SourceFile");
356         SourceID = fromString("SourceID");
357         StackMap = fromString("StackMap");
358         StackMapTable = fromString("StackMapTable");
359         Synthetic = fromString("Synthetic");
360         Value = fromString("Value");
361         Varargs = fromString("Varargs");
362         PermittedSubclasses = fromString("PermittedSubclasses");
363 
364         // members of java.lang.annotation.ElementType
365         ANNOTATION_TYPE = fromString("ANNOTATION_TYPE");
366         CONSTRUCTOR = fromString("CONSTRUCTOR");
367         FIELD = fromString("FIELD");
368         LOCAL_VARIABLE = fromString("LOCAL_VARIABLE");
369         METHOD = fromString("METHOD");
370         MODULE = fromString("MODULE");
371         PACKAGE = fromString("PACKAGE");
372         PARAMETER = fromString("PARAMETER");
373         TYPE = fromString("TYPE");
374         TYPE_PARAMETER = fromString("TYPE_PARAMETER");
375         TYPE_USE = fromString("TYPE_USE");
376         RECORD_COMPONENT = fromString("RECORD_COMPONENT");
377 
378         // members of java.lang.annotation.RetentionPolicy
379         CLASS = fromString("CLASS");
380         RUNTIME = fromString("RUNTIME");
381         SOURCE = fromString("SOURCE");
382 
383         // other identifiers
384         T = fromString("T");
385         ex = fromString("ex");
386         module_info = fromString("module-info");
387         package_info = fromString("package-info");
388         requireNonNull = fromString("requireNonNull");
389         main = fromString("main");
390 
391         //lambda-related
392         lambda = fromString("lambda$");
393         metafactory = fromString("metafactory");
394         altMetafactory = fromString("altMetafactory");
395 
396         // string concat
397         makeConcat = fromString("makeConcat");
398         makeConcatWithConstants = fromString("makeConcatWithConstants");
399 
400         bootstrap = fromString("bootstrap");
401         record = fromString("record");
402         non = fromString("non");
403 
404         serialPersistentFields = fromString("serialPersistentFields");
405         writeObject = fromString("writeObject");
406         writeReplace = fromString("writeReplace");
407         readObjectNoData = fromString("readObjectNoData");
408 
409         // sealed types
410         permits = fromString("permits");
411         sealed = fromString("sealed");
412 
413 
414         // pattern switches
415         typeSwitch = fromString("typeSwitch");
416         enumSwitch = fromString("enumSwitch");
417         enumConstant = fromString("enumConstant");
418 
419         // code reflection
420         jdk_incubator_code = fromString("jdk.incubator.code");
421         quoted = fromString("Quoted");
422         quotable = fromString("Quotable");
423     }
424 
425     protected Name.Table createTable(Options options) {
426         boolean useUnsharedTable = options.isSet("useUnsharedTable");
427         if (useUnsharedTable)
428             return newUnsharedNameTable();
429         boolean useSharedTable = options.isSet("useSharedTable");
430         if (useSharedTable)
431             return newSharedNameTable();
432         boolean internStringTable = options.isSet("internStringTable");
433         return newStringNameTable(internStringTable);
434     }
435 
436     public StringNameTable newStringNameTable(boolean intern) {
437         return StringNameTable.create(this, intern);
438     }
439 
440     public SharedNameTable newSharedNameTable() {
441         return SharedNameTable.create(this);
442     }
443 
444     public UnsharedNameTable newUnsharedNameTable() {
445         return UnsharedNameTable.create(this);
446     }
447 
448     public void dispose() {
449         table.dispose();
450     }
451 
452     public Name fromChars(char[] cs, int start, int len) {
453         return table.fromChars(cs, start, len);
454     }
455 
456     public Name fromString(String s) {
457         return table.fromString(s);
458     }
459 
460     public Name fromUtf(byte[] cs) throws InvalidUtfException {
461         return table.fromUtf(cs);
462     }
463 
464     public Name fromUtf(byte[] cs, int start, int len, Convert.Validation validation) throws InvalidUtfException {
465         return table.fromUtf(cs, start, len, validation);
466     }
467 
468     public Name fromUtfLax(byte[] cs, int start, int len) {
469         try {
470             return table.fromUtf(cs, start, len, Convert.Validation.NONE);
471         } catch (InvalidUtfException e) {
472             throw new AssertionError(e);
473         }
474     }
475 }