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 java_se;
131     public final Name jdk_unsupported;
132 
133     // attribute names
134     public final Name Annotation;
135     public final Name AnnotationDefault;
136     public final Name BootstrapMethods;
137     public final Name Bridge;
138     public final Name CharacterRangeTable;
139     public final Name Code;
140     public final Name CompilationID;
141     public final Name ConstantValue;
142     public final Name Deprecated;
143     public final Name EnclosingMethod;
144     public final Name Enum;
145     public final Name Exceptions;
146     public final Name InnerClasses;
147     public final Name LineNumberTable;
148     public final Name LocalVariableTable;
149     public final Name LocalVariableTypeTable;
150     public final Name MethodParameters;
151     public final Name Module;
152     public final Name ModuleResolution;
153     public final Name NestHost;
154     public final Name NestMembers;
155     public final Name Record;
156     public final Name RuntimeInvisibleAnnotations;
157     public final Name RuntimeInvisibleParameterAnnotations;
158     public final Name RuntimeInvisibleTypeAnnotations;
159     public final Name RuntimeVisibleAnnotations;
160     public final Name RuntimeVisibleParameterAnnotations;
161     public final Name RuntimeVisibleTypeAnnotations;
162     public final Name Signature;
163     public final Name SourceFile;
164     public final Name SourceID;
165     public final Name StackMap;
166     public final Name StackMapTable;
167     public final Name Synthetic;
168     public final Name Value;
169     public final Name Varargs;
170     public final Name PermittedSubclasses;
171 
172     // members of java.lang.annotation.ElementType
173     public final Name ANNOTATION_TYPE;
174     public final Name CONSTRUCTOR;
175     public final Name FIELD;
176     public final Name LOCAL_VARIABLE;
177     public final Name METHOD;
178     public final Name MODULE;
179     public final Name PACKAGE;
180     public final Name PARAMETER;
181     public final Name TYPE;
182     public final Name TYPE_PARAMETER;
183     public final Name TYPE_USE;
184     public final Name RECORD_COMPONENT;
185 
186     // members of java.lang.annotation.RetentionPolicy
187     public final Name CLASS;
188     public final Name RUNTIME;
189     public final Name SOURCE;
190 
191     // other identifiers
192     public final Name T;
193     public final Name ex;
194     public final Name module_info;
195     public final Name package_info;
196     public final Name requireNonNull;
197     public final Name main;
198 
199     // lambda-related
200     public final Name lambda;
201     public final Name metafactory;
202     public final Name altMetafactory;
203     public final Name dollarThis;
204 
205     // string concat
206     public final Name makeConcat;
207     public final Name makeConcatWithConstants;
208 
209     // record related
210     // members of java.lang.runtime.ObjectMethods
211     public final Name bootstrap;
212 
213     public final Name record;
214     public final Name non;
215 
216     // serialization members, used by records too
217     public final Name serialPersistentFields;
218     public final Name writeObject;
219     public final Name writeReplace;
220     public final Name readObjectNoData;
221 
222     // sealed types
223     public final Name permits;
224     public final Name sealed;
225 
226     // pattern switches
227     public final Name typeSwitch;
228     public final Name enumSwitch;
229     public final Name enumConstant;
230 
231     // code reflection
232     public final Name jdk_incubator_code;
233     public final Name quoted;
234     public final Name quotable;
235 
236     public final Name.Table table;
237 
238     @SuppressWarnings("this-escape")
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         _super = fromString("super");
254         _this = fromString("this");
255         var = fromString("var");
256         exports = fromString("exports");
257         opens = fromString("opens");
258         module = fromString("module");
259         provides = fromString("provides");
260         requires = fromString("requires");
261         to = fromString("to");
262         transitive = fromString("transitive");
263         uses = fromString("uses");
264         open = fromString("open");
265         underscore = fromString("_");
266         when = fromString("when");
267         with = fromString("with");
268         yield = fromString("yield");
269 
270         // field and method names
271         _name = fromString("name");
272         addSuppressed = fromString("addSuppressed");
273         any = fromString("<any>");
274         append = fromString("append");
275         clinit = fromString("<clinit>");
276         clone = fromString("clone");
277         close = fromString("close");
278         deserializeLambda = fromString("$deserializeLambda$");
279         desiredAssertionStatus = fromString("desiredAssertionStatus");
280         equals = fromString("equals");
281         error = fromString("<error>");
282         finalize = fromString("finalize");
283         forRemoval = fromString("forRemoval");
284         reflective = fromString("reflective");
285         getClass = fromString("getClass");
286         hasNext = fromString("hasNext");
287         hashCode = fromString("hashCode");
288         init = fromString("<init>");
289         invoke = fromString("invoke");
290         iterator = fromString("iterator");
291         length = fromString("length");
292         next = fromString("next");
293         of = fromString("of");
294         ordinal = fromString("ordinal");
295         provider = fromString("provider");
296         serialVersionUID = fromString("serialVersionUID");
297         toString = fromString("toString");
298         value = fromString("value");
299         valueOf = fromString("valueOf");
300         values = fromString("values");
301         readResolve = fromString("readResolve");
302         readObject = fromString("readObject");
303         dollarThis = fromString("$this");
304 
305         // class names
306         java_io_Serializable = fromString("java.io.Serializable");
307         java_lang_Class = fromString("java.lang.Class");
308         java_lang_Cloneable = fromString("java.lang.Cloneable");
309         java_lang_Enum = fromString("java.lang.Enum");
310         java_lang_Object = fromString("java.lang.Object");
311 
312         // names of builtin classes
313         Array = fromString("Array");
314         Bound = fromString("Bound");
315         Method = fromString("Method");
316 
317         // package names
318         java = fromString("java");
319         java_lang = fromString("java.lang");
320         jdk_internal_javac = fromString("jdk.internal.javac");
321 
322         // module names
323         java_base = fromString("java.base");
324         java_se = fromString("java.se");
325         jdk_unsupported = fromString("jdk.unsupported");
326 
327         // attribute names
328         Annotation = fromString("Annotation");
329         AnnotationDefault = fromString("AnnotationDefault");
330         BootstrapMethods = fromString("BootstrapMethods");
331         Bridge = fromString("Bridge");
332         CharacterRangeTable = fromString("CharacterRangeTable");
333         Code = fromString("Code");
334         CompilationID = fromString("CompilationID");
335         ConstantValue = fromString("ConstantValue");
336         Deprecated = fromString("Deprecated");
337         EnclosingMethod = fromString("EnclosingMethod");
338         Enum = fromString("Enum");
339         Exceptions = fromString("Exceptions");
340         InnerClasses = fromString("InnerClasses");
341         LineNumberTable = fromString("LineNumberTable");
342         LocalVariableTable = fromString("LocalVariableTable");
343         LocalVariableTypeTable = fromString("LocalVariableTypeTable");
344         MethodParameters = fromString("MethodParameters");
345         Module = fromString("Module");
346         ModuleResolution = fromString("ModuleResolution");
347         NestHost = fromString("NestHost");
348         NestMembers = fromString("NestMembers");
349         Record = fromString("Record");
350         RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations");
351         RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations");
352         RuntimeInvisibleTypeAnnotations = fromString("RuntimeInvisibleTypeAnnotations");
353         RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations");
354         RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations");
355         RuntimeVisibleTypeAnnotations = fromString("RuntimeVisibleTypeAnnotations");
356         Signature = fromString("Signature");
357         SourceFile = fromString("SourceFile");
358         SourceID = fromString("SourceID");
359         StackMap = fromString("StackMap");
360         StackMapTable = fromString("StackMapTable");
361         Synthetic = fromString("Synthetic");
362         Value = fromString("Value");
363         Varargs = fromString("Varargs");
364         PermittedSubclasses = fromString("PermittedSubclasses");
365 
366         // members of java.lang.annotation.ElementType
367         ANNOTATION_TYPE = fromString("ANNOTATION_TYPE");
368         CONSTRUCTOR = fromString("CONSTRUCTOR");
369         FIELD = fromString("FIELD");
370         LOCAL_VARIABLE = fromString("LOCAL_VARIABLE");
371         METHOD = fromString("METHOD");
372         MODULE = fromString("MODULE");
373         PACKAGE = fromString("PACKAGE");
374         PARAMETER = fromString("PARAMETER");
375         TYPE = fromString("TYPE");
376         TYPE_PARAMETER = fromString("TYPE_PARAMETER");
377         TYPE_USE = fromString("TYPE_USE");
378         RECORD_COMPONENT = fromString("RECORD_COMPONENT");
379 
380         // members of java.lang.annotation.RetentionPolicy
381         CLASS = fromString("CLASS");
382         RUNTIME = fromString("RUNTIME");
383         SOURCE = fromString("SOURCE");
384 
385         // other identifiers
386         T = fromString("T");
387         ex = fromString("ex");
388         module_info = fromString("module-info");
389         package_info = fromString("package-info");
390         requireNonNull = fromString("requireNonNull");
391         main = fromString("main");
392 
393         //lambda-related
394         lambda = fromString("lambda$");
395         metafactory = fromString("metafactory");
396         altMetafactory = fromString("altMetafactory");
397 
398         // string concat
399         makeConcat = fromString("makeConcat");
400         makeConcatWithConstants = fromString("makeConcatWithConstants");
401 
402         bootstrap = fromString("bootstrap");
403         record = fromString("record");
404         non = fromString("non");
405 
406         serialPersistentFields = fromString("serialPersistentFields");
407         writeObject = fromString("writeObject");
408         writeReplace = fromString("writeReplace");
409         readObjectNoData = fromString("readObjectNoData");
410 
411         // sealed types
412         permits = fromString("permits");
413         sealed = fromString("sealed");
414 
415 
416         // pattern switches
417         typeSwitch = fromString("typeSwitch");
418         enumSwitch = fromString("enumSwitch");
419         enumConstant = fromString("enumConstant");
420 
421         // code reflection
422         jdk_incubator_code = fromString("jdk.incubator.code");
423         quoted = fromString("Quoted");
424         quotable = fromString("Quotable");
425     }
426 
427     protected Name.Table createTable(Options options) {
428         boolean useUnsharedTable = options.isSet("useUnsharedTable");
429         if (useUnsharedTable)
430             return newUnsharedNameTable();
431         boolean useSharedTable = options.isSet("useSharedTable");
432         if (useSharedTable)
433             return newSharedNameTable();
434         boolean internStringTable = options.isSet("internStringTable");
435         return newStringNameTable(internStringTable);
436     }
437 
438     public StringNameTable newStringNameTable(boolean intern) {
439         return StringNameTable.create(this, intern);
440     }
441 
442     public SharedNameTable newSharedNameTable() {
443         return SharedNameTable.create(this);
444     }
445 
446     public UnsharedNameTable newUnsharedNameTable() {
447         return UnsharedNameTable.create(this);
448     }
449 
450     public void dispose() {
451         table.dispose();
452     }
453 
454     public Name fromChars(char[] cs, int start, int len) {
455         return table.fromChars(cs, start, len);
456     }
457 
458     public Name fromString(String s) {
459         return table.fromString(s);
460     }
461 
462     public Name fromUtf(byte[] cs) throws InvalidUtfException {
463         return table.fromUtf(cs);
464     }
465 
466     public Name fromUtf(byte[] cs, int start, int len, Convert.Validation validation) throws InvalidUtfException {
467         return table.fromUtf(cs, start, len, validation);
468     }
469 
470     public Name fromUtfLax(byte[] cs, int start, int len) {
471         try {
472             return table.fromUtf(cs, start, len, Convert.Validation.NONE);
473         } catch (InvalidUtfException e) {
474             throw new AssertionError(e);
475         }
476     }
477 }