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     public final Name.Table table;
232 
233     @SuppressWarnings("this-escape")
234     public Names(Context context) {
235         Options options = Options.instance(context);
236         table = createTable(options);
237 
238         // operators and punctuation
239         asterisk = fromString("*");
240         comma = fromString(",");
241         empty = fromString("");
242         hyphen = fromString("-");
243         one = fromString("1");
244         slash = fromString("/");
245 
246         // keywords
247         _class = fromString("class");
248         _super = fromString("super");
249         _this = fromString("this");
250         var = fromString("var");
251         exports = fromString("exports");
252         opens = fromString("opens");
253         module = fromString("module");
254         provides = fromString("provides");
255         requires = fromString("requires");
256         to = fromString("to");
257         transitive = fromString("transitive");
258         uses = fromString("uses");
259         open = fromString("open");
260         underscore = fromString("_");
261         when = fromString("when");
262         with = fromString("with");
263         yield = fromString("yield");
264 
265         // field and method names
266         _name = fromString("name");
267         addSuppressed = fromString("addSuppressed");
268         any = fromString("<any>");
269         append = fromString("append");
270         clinit = fromString("<clinit>");
271         clone = fromString("clone");
272         close = fromString("close");
273         deserializeLambda = fromString("$deserializeLambda$");
274         desiredAssertionStatus = fromString("desiredAssertionStatus");
275         equals = fromString("equals");
276         error = fromString("<error>");
277         finalize = fromString("finalize");
278         forRemoval = fromString("forRemoval");
279         reflective = fromString("reflective");
280         getClass = fromString("getClass");
281         hasNext = fromString("hasNext");
282         hashCode = fromString("hashCode");
283         init = fromString("<init>");
284         invoke = fromString("invoke");
285         iterator = fromString("iterator");
286         length = fromString("length");
287         next = fromString("next");
288         of = fromString("of");
289         ordinal = fromString("ordinal");
290         provider = fromString("provider");
291         serialVersionUID = fromString("serialVersionUID");
292         toString = fromString("toString");
293         value = fromString("value");
294         valueOf = fromString("valueOf");
295         values = fromString("values");
296         readResolve = fromString("readResolve");
297         readObject = fromString("readObject");
298         dollarThis = fromString("$this");
299 
300         // class names
301         java_io_Serializable = fromString("java.io.Serializable");
302         java_lang_Class = fromString("java.lang.Class");
303         java_lang_Cloneable = fromString("java.lang.Cloneable");
304         java_lang_Enum = fromString("java.lang.Enum");
305         java_lang_Object = fromString("java.lang.Object");
306 
307         // names of builtin classes
308         Array = fromString("Array");
309         Bound = fromString("Bound");
310         Method = fromString("Method");
311 
312         // package names
313         java = fromString("java");
314         java_lang = fromString("java.lang");
315         jdk_internal_javac = fromString("jdk.internal.javac");
316 
317         // module names
318         java_base = fromString("java.base");
319         java_se = fromString("java.se");
320         jdk_unsupported = fromString("jdk.unsupported");
321 
322         // attribute names
323         Annotation = fromString("Annotation");
324         AnnotationDefault = fromString("AnnotationDefault");
325         BootstrapMethods = fromString("BootstrapMethods");
326         Bridge = fromString("Bridge");
327         CharacterRangeTable = fromString("CharacterRangeTable");
328         Code = fromString("Code");
329         CompilationID = fromString("CompilationID");
330         ConstantValue = fromString("ConstantValue");
331         Deprecated = fromString("Deprecated");
332         EnclosingMethod = fromString("EnclosingMethod");
333         Enum = fromString("Enum");
334         Exceptions = fromString("Exceptions");
335         InnerClasses = fromString("InnerClasses");
336         LineNumberTable = fromString("LineNumberTable");
337         LocalVariableTable = fromString("LocalVariableTable");
338         LocalVariableTypeTable = fromString("LocalVariableTypeTable");
339         MethodParameters = fromString("MethodParameters");
340         Module = fromString("Module");
341         ModuleResolution = fromString("ModuleResolution");
342         NestHost = fromString("NestHost");
343         NestMembers = fromString("NestMembers");
344         Record = fromString("Record");
345         RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations");
346         RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations");
347         RuntimeInvisibleTypeAnnotations = fromString("RuntimeInvisibleTypeAnnotations");
348         RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations");
349         RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations");
350         RuntimeVisibleTypeAnnotations = fromString("RuntimeVisibleTypeAnnotations");
351         Signature = fromString("Signature");
352         SourceFile = fromString("SourceFile");
353         SourceID = fromString("SourceID");
354         StackMap = fromString("StackMap");
355         StackMapTable = fromString("StackMapTable");
356         Synthetic = fromString("Synthetic");
357         Value = fromString("Value");
358         Varargs = fromString("Varargs");
359         PermittedSubclasses = fromString("PermittedSubclasses");
360 
361         // members of java.lang.annotation.ElementType
362         ANNOTATION_TYPE = fromString("ANNOTATION_TYPE");
363         CONSTRUCTOR = fromString("CONSTRUCTOR");
364         FIELD = fromString("FIELD");
365         LOCAL_VARIABLE = fromString("LOCAL_VARIABLE");
366         METHOD = fromString("METHOD");
367         MODULE = fromString("MODULE");
368         PACKAGE = fromString("PACKAGE");
369         PARAMETER = fromString("PARAMETER");
370         TYPE = fromString("TYPE");
371         TYPE_PARAMETER = fromString("TYPE_PARAMETER");
372         TYPE_USE = fromString("TYPE_USE");
373         RECORD_COMPONENT = fromString("RECORD_COMPONENT");
374 
375         // members of java.lang.annotation.RetentionPolicy
376         CLASS = fromString("CLASS");
377         RUNTIME = fromString("RUNTIME");
378         SOURCE = fromString("SOURCE");
379 
380         // other identifiers
381         T = fromString("T");
382         ex = fromString("ex");
383         module_info = fromString("module-info");
384         package_info = fromString("package-info");
385         requireNonNull = fromString("requireNonNull");
386         main = fromString("main");
387 
388         //lambda-related
389         lambda = fromString("lambda$");
390         metafactory = fromString("metafactory");
391         altMetafactory = fromString("altMetafactory");
392 
393         // string concat
394         makeConcat = fromString("makeConcat");
395         makeConcatWithConstants = fromString("makeConcatWithConstants");
396 
397         bootstrap = fromString("bootstrap");
398         record = fromString("record");
399         non = fromString("non");
400 
401         serialPersistentFields = fromString("serialPersistentFields");
402         writeObject = fromString("writeObject");
403         writeReplace = fromString("writeReplace");
404         readObjectNoData = fromString("readObjectNoData");
405 
406         // sealed types
407         permits = fromString("permits");
408         sealed = fromString("sealed");
409 
410 
411         // pattern switches
412         typeSwitch = fromString("typeSwitch");
413         enumSwitch = fromString("enumSwitch");
414         enumConstant = fromString("enumConstant");
415     }
416 
417     protected Name.Table createTable(Options options) {
418         boolean useUnsharedTable = options.isSet("useUnsharedTable");
419         if (useUnsharedTable)
420             return newUnsharedNameTable();
421         boolean useSharedTable = options.isSet("useSharedTable");
422         if (useSharedTable)
423             return newSharedNameTable();
424         boolean internStringTable = options.isSet("internStringTable");
425         return newStringNameTable(internStringTable);
426     }
427 
428     public StringNameTable newStringNameTable(boolean intern) {
429         return StringNameTable.create(this, intern);
430     }
431 
432     public SharedNameTable newSharedNameTable() {
433         return SharedNameTable.create(this);
434     }
435 
436     public UnsharedNameTable newUnsharedNameTable() {
437         return UnsharedNameTable.create(this);
438     }
439 
440     public void dispose() {
441         table.dispose();
442     }
443 
444     public Name fromChars(char[] cs, int start, int len) {
445         return table.fromChars(cs, start, len);
446     }
447 
448     public Name fromString(String s) {
449         return table.fromString(s);
450     }
451 
452     public Name fromUtf(byte[] cs) throws InvalidUtfException {
453         return table.fromUtf(cs);
454     }
455 
456     public Name fromUtf(byte[] cs, int start, int len, Convert.Validation validation) throws InvalidUtfException {
457         return table.fromUtf(cs, start, len, validation);
458     }
459 
460     public Name fromUtfLax(byte[] cs, int start, int len) {
461         try {
462             return table.fromUtf(cs, start, len, Convert.Validation.NONE);
463         } catch (InvalidUtfException e) {
464             throw new AssertionError(e);
465         }
466     }
467 }