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