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