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