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