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