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