1 /*
  2  * Copyright (c) 1999, 2021, 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 with;
 74     public final Name yield;
 75 
 76     // field and method names
 77     public final Name _name;
 78     public final Name addSuppressed;
 79     public final Name any;
 80     public final Name append;
 81     public final Name clinit;
 82     public final Name clone;
 83     public final Name close;
 84     public final Name deserializeLambda;
 85     public final Name desiredAssertionStatus;
 86     public final Name equals;
 87     public final Name error;
 88     public final Name finalize;
 89     public final Name forRemoval;
 90     public final Name reflective;
 91     public final Name getClass;
 92     public final Name hasNext;
 93     public final Name hashCode;
 94     public final Name init;
 95     public final Name iterator;
 96     public final Name length;
 97     public final Name next;
 98     public final Name ordinal;
 99     public final Name provider;
100     public final Name serialVersionUID;
101     public final Name toString;
102     public final Name value;
103     public final Name primitive;
104     public final Name identity;
105     public final Name valueOf;
106     public final Name values;
107     public final Name readResolve;
108     public final Name readObject;
109 
110     // class names
111     public final Name java_io_Serializable;
112     public final Name java_lang_Class;
113     public final Name java_lang_Cloneable;
114     public final Name java_lang_Enum;
115     public final Name java_lang_Object;
116     public final Name java_lang_System;
117     public final Name __primitive__;
118     public final Name java_lang___primitive__;
119     public final Name __value__;
120     public final Name java_lang___value__;
121 
122     // names of builtin classes
123     public final Name Array;
124     public final Name Bound;
125     public final Name Method;
126 
127     // package names
128     public final Name java;
129     public final Name java_lang;
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         with = fromString("with");
268         yield = fromString("yield");
269 
270         // field and method names
271         _name = fromString("name");
272         addSuppressed = fromString("addSuppressed");
273         any = fromString("<any>");
274         append = fromString("append");
275         clinit = fromString("<clinit>");
276         clone = fromString("clone");
277         close = fromString("close");
278         deserializeLambda = fromString("$deserializeLambda$");
279         desiredAssertionStatus = fromString("desiredAssertionStatus");
280         equals = fromString("equals");
281         error = fromString("<error>");
282         finalize = fromString("finalize");
283         forRemoval = fromString("forRemoval");
284         reflective = fromString("reflective");
285         getClass = fromString("getClass");
286         hasNext = fromString("hasNext");
287         hashCode = fromString("hashCode");
288         init = fromString("<init>");
289         iterator = fromString("iterator");
290         length = fromString("length");
291         next = fromString("next");
292         ordinal = fromString("ordinal");
293         provider = fromString("provider");
294         serialVersionUID = fromString("serialVersionUID");
295         toString = fromString("toString");
296         value = fromString("value");
297         primitive = fromString("primitive");
298         identity = fromString("identity");
299         valueOf = fromString("valueOf");
300         values = fromString("values");
301         readResolve = fromString("readResolve");
302         readObject = fromString("readObject");
303         dollarThis = fromString("$this");
304 
305         // class names
306         java_io_Serializable = fromString("java.io.Serializable");
307         java_lang_Class = fromString("java.lang.Class");
308         java_lang_Cloneable = fromString("java.lang.Cloneable");
309         java_lang_Enum = fromString("java.lang.Enum");
310         java_lang_Object = fromString("java.lang.Object");
311         java_lang_System = fromString("java.lang.System");
312         __primitive__ = fromString("__primitive__");
313         java_lang___primitive__ = fromString("java.lang.__primitive__");
314         __value__ = fromString("__value__");
315         java_lang___value__ = fromString("java.lang.__value__");
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 
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 void dispose() {
437         table.dispose();
438     }
439 
440     public Name fromChars(char[] cs, int start, int len) {
441         return table.fromChars(cs, start, len);
442     }
443 
444     public Name fromString(String s) {
445         return table.fromString(s);
446     }
447 
448     public Name fromUtf(byte[] cs) {
449         return table.fromUtf(cs);
450     }
451 
452     public Name fromUtf(byte[] cs, int start, int len) {
453         return table.fromUtf(cs, start, len);
454     }
455 }