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     // special annotation names
232     public final Name requiresIdentityInternal;
233 
234     public final Name.Table table;
235 
236     @SuppressWarnings("this-escape")
237     public Names(Context context) {
238         Options options = Options.instance(context);
239         table = createTable(options);
240 
241         // operators and punctuation
242         asterisk = fromString("*");
243         comma = fromString(",");
244         empty = fromString("");
245         hyphen = fromString("-");
246         one = fromString("1");
247         slash = fromString("/");
248 
249         // keywords
250         _class = fromString("class");
251         _super = fromString("super");
252         _this = fromString("this");
253         var = fromString("var");
254         exports = fromString("exports");
255         opens = fromString("opens");
256         module = fromString("module");
257         provides = fromString("provides");
258         requires = fromString("requires");
259         to = fromString("to");
260         transitive = fromString("transitive");
261         uses = fromString("uses");
262         open = fromString("open");
263         underscore = fromString("_");
264         when = fromString("when");
265         with = fromString("with");
266         yield = fromString("yield");
267 
268         // field and method names
269         _name = fromString("name");
270         addSuppressed = fromString("addSuppressed");
271         any = fromString("<any>");
272         append = fromString("append");
273         clinit = fromString("<clinit>");
274         clone = fromString("clone");
275         close = fromString("close");
276         deserializeLambda = fromString("$deserializeLambda$");
277         desiredAssertionStatus = fromString("desiredAssertionStatus");
278         equals = fromString("equals");
279         error = fromString("<error>");
280         finalize = fromString("finalize");
281         forRemoval = fromString("forRemoval");
282         reflective = fromString("reflective");
283         getClass = fromString("getClass");
284         hasNext = fromString("hasNext");
285         hashCode = fromString("hashCode");
286         init = fromString("<init>");
287         invoke = fromString("invoke");
288         iterator = fromString("iterator");
289         length = fromString("length");
290         next = fromString("next");
291         of = fromString("of");
292         ordinal = fromString("ordinal");
293         provider = fromString("provider");
294         serialVersionUID = fromString("serialVersionUID");
295         toString = fromString("toString");
296         value = fromString("value");
297         valueOf = fromString("valueOf");
298         values = fromString("values");
299         readResolve = fromString("readResolve");
300         readObject = fromString("readObject");
301         dollarThis = fromString("$this");
302 
303         // class names
304         java_io_Serializable = fromString("java.io.Serializable");
305         java_lang_Class = fromString("java.lang.Class");
306         java_lang_Cloneable = fromString("java.lang.Cloneable");
307         java_lang_Enum = fromString("java.lang.Enum");
308         java_lang_Object = fromString("java.lang.Object");
309 
310         // names of builtin classes
311         Array = fromString("Array");
312         Bound = fromString("Bound");
313         Method = fromString("Method");
314 
315         // package names
316         java = fromString("java");
317         java_lang = fromString("java.lang");
318         jdk_internal_javac = fromString("jdk.internal.javac");
319 
320         // module names
321         java_base = fromString("java.base");
322         java_se = fromString("java.se");
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         // special annotations:
420         requiresIdentityInternal = fromString("jdk.internal.RequiresIdentity+Annotation");
421     }
422 
423     protected Name.Table createTable(Options options) {
424         boolean useUnsharedTable = options.isSet("useUnsharedTable");
425         if (useUnsharedTable)
426             return newUnsharedNameTable();
427         boolean useSharedTable = options.isSet("useSharedTable");
428         if (useSharedTable)
429             return newSharedNameTable();
430         boolean internStringTable = options.isSet("internStringTable");
431         return newStringNameTable(internStringTable);
432     }
433 
434     public StringNameTable newStringNameTable(boolean intern) {
435         return StringNameTable.create(this, intern);
436     }
437 
438     public SharedNameTable newSharedNameTable() {
439         return SharedNameTable.create(this);
440     }
441 
442     public UnsharedNameTable newUnsharedNameTable() {
443         return UnsharedNameTable.create(this);
444     }
445 
446     public void dispose() {
447         table.dispose();
448     }
449 
450     public Name fromChars(char[] cs, int start, int len) {
451         return table.fromChars(cs, start, len);
452     }
453 
454     public Name fromString(String s) {
455         return table.fromString(s);
456     }
457 
458     public Name fromUtf(byte[] cs) throws InvalidUtfException {
459         return table.fromUtf(cs);
460     }
461 
462     public Name fromUtf(byte[] cs, int start, int len, Convert.Validation validation) throws InvalidUtfException {
463         return table.fromUtf(cs, start, len, validation);
464     }
465 
466     public Name fromUtfLax(byte[] cs, int start, int len) {
467         try {
468             return table.fromUtf(cs, start, len, Convert.Validation.NONE);
469         } catch (InvalidUtfException e) {
470             throw new AssertionError(e);
471         }
472     }
473 }