1 /*
  2  * Copyright (c) 1999, 2022, 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 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 vnew;
 96     public final Name init;
 97     public final Name iterator;
 98     public final Name length;
 99     public final Name next;
100     public final Name ordinal;
101     public final Name provider;
102     public final Name serialVersionUID;
103     public final Name toString;
104     public final Name value;
105     public final Name primitive;
106     public final Name identity;
107     public final Name valueOf;
108     public final Name values;
109     public final Name readResolve;
110     public final Name readObject;
111     public final Name isValueObject;
112 
113     // class names
114     public final Name java_io_Serializable;
115     public final Name java_lang_Class;
116     public final Name java_lang_Cloneable;
117     public final Name java_lang_Enum;
118     public final Name java_lang_Object;
119     public final Name java_lang_System;
120 
121     // names of builtin classes
122     public final Name Array;
123     public final Name Bound;
124     public final Name Method;
125 
126     // package names
127     public final Name java;
128     public final Name java_lang;
129     public final Name jdk_internal_javac;
130 
131     // module names
132     public final Name java_base;
133     public final Name jdk_unsupported;
134 
135     // attribute names
136     public final Name Annotation;
137     public final Name AnnotationDefault;
138     public final Name BootstrapMethods;
139     public final Name Bridge;
140     public final Name CharacterRangeTable;
141     public final Name Code;
142     public final Name CompilationID;
143     public final Name ConstantValue;
144     public final Name Deprecated;
145     public final Name EnclosingMethod;
146     public final Name Enum;
147     public final Name Exceptions;
148     public final Name InnerClasses;
149     public final Name LineNumberTable;
150     public final Name LocalVariableTable;
151     public final Name LocalVariableTypeTable;
152     public final Name MethodParameters;
153     public final Name Module;
154     public final Name ModuleResolution;
155     public final Name NestHost;
156     public final Name NestMembers;
157     public final Name Preload;
158     public final Name Record;
159     public final Name RuntimeInvisibleAnnotations;
160     public final Name RuntimeInvisibleParameterAnnotations;
161     public final Name RuntimeInvisibleTypeAnnotations;
162     public final Name RuntimeVisibleAnnotations;
163     public final Name RuntimeVisibleParameterAnnotations;
164     public final Name RuntimeVisibleTypeAnnotations;
165     public final Name Signature;
166     public final Name SourceFile;
167     public final Name SourceID;
168     public final Name StackMap;
169     public final Name StackMapTable;
170     public final Name Synthetic;
171     public final Name Value;
172     public final Name Varargs;
173     public final Name PermittedSubclasses;
174 
175     // members of java.lang.annotation.ElementType
176     public final Name ANNOTATION_TYPE;
177     public final Name CONSTRUCTOR;
178     public final Name FIELD;
179     public final Name LOCAL_VARIABLE;
180     public final Name METHOD;
181     public final Name MODULE;
182     public final Name PACKAGE;
183     public final Name PARAMETER;
184     public final Name TYPE;
185     public final Name TYPE_PARAMETER;
186     public final Name TYPE_USE;
187     public final Name RECORD_COMPONENT;
188 
189     // members of java.lang.annotation.RetentionPolicy
190     public final Name CLASS;
191     public final Name RUNTIME;
192     public final Name SOURCE;
193 
194     // other identifiers
195     public final Name T;
196     public final Name ex;
197     public final Name module_info;
198     public final Name package_info;
199     public final Name requireNonNull;
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     public final Name ref;
214     public final Name val;
215 
216 
217     // record related
218     // members of java.lang.runtime.ObjectMethods
219     public final Name bootstrap;
220 
221     public final Name record;
222     public final Name non;
223 
224     // serialization members, used by records too
225     public final Name serialPersistentFields;
226     public final Name writeObject;
227     public final Name writeReplace;
228     public final Name readObjectNoData;
229 
230     // sealed types
231     public final Name permits;
232     public final Name sealed;
233 
234     // pattern switches
235     public final Name typeSwitch;
236     public final Name enumSwitch;
237 
238     public final Name.Table table;
239 
240     public Names(Context context) {
241         Options options = Options.instance(context);
242         table = createTable(options);
243 
244         // operators and punctuation
245         asterisk = fromString("*");
246         comma = fromString(",");
247         empty = fromString("");
248         hyphen = fromString("-");
249         one = fromString("1");
250         slash = fromString("/");
251 
252         // keywords
253         _class = fromString("class");
254         _default = fromString("default");
255         _super = fromString("super");
256         _this = fromString("this");
257         var = fromString("var");
258         exports = fromString("exports");
259         opens = fromString("opens");
260         module = fromString("module");
261         provides = fromString("provides");
262         requires = fromString("requires");
263         to = fromString("to");
264         transitive = fromString("transitive");
265         uses = fromString("uses");
266         open = fromString("open");
267         when = fromString("when");
268         with = fromString("with");
269         yield = fromString("yield");
270 
271         // field and method names
272         _name = fromString("name");
273         addSuppressed = fromString("addSuppressed");
274         any = fromString("<any>");
275         append = fromString("append");
276         clinit = fromString("<clinit>");
277         clone = fromString("clone");
278         close = fromString("close");
279         deserializeLambda = fromString("$deserializeLambda$");
280         desiredAssertionStatus = fromString("desiredAssertionStatus");
281         equals = fromString("equals");
282         error = fromString("<error>");
283         finalize = fromString("finalize");
284         forRemoval = fromString("forRemoval");
285         reflective = fromString("reflective");
286         getClass = fromString("getClass");
287         hasNext = fromString("hasNext");
288         hashCode = fromString("hashCode");
289         vnew = fromString("<vnew>");
290         init = fromString("<init>");
291         iterator = fromString("iterator");
292         length = fromString("length");
293         next = fromString("next");
294         ordinal = fromString("ordinal");
295         provider = fromString("provider");
296         serialVersionUID = fromString("serialVersionUID");
297         toString = fromString("toString");
298         value = fromString("value");
299         primitive = fromString("primitive");
300         identity = fromString("identity");
301         valueOf = fromString("valueOf");
302         values = fromString("values");
303         readResolve = fromString("readResolve");
304         readObject = fromString("readObject");
305         isValueObject = fromString("isValueObject");
306         dollarThis = fromString("$this");
307 
308         // class names
309         java_io_Serializable = fromString("java.io.Serializable");
310         java_lang_Class = fromString("java.lang.Class");
311         java_lang_Cloneable = fromString("java.lang.Cloneable");
312         java_lang_Enum = fromString("java.lang.Enum");
313         java_lang_Object = fromString("java.lang.Object");
314         java_lang_System = fromString("java.lang.System");
315 
316         // names of builtin classes
317         Array = fromString("Array");
318         Bound = fromString("Bound");
319         Method = fromString("Method");
320 
321         // package names
322         java = fromString("java");
323         java_lang = fromString("java.lang");
324         jdk_internal_javac = fromString("jdk.internal.javac");
325 
326         // module names
327         java_base = fromString("java.base");
328         jdk_unsupported = fromString("jdk.unsupported");
329 
330         // attribute names
331         Annotation = fromString("Annotation");
332         AnnotationDefault = fromString("AnnotationDefault");
333         BootstrapMethods = fromString("BootstrapMethods");
334         Bridge = fromString("Bridge");
335         CharacterRangeTable = fromString("CharacterRangeTable");
336         Code = fromString("Code");
337         CompilationID = fromString("CompilationID");
338         ConstantValue = fromString("ConstantValue");
339         Deprecated = fromString("Deprecated");
340         EnclosingMethod = fromString("EnclosingMethod");
341         Enum = fromString("Enum");
342         Exceptions = fromString("Exceptions");
343         InnerClasses = fromString("InnerClasses");
344         LineNumberTable = fromString("LineNumberTable");
345         LocalVariableTable = fromString("LocalVariableTable");
346         LocalVariableTypeTable = fromString("LocalVariableTypeTable");
347         MethodParameters = fromString("MethodParameters");
348         Module = fromString("Module");
349         ModuleResolution = fromString("ModuleResolution");
350         NestHost = fromString("NestHost");
351         NestMembers = fromString("NestMembers");
352         Preload = fromString("Preload");
353         Record = fromString("Record");
354         RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations");
355         RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations");
356         RuntimeInvisibleTypeAnnotations = fromString("RuntimeInvisibleTypeAnnotations");
357         RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations");
358         RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations");
359         RuntimeVisibleTypeAnnotations = fromString("RuntimeVisibleTypeAnnotations");
360         Signature = fromString("Signature");
361         SourceFile = fromString("SourceFile");
362         SourceID = fromString("SourceID");
363         StackMap = fromString("StackMap");
364         StackMapTable = fromString("StackMapTable");
365         Synthetic = fromString("Synthetic");
366         Value = fromString("Value");
367         Varargs = fromString("Varargs");
368         PermittedSubclasses = fromString("PermittedSubclasses");
369 
370         // members of java.lang.annotation.ElementType
371         ANNOTATION_TYPE = fromString("ANNOTATION_TYPE");
372         CONSTRUCTOR = fromString("CONSTRUCTOR");
373         FIELD = fromString("FIELD");
374         LOCAL_VARIABLE = fromString("LOCAL_VARIABLE");
375         METHOD = fromString("METHOD");
376         MODULE = fromString("MODULE");
377         PACKAGE = fromString("PACKAGE");
378         PARAMETER = fromString("PARAMETER");
379         TYPE = fromString("TYPE");
380         TYPE_PARAMETER = fromString("TYPE_PARAMETER");
381         TYPE_USE = fromString("TYPE_USE");
382         RECORD_COMPONENT = fromString("RECORD_COMPONENT");
383 
384         // members of java.lang.annotation.RetentionPolicy
385         CLASS = fromString("CLASS");
386         RUNTIME = fromString("RUNTIME");
387         SOURCE = fromString("SOURCE");
388 
389         // other identifiers
390         T = fromString("T");
391         ex = fromString("ex");
392         module_info = fromString("module-info");
393         package_info = fromString("package-info");
394         requireNonNull = fromString("requireNonNull");
395 
396         //lambda-related
397         lambda = fromString("lambda$");
398         metafactory = fromString("metafactory");
399         altMetafactory = fromString("altMetafactory");
400 
401         // string concat
402         makeConcat = fromString("makeConcat");
403         makeConcatWithConstants = fromString("makeConcatWithConstants");
404 
405         // primitive classes
406         dollarValue = fromString("$value");
407         ref = fromString("ref");
408         val = fromString("val");
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         // pattern switches
424         typeSwitch = fromString("typeSwitch");
425         enumSwitch = fromString("enumSwitch");
426     }
427 
428     protected Name.Table createTable(Options options) {
429         boolean useUnsharedTable = options.isSet("useUnsharedTable");
430         if (useUnsharedTable)
431             return UnsharedNameTable.create(this);
432         else
433             return SharedNameTable.create(this);
434     }
435 
436     public boolean isInitOrVNew(Name name) {
437         return name == init || name == vnew;
438     }
439 
440     public void dispose() {
441         table.dispose();
442     }
443 
444     public Name fromChars(char[] cs, int start, int len) {
445         return table.fromChars(cs, start, len);
446     }
447 
448     public Name fromString(String s) {
449         return table.fromString(s);
450     }
451 
452     public Name fromUtf(byte[] cs) {
453         return table.fromUtf(cs);
454     }
455 
456     public Name fromUtf(byte[] cs, int start, int len) {
457         return table.fromUtf(cs, start, len);
458     }
459 }