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