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