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 _default;
 61     public final Name _super;
 62     public final Name _this;
 63     public final Name var;
 64     public final Name exports;
 65     public final Name opens;
 66     public final Name module;
 67     public final Name provides;
 68     public final Name requires;
 69     public final Name to;
 70     public final Name transitive;
 71     public final Name uses;
 72     public final Name open;
 73     public final Name underscore;
 74     public final Name when;
 75     public final Name with;
 76     public final Name yield;
 77 
 78     // field and method names
 79     public final Name _name;
 80     public final Name addSuppressed;
 81     public final Name any;
 82     public final Name append;
 83     public final Name clinit;
 84     public final Name clone;
 85     public final Name close;
 86     public final Name deserializeLambda;
 87     public final Name desiredAssertionStatus;
 88     public final Name equals;
 89     public final Name error;
 90     public final Name finalize;
 91     public final Name forRemoval;
 92     public final Name reflective;
 93     public final Name getClass;
 94     public final Name hasNext;
 95     public final Name hashCode;
 96     public final Name vnew;
 97     public final Name init;
 98     public final Name invoke;
 99     public final Name iterator;
100     public final Name length;
101     public final Name next;
102     public final Name of;
103     public final Name ordinal;
104     public final Name provider;
105     public final Name serialVersionUID;
106     public final Name toString;
107     public final Name value;
108     public final Name primitive;
109     public final Name identity;
110     public final Name valueOf;
111     public final Name values;
112     public final Name readResolve;
113     public final Name readObject;
114     public final Name isValueObject;
115 
116     // class names
117     public final Name java_io_Serializable;
118     public final Name java_lang_Class;
119     public final Name java_lang_Cloneable;
120     public final Name java_lang_Enum;
121     public final Name java_lang_Object;
122     public final Name java_lang_System;
123 
124     // names of builtin classes
125     public final Name Array;
126     public final Name Bound;
127     public final Name Method;
128 
129     // package names
130     public final Name java;
131     public final Name java_lang;
132     public final Name jdk_internal_javac;
133 
134     // module names
135     public final Name java_base;
136     public final Name jdk_unsupported;
137 
138     // attribute names
139     public final Name Annotation;
140     public final Name AnnotationDefault;
141     public final Name BootstrapMethods;
142     public final Name Bridge;
143     public final Name CharacterRangeTable;
144     public final Name Code;
145     public final Name CompilationID;
146     public final Name ConstantValue;
147     public final Name Deprecated;
148     public final Name EnclosingMethod;
149     public final Name Enum;
150     public final Name Exceptions;
151     public final Name InnerClasses;
152     public final Name LineNumberTable;
153     public final Name LocalVariableTable;
154     public final Name LocalVariableTypeTable;
155     public final Name MethodParameters;
156     public final Name Module;
157     public final Name ModuleResolution;
158     public final Name NestHost;
159     public final Name NestMembers;
160     public final Name Preload;
161     public final Name Record;
162     public final Name RuntimeInvisibleAnnotations;
163     public final Name RuntimeInvisibleParameterAnnotations;
164     public final Name RuntimeInvisibleTypeAnnotations;
165     public final Name RuntimeVisibleAnnotations;
166     public final Name RuntimeVisibleParameterAnnotations;
167     public final Name RuntimeVisibleTypeAnnotations;
168     public final Name Signature;
169     public final Name SourceFile;
170     public final Name SourceID;
171     public final Name StackMap;
172     public final Name StackMapTable;
173     public final Name Synthetic;
174     public final Name Value;
175     public final Name Varargs;
176     public final Name PermittedSubclasses;
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     public final Name ref;
218     public final Name val;
219 
220 
221     // record related
222     // members of java.lang.runtime.ObjectMethods
223     public final Name bootstrap;
224 
225     public final Name record;
226     public final Name non;
227 
228     // serialization members, used by records too
229     public final Name serialPersistentFields;
230     public final Name writeObject;
231     public final Name writeReplace;
232     public final Name readObjectNoData;
233 
234     // sealed types
235     public final Name permits;
236     public final Name sealed;
237 
238     // pattern switches
239     public final Name typeSwitch;
240     public final Name enumSwitch;
241     public final Name enumConstant;
242 
243     // templated string
244     public final Name process;
245     public final Name STR;
246     public final Name RAW;
247     public final Name newStringTemplate;
248     public final Name newLargeStringTemplate;
249     public final Name processStringTemplate;
250 
251     public final Name.Table table;
252 
253     @SuppressWarnings("this-escape")
254     public Names(Context context) {
255         Options options = Options.instance(context);
256         table = createTable(options);
257 
258         // operators and punctuation
259         asterisk = fromString("*");
260         comma = fromString(",");
261         empty = fromString("");
262         hyphen = fromString("-");
263         one = fromString("1");
264         slash = fromString("/");
265 
266         // keywords
267         _class = fromString("class");
268         _default = fromString("default");
269         _super = fromString("super");
270         _this = fromString("this");
271         var = fromString("var");
272         exports = fromString("exports");
273         opens = fromString("opens");
274         module = fromString("module");
275         provides = fromString("provides");
276         requires = fromString("requires");
277         to = fromString("to");
278         transitive = fromString("transitive");
279         uses = fromString("uses");
280         open = fromString("open");
281         underscore = fromString("_");
282         when = fromString("when");
283         with = fromString("with");
284         yield = fromString("yield");
285 
286         // field and method names
287         _name = fromString("name");
288         addSuppressed = fromString("addSuppressed");
289         any = fromString("<any>");
290         append = fromString("append");
291         clinit = fromString("<clinit>");
292         clone = fromString("clone");
293         close = fromString("close");
294         deserializeLambda = fromString("$deserializeLambda$");
295         desiredAssertionStatus = fromString("desiredAssertionStatus");
296         equals = fromString("equals");
297         error = fromString("<error>");
298         finalize = fromString("finalize");
299         forRemoval = fromString("forRemoval");
300         reflective = fromString("reflective");
301         getClass = fromString("getClass");
302         hasNext = fromString("hasNext");
303         hashCode = fromString("hashCode");
304         vnew = fromString("<vnew>");
305         init = fromString("<init>");
306         invoke = fromString("invoke");
307         iterator = fromString("iterator");
308         length = fromString("length");
309         next = fromString("next");
310         of = fromString("of");
311         ordinal = fromString("ordinal");
312         provider = fromString("provider");
313         serialVersionUID = fromString("serialVersionUID");
314         toString = fromString("toString");
315         value = fromString("value");
316         primitive = fromString("primitive");
317         identity = fromString("identity");
318         valueOf = fromString("valueOf");
319         values = fromString("values");
320         readResolve = fromString("readResolve");
321         readObject = fromString("readObject");
322         isValueObject = fromString("isValueObject");
323         dollarThis = fromString("$this");
324 
325         // class names
326         java_io_Serializable = fromString("java.io.Serializable");
327         java_lang_Class = fromString("java.lang.Class");
328         java_lang_Cloneable = fromString("java.lang.Cloneable");
329         java_lang_Enum = fromString("java.lang.Enum");
330         java_lang_Object = fromString("java.lang.Object");
331         java_lang_System = fromString("java.lang.System");
332 
333         // names of builtin classes
334         Array = fromString("Array");
335         Bound = fromString("Bound");
336         Method = fromString("Method");
337 
338         // package names
339         java = fromString("java");
340         java_lang = fromString("java.lang");
341         jdk_internal_javac = fromString("jdk.internal.javac");
342 
343         // module names
344         java_base = fromString("java.base");
345         jdk_unsupported = fromString("jdk.unsupported");
346 
347         // attribute names
348         Annotation = fromString("Annotation");
349         AnnotationDefault = fromString("AnnotationDefault");
350         BootstrapMethods = fromString("BootstrapMethods");
351         Bridge = fromString("Bridge");
352         CharacterRangeTable = fromString("CharacterRangeTable");
353         Code = fromString("Code");
354         CompilationID = fromString("CompilationID");
355         ConstantValue = fromString("ConstantValue");
356         Deprecated = fromString("Deprecated");
357         EnclosingMethod = fromString("EnclosingMethod");
358         Enum = fromString("Enum");
359         Exceptions = fromString("Exceptions");
360         InnerClasses = fromString("InnerClasses");
361         LineNumberTable = fromString("LineNumberTable");
362         LocalVariableTable = fromString("LocalVariableTable");
363         LocalVariableTypeTable = fromString("LocalVariableTypeTable");
364         MethodParameters = fromString("MethodParameters");
365         Module = fromString("Module");
366         ModuleResolution = fromString("ModuleResolution");
367         NestHost = fromString("NestHost");
368         NestMembers = fromString("NestMembers");
369         Preload = fromString("Preload");
370         Record = fromString("Record");
371         RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations");
372         RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations");
373         RuntimeInvisibleTypeAnnotations = fromString("RuntimeInvisibleTypeAnnotations");
374         RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations");
375         RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations");
376         RuntimeVisibleTypeAnnotations = fromString("RuntimeVisibleTypeAnnotations");
377         Signature = fromString("Signature");
378         SourceFile = fromString("SourceFile");
379         SourceID = fromString("SourceID");
380         StackMap = fromString("StackMap");
381         StackMapTable = fromString("StackMapTable");
382         Synthetic = fromString("Synthetic");
383         Value = fromString("Value");
384         Varargs = fromString("Varargs");
385         PermittedSubclasses = fromString("PermittedSubclasses");
386 
387         // members of java.lang.annotation.ElementType
388         ANNOTATION_TYPE = fromString("ANNOTATION_TYPE");
389         CONSTRUCTOR = fromString("CONSTRUCTOR");
390         FIELD = fromString("FIELD");
391         LOCAL_VARIABLE = fromString("LOCAL_VARIABLE");
392         METHOD = fromString("METHOD");
393         MODULE = fromString("MODULE");
394         PACKAGE = fromString("PACKAGE");
395         PARAMETER = fromString("PARAMETER");
396         TYPE = fromString("TYPE");
397         TYPE_PARAMETER = fromString("TYPE_PARAMETER");
398         TYPE_USE = fromString("TYPE_USE");
399         RECORD_COMPONENT = fromString("RECORD_COMPONENT");
400 
401         // members of java.lang.annotation.RetentionPolicy
402         CLASS = fromString("CLASS");
403         RUNTIME = fromString("RUNTIME");
404         SOURCE = fromString("SOURCE");
405 
406         // other identifiers
407         T = fromString("T");
408         ex = fromString("ex");
409         module_info = fromString("module-info");
410         package_info = fromString("package-info");
411         requireNonNull = fromString("requireNonNull");
412         main = fromString("main");
413 
414         //lambda-related
415         lambda = fromString("lambda$");
416         metafactory = fromString("metafactory");
417         altMetafactory = fromString("altMetafactory");
418 
419         // string concat
420         makeConcat = fromString("makeConcat");
421         makeConcatWithConstants = fromString("makeConcatWithConstants");
422 
423         // primitive classes
424         dollarValue = fromString("$value");
425         ref = fromString("ref");
426         val = fromString("val");
427 
428         bootstrap = fromString("bootstrap");
429         record = fromString("record");
430         non = fromString("non");
431 
432         serialPersistentFields = fromString("serialPersistentFields");
433         writeObject = fromString("writeObject");
434         writeReplace = fromString("writeReplace");
435         readObjectNoData = fromString("readObjectNoData");
436 
437         // sealed types
438         permits = fromString("permits");
439         sealed = fromString("sealed");
440 
441         // templated string
442         process = fromString("process");
443         STR = fromString("STR");
444         RAW = fromString("RAW");
445         newStringTemplate = fromString("newStringTemplate");
446         newLargeStringTemplate = fromString("newLargeStringTemplate");
447         processStringTemplate = fromString("processStringTemplate");
448 
449         // pattern switches
450         typeSwitch = fromString("typeSwitch");
451         enumSwitch = fromString("enumSwitch");
452         enumConstant = fromString("enumConstant");
453     }
454 
455     protected Name.Table createTable(Options options) {
456         boolean useStringTable = options.isSet("useStringTable");
457         if (useStringTable)
458             return newStringNameTable();
459         boolean useUnsharedTable = options.isSet("useUnsharedTable");
460         if (useUnsharedTable)
461             return newUnsharedNameTable();
462         return newSharedNameTable();
463     }
464 
465     public StringNameTable newStringNameTable() {
466         return StringNameTable.create(this);
467     }
468 
469     public SharedNameTable newSharedNameTable() {
470         return SharedNameTable.create(this);
471     }
472 
473     public UnsharedNameTable newUnsharedNameTable() {
474         return UnsharedNameTable.create(this);
475     }
476 
477     public boolean isInitOrVNew(Name name) {
478         return name == init || name == vnew;
479     }
480 
481     public void dispose() {
482         table.dispose();
483     }
484 
485     public Name fromChars(char[] cs, int start, int len) {
486         return table.fromChars(cs, start, len);
487     }
488 
489     public Name fromString(String s) {
490         return table.fromString(s);
491     }
492 
493     public Name fromUtf(byte[] cs) throws InvalidUtfException {
494         return table.fromUtf(cs);
495     }
496 
497     public Name fromUtf(byte[] cs, int start, int len, Convert.Validation validation) throws InvalidUtfException {
498         return table.fromUtf(cs, start, len, validation);
499     }
500 
501     public Name fromUtfLax(byte[] cs, int start, int len) {
502         try {
503             return table.fromUtf(cs, start, len, Convert.Validation.NONE);
504         } catch (InvalidUtfException e) {
505             throw new AssertionError(e);
506         }
507     }
508 }