1 /* 2 * Copyright (c) 1999, 2024, 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 underscore; 73 public final Name when; 74 public final Name with; 75 public final Name yield; 76 77 // field and method names 78 public final Name _name; 79 public final Name addSuppressed; 80 public final Name any; 81 public final Name append; 82 public final Name clinit; 83 public final Name clone; 84 public final Name close; 85 public final Name deserializeLambda; 86 public final Name desiredAssertionStatus; 87 public final Name equals; 88 public final Name error; 89 public final Name finalize; 90 public final Name forRemoval; 91 public final Name reflective; 92 public final Name getClass; 93 public final Name hasNext; 94 public final Name hashCode; 95 public final Name init; 96 public final Name invoke; 97 public final Name iterator; 98 public final Name length; 99 public final Name next; 100 public final Name of; 101 public final Name ordinal; 102 public final Name provider; 103 public final Name serialVersionUID; 104 public final Name toString; 105 public final Name value; 106 public final Name valueOf; 107 public final Name values; 108 public final Name readResolve; 109 public final Name readObject; 110 111 // class names 112 public final Name java_io_Serializable; 113 public final Name java_lang_Class; 114 public final Name java_lang_Cloneable; 115 public final Name java_lang_Enum; 116 public final Name java_lang_Object; 117 118 // names of builtin classes 119 public final Name Array; 120 public final Name Bound; 121 public final Name Method; 122 123 // package names 124 public final Name java; 125 public final Name java_lang; 126 public final Name jdk_internal_javac; 127 128 // module names 129 public final Name java_base; 130 public final Name java_se; 131 public final Name jdk_unsupported; 132 133 // attribute names 134 public final Name Annotation; 135 public final Name AnnotationDefault; 136 public final Name BootstrapMethods; 137 public final Name Bridge; 138 public final Name CharacterRangeTable; 139 public final Name Code; 140 public final Name CompilationID; 141 public final Name ConstantValue; 142 public final Name Deprecated; 143 public final Name EnclosingMethod; 144 public final Name Enum; 145 public final Name Exceptions; 146 public final Name InnerClasses; 147 public final Name LineNumberTable; 148 public final Name LocalVariableTable; 149 public final Name LocalVariableTypeTable; 150 public final Name MethodParameters; 151 public final Name Module; 152 public final Name ModuleResolution; 153 public final Name NestHost; 154 public final Name NestMembers; 155 public final Name Record; 156 public final Name RuntimeInvisibleAnnotations; 157 public final Name RuntimeInvisibleParameterAnnotations; 158 public final Name RuntimeInvisibleTypeAnnotations; 159 public final Name RuntimeVisibleAnnotations; 160 public final Name RuntimeVisibleParameterAnnotations; 161 public final Name RuntimeVisibleTypeAnnotations; 162 public final Name Signature; 163 public final Name SourceFile; 164 public final Name SourceID; 165 public final Name StackMap; 166 public final Name StackMapTable; 167 public final Name Synthetic; 168 public final Name Value; 169 public final Name Varargs; 170 public final Name PermittedSubclasses; 171 172 // members of java.lang.annotation.ElementType 173 public final Name ANNOTATION_TYPE; 174 public final Name CONSTRUCTOR; 175 public final Name FIELD; 176 public final Name LOCAL_VARIABLE; 177 public final Name METHOD; 178 public final Name MODULE; 179 public final Name PACKAGE; 180 public final Name PARAMETER; 181 public final Name TYPE; 182 public final Name TYPE_PARAMETER; 183 public final Name TYPE_USE; 184 public final Name RECORD_COMPONENT; 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 ex; 194 public final Name module_info; 195 public final Name package_info; 196 public final Name requireNonNull; 197 public final Name main; 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 // record related 210 // members of java.lang.runtime.ObjectMethods 211 public final Name bootstrap; 212 213 public final Name record; 214 public final Name non; 215 216 // serialization members, used by records too 217 public final Name serialPersistentFields; 218 public final Name writeObject; 219 public final Name writeReplace; 220 public final Name readObjectNoData; 221 222 // sealed types 223 public final Name permits; 224 public final Name sealed; 225 226 // pattern switches 227 public final Name typeSwitch; 228 public final Name enumSwitch; 229 public final Name enumConstant; 230 231 // code reflection 232 public final Name jdk_incubator_code; 233 public final Name quoted; 234 public final Name quotable; 235 236 // special annotation names 237 public final Name requiresIdentityInternal; 238 239 public final Name.Table table; 240 241 @SuppressWarnings("this-escape") 242 public Names(Context context) { 243 Options options = Options.instance(context); 244 table = createTable(options); 245 246 // operators and punctuation 247 asterisk = fromString("*"); 248 comma = fromString(","); 249 empty = fromString(""); 250 hyphen = fromString("-"); 251 one = fromString("1"); 252 slash = fromString("/"); 253 254 // keywords 255 _class = fromString("class"); 256 _super = fromString("super"); 257 _this = fromString("this"); 258 var = fromString("var"); 259 exports = fromString("exports"); 260 opens = fromString("opens"); 261 module = fromString("module"); 262 provides = fromString("provides"); 263 requires = fromString("requires"); 264 to = fromString("to"); 265 transitive = fromString("transitive"); 266 uses = fromString("uses"); 267 open = fromString("open"); 268 underscore = fromString("_"); 269 when = fromString("when"); 270 with = fromString("with"); 271 yield = fromString("yield"); 272 273 // field and method names 274 _name = fromString("name"); 275 addSuppressed = fromString("addSuppressed"); 276 any = fromString("<any>"); 277 append = fromString("append"); 278 clinit = fromString("<clinit>"); 279 clone = fromString("clone"); 280 close = fromString("close"); 281 deserializeLambda = fromString("$deserializeLambda$"); 282 desiredAssertionStatus = fromString("desiredAssertionStatus"); 283 equals = fromString("equals"); 284 error = fromString("<error>"); 285 finalize = fromString("finalize"); 286 forRemoval = fromString("forRemoval"); 287 reflective = fromString("reflective"); 288 getClass = fromString("getClass"); 289 hasNext = fromString("hasNext"); 290 hashCode = fromString("hashCode"); 291 init = fromString("<init>"); 292 invoke = fromString("invoke"); 293 iterator = fromString("iterator"); 294 length = fromString("length"); 295 next = fromString("next"); 296 of = fromString("of"); 297 ordinal = fromString("ordinal"); 298 provider = fromString("provider"); 299 serialVersionUID = fromString("serialVersionUID"); 300 toString = fromString("toString"); 301 value = fromString("value"); 302 valueOf = fromString("valueOf"); 303 values = fromString("values"); 304 readResolve = fromString("readResolve"); 305 readObject = fromString("readObject"); 306 dollarThis = fromString("$this"); 307 308 // class names 309 java_io_Serializable = fromString("java.io.Serializable"); 310 java_lang_Class = fromString("java.lang.Class"); 311 java_lang_Cloneable = fromString("java.lang.Cloneable"); 312 java_lang_Enum = fromString("java.lang.Enum"); 313 java_lang_Object = fromString("java.lang.Object"); 314 315 // names of builtin classes 316 Array = fromString("Array"); 317 Bound = fromString("Bound"); 318 Method = fromString("Method"); 319 320 // package names 321 java = fromString("java"); 322 java_lang = fromString("java.lang"); 323 jdk_internal_javac = fromString("jdk.internal.javac"); 324 325 // module names 326 java_base = fromString("java.base"); 327 java_se = fromString("java.se"); 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 Record = fromString("Record"); 353 RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations"); 354 RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations"); 355 RuntimeInvisibleTypeAnnotations = fromString("RuntimeInvisibleTypeAnnotations"); 356 RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations"); 357 RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations"); 358 RuntimeVisibleTypeAnnotations = fromString("RuntimeVisibleTypeAnnotations"); 359 Signature = fromString("Signature"); 360 SourceFile = fromString("SourceFile"); 361 SourceID = fromString("SourceID"); 362 StackMap = fromString("StackMap"); 363 StackMapTable = fromString("StackMapTable"); 364 Synthetic = fromString("Synthetic"); 365 Value = fromString("Value"); 366 Varargs = fromString("Varargs"); 367 PermittedSubclasses = fromString("PermittedSubclasses"); 368 369 // members of java.lang.annotation.ElementType 370 ANNOTATION_TYPE = fromString("ANNOTATION_TYPE"); 371 CONSTRUCTOR = fromString("CONSTRUCTOR"); 372 FIELD = fromString("FIELD"); 373 LOCAL_VARIABLE = fromString("LOCAL_VARIABLE"); 374 METHOD = fromString("METHOD"); 375 MODULE = fromString("MODULE"); 376 PACKAGE = fromString("PACKAGE"); 377 PARAMETER = fromString("PARAMETER"); 378 TYPE = fromString("TYPE"); 379 TYPE_PARAMETER = fromString("TYPE_PARAMETER"); 380 TYPE_USE = fromString("TYPE_USE"); 381 RECORD_COMPONENT = fromString("RECORD_COMPONENT"); 382 383 // members of java.lang.annotation.RetentionPolicy 384 CLASS = fromString("CLASS"); 385 RUNTIME = fromString("RUNTIME"); 386 SOURCE = fromString("SOURCE"); 387 388 // other identifiers 389 T = fromString("T"); 390 ex = fromString("ex"); 391 module_info = fromString("module-info"); 392 package_info = fromString("package-info"); 393 requireNonNull = fromString("requireNonNull"); 394 main = fromString("main"); 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 bootstrap = fromString("bootstrap"); 406 record = fromString("record"); 407 non = fromString("non"); 408 409 serialPersistentFields = fromString("serialPersistentFields"); 410 writeObject = fromString("writeObject"); 411 writeReplace = fromString("writeReplace"); 412 readObjectNoData = fromString("readObjectNoData"); 413 414 // sealed types 415 permits = fromString("permits"); 416 sealed = fromString("sealed"); 417 418 419 // pattern switches 420 typeSwitch = fromString("typeSwitch"); 421 enumSwitch = fromString("enumSwitch"); 422 enumConstant = fromString("enumConstant"); 423 424 // code reflection 425 jdk_incubator_code = fromString("jdk.incubator.code"); 426 quoted = fromString("Quoted"); 427 quotable = fromString("Quotable"); 428 429 // special annotations: 430 requiresIdentityInternal = fromString("jdk.internal.RequiresIdentity+Annotation"); 431 } 432 433 protected Name.Table createTable(Options options) { 434 boolean useUnsharedTable = options.isSet("useUnsharedTable"); 435 if (useUnsharedTable) 436 return newUnsharedNameTable(); 437 boolean useSharedTable = options.isSet("useSharedTable"); 438 if (useSharedTable) 439 return newSharedNameTable(); 440 boolean internStringTable = options.isSet("internStringTable"); 441 return newStringNameTable(internStringTable); 442 } 443 444 public StringNameTable newStringNameTable(boolean intern) { 445 return StringNameTable.create(this, intern); 446 } 447 448 public SharedNameTable newSharedNameTable() { 449 return SharedNameTable.create(this); 450 } 451 452 public UnsharedNameTable newUnsharedNameTable() { 453 return UnsharedNameTable.create(this); 454 } 455 456 public void dispose() { 457 table.dispose(); 458 } 459 460 public Name fromChars(char[] cs, int start, int len) { 461 return table.fromChars(cs, start, len); 462 } 463 464 public Name fromString(String s) { 465 return table.fromString(s); 466 } 467 468 public Name fromUtf(byte[] cs) throws InvalidUtfException { 469 return table.fromUtf(cs); 470 } 471 472 public Name fromUtf(byte[] cs, int start, int len, Convert.Validation validation) throws InvalidUtfException { 473 return table.fromUtf(cs, start, len, validation); 474 } 475 476 public Name fromUtfLax(byte[] cs, int start, int len) { 477 try { 478 return table.fromUtf(cs, start, len, Convert.Validation.NONE); 479 } catch (InvalidUtfException e) { 480 throw new AssertionError(e); 481 } 482 } 483 }