1 /* 2 * Copyright (c) 2003, 2023, 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 java.lang; 27 28 // Android-changed: Make JavaLangAccess a final class. http://b/399374716 29 // public interface JavaLangAccess { 30 public final class JavaLangAccess { 31 32 // BEGIN Android-removed: Not used in Android. 33 /* 34 /** 35 * Returns the list of {@code Method} objects for the declared public 36 * methods of this class or interface that have the specified method name 37 * and parameter types. 38 * / 39 List<Method> getDeclaredPublicMethods(Class<?> klass, String name, Class<?>... parameterTypes); 40 41 /** 42 * Return the constant pool for a class. 43 * / 44 ConstantPool getConstantPool(Class<?> klass); 45 46 /** 47 * Compare-And-Set the AnnotationType instance corresponding to this class. 48 * (This method only applies to annotation types.) 49 * / 50 boolean casAnnotationType(Class<?> klass, AnnotationType oldType, AnnotationType newType); 51 52 /** 53 * Get the AnnotationType instance corresponding to this class. 54 * (This method only applies to annotation types.) 55 * / 56 AnnotationType getAnnotationType(Class<?> klass); 57 58 /** 59 * Get the declared annotations for a given class, indexed by their types. 60 * / 61 Map<Class<? extends Annotation>, Annotation> getDeclaredAnnotationMap(Class<?> klass); 62 63 /** 64 * Get the array of bytes that is the class-file representation 65 * of this Class' annotations. 66 * / 67 byte[] getRawClassAnnotations(Class<?> klass); 68 69 /** 70 * Get the array of bytes that is the class-file representation 71 * of this Class' type annotations. 72 * / 73 byte[] getRawClassTypeAnnotations(Class<?> klass); 74 75 /** 76 * Get the array of bytes that is the class-file representation 77 * of this Executable's type annotations. 78 * / 79 byte[] getRawExecutableTypeAnnotations(Executable executable); 80 */ 81 // END Android-removed: Not used in Android. 82 83 /** 84 * Returns the elements of an enum class or null if the 85 * Class object does not represent an enum type; 86 * the result is uncloned, cached, and shared by all callers. 87 */ getEnumConstantsShared(Class<E> klass)88 public <E extends Enum<E>> E[] getEnumConstantsShared(Class<E> klass) { 89 return klass.getEnumConstantsShared(); 90 } 91 92 // BEGIN Android-removed: Not used in Android. 93 /* 94 /** 95 * Set current thread's blocker field. 96 * / 97 void blockedOn(Interruptible b); 98 99 /** 100 * Registers a shutdown hook. 101 * 102 * It is expected that this method with registerShutdownInProgress=true 103 * is only used to register DeleteOnExitHook since the first file 104 * may be added to the delete on exit list by the application shutdown 105 * hooks. 106 * 107 * @param slot the slot in the shutdown hook array, whose element 108 * will be invoked in order during shutdown 109 * @param registerShutdownInProgress true to allow the hook 110 * to be registered even if the shutdown is in progress. 111 * @param hook the hook to be registered 112 * 113 * @throws IllegalStateException if shutdown is in progress and 114 * the slot is not valid to register. 115 * / 116 void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook); 117 118 /** 119 * Returns a new Thread with the given Runnable and an 120 * inherited AccessControlContext. 121 * / 122 Thread newThreadWithAcc(Runnable target, @SuppressWarnings("removal") AccessControlContext acc); 123 124 /** 125 * Invokes the finalize method of the given object. 126 * / 127 void invokeFinalize(Object o) throws Throwable; 128 129 /** 130 * Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s) 131 * associated with the given class loader, creating it if it doesn't already exist. 132 * / 133 ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap(ClassLoader cl); 134 135 /** 136 * Defines a class with the given name to a class loader. 137 * / 138 Class<?> defineClass(ClassLoader cl, String name, byte[] b, ProtectionDomain pd, String source); 139 140 /** 141 * Defines a class with the given name to a class loader with 142 * the given flags and class data. 143 * 144 * @see java.lang.invoke.MethodHandles.Lookup#defineClass 145 * / 146 Class<?> defineClass(ClassLoader cl, Class<?> lookup, String name, byte[] b, ProtectionDomain pd, boolean initialize, int flags, Object classData); 147 148 /** 149 * Returns a class loaded by the bootstrap class loader. 150 * / 151 Class<?> findBootstrapClassOrNull(String name); 152 153 /** 154 * Define a Package of the given name and module by the given class loader. 155 * / 156 Package definePackage(ClassLoader cl, String name, Module module); 157 158 /** 159 * Invokes Long.fastUUID 160 * / 161 String fastUUID(long lsb, long msb); 162 163 /** 164 * Record the non-exported packages of the modules in the given layer 165 * / 166 void addNonExportedPackages(ModuleLayer layer); 167 168 /** 169 * Invalidate package access cache 170 * / 171 void invalidatePackageAccessCache(); 172 173 /** 174 * Defines a new module to the Java virtual machine. The module 175 * is defined to the given class loader. 176 * 177 * The URI is for information purposes only, it can be {@code null}. 178 * / 179 Module defineModule(ClassLoader loader, ModuleDescriptor descriptor, URI uri); 180 181 /** 182 * Defines the unnamed module for the given class loader. 183 * / 184 Module defineUnnamedModule(ClassLoader loader); 185 186 /** 187 * Updates the readability so that module m1 reads m2. The new read edge 188 * does not result in a strong reference to m2 (m2 can be GC'ed). 189 * 190 * This method is the same as m1.addReads(m2) but without a permission check. 191 * / 192 void addReads(Module m1, Module m2); 193 194 /** 195 * Updates module m to read all unnamed modules. 196 * / 197 void addReadsAllUnnamed(Module m); 198 199 /** 200 * Updates module m1 to export a package unconditionally. 201 * / 202 void addExports(Module m1, String pkg); 203 204 /** 205 * Updates module m1 to export a package to module m2. The export does 206 * not result in a strong reference to m2 (m2 can be GC'ed). 207 * / 208 void addExports(Module m1, String pkg, Module m2); 209 210 /** 211 * Updates a module m to export a package to all unnamed modules. 212 * / 213 void addExportsToAllUnnamed(Module m, String pkg); 214 215 /** 216 * Updates module m1 to open a package to module m2. Opening the 217 * package does not result in a strong reference to m2 (m2 can be GC'ed). 218 * / 219 void addOpens(Module m1, String pkg, Module m2); 220 221 /** 222 * Updates module m to open a package to all unnamed modules. 223 * / 224 void addOpensToAllUnnamed(Module m, String pkg); 225 226 /** 227 * Updates module m to open all packages in the given sets. 228 * / 229 void addOpensToAllUnnamed(Module m, Set<String> concealedPkgs, Set<String> exportedPkgs); 230 231 /** 232 * Updates module m to use a service. 233 * / 234 void addUses(Module m, Class<?> service); 235 236 /** 237 * Returns true if module m reflectively exports a package to other 238 * / 239 boolean isReflectivelyExported(Module module, String pn, Module other); 240 241 /** 242 * Returns true if module m reflectively opens a package to other 243 * / 244 boolean isReflectivelyOpened(Module module, String pn, Module other); 245 246 /** 247 * Updates module m to allow access to restricted methods. 248 * / 249 Module addEnableNativeAccess(Module m); 250 251 /** 252 * Updates all unnamed modules to allow access to restricted methods. 253 * / 254 void addEnableNativeAccessToAllUnnamed(); 255 256 /** 257 * Ensure that the given module has native access. If not, warn or 258 * throw exception depending on the configuration. 259 * / 260 void ensureNativeAccess(Module m, Class<?> owner, String methodName); 261 262 /** 263 * Returns the ServicesCatalog for the given Layer. 264 * / 265 ServicesCatalog getServicesCatalog(ModuleLayer layer); 266 267 /** 268 * Record that this layer has at least one module defined to the given 269 * class loader. 270 * / 271 void bindToLoader(ModuleLayer layer, ClassLoader loader); 272 273 /** 274 * Returns an ordered stream of layers. The first element is the 275 * given layer, the remaining elements are its parents, in DFS order. 276 * / 277 Stream<ModuleLayer> layers(ModuleLayer layer); 278 279 /** 280 * Returns a stream of the layers that have modules defined to the 281 * given class loader. 282 * / 283 Stream<ModuleLayer> layers(ClassLoader loader); 284 285 /** 286 * Count the number of leading positive bytes in the range. 287 * / 288 int countPositives(byte[] ba, int off, int len); 289 290 /** 291 * Constructs a new {@code String} by decoding the specified subarray of 292 * bytes using the specified {@linkplain java.nio.charset.Charset charset}. 293 * 294 * The caller of this method shall relinquish and transfer the ownership of 295 * the byte array to the callee since the later will not make a copy. 296 * 297 * @param bytes the byte array source 298 * @param cs the Charset 299 * @return the newly created string 300 * @throws CharacterCodingException for malformed or unmappable bytes 301 * / 302 String newStringNoRepl(byte[] bytes, Charset cs) throws CharacterCodingException; 303 304 /** 305 * Encode the given string into a sequence of bytes using the specified Charset. 306 * 307 * This method avoids copying the String's internal representation if the input 308 * is ASCII. 309 * 310 * This method throws CharacterCodingException instead of replacing when 311 * malformed input or unmappable characters are encountered. 312 * 313 * @param s the string to encode 314 * @param cs the charset 315 * @return the encoded bytes 316 * @throws CharacterCodingException for malformed input or unmappable characters 317 * / 318 byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException; 319 320 /** 321 * Returns a new string by decoding from the given utf8 bytes array. 322 * 323 * @param off the index of the first byte to decode 324 * @param len the number of bytes to decode 325 * @return the newly created string 326 * @throws IllegalArgumentException for malformed or unmappable bytes. 327 * / 328 String newStringUTF8NoRepl(byte[] bytes, int off, int len); 329 330 /** 331 * Get the char at index in a byte[] in internal UTF-16 representation, 332 * with no bounds checks. 333 * 334 * @param bytes the UTF-16 encoded bytes 335 * @param index of the char to retrieve, 0 <= index < (bytes.length >> 1) 336 * @return the char value 337 * / 338 char getUTF16Char(byte[] bytes, int index); 339 340 /** 341 * Encode the given string into a sequence of bytes using utf8. 342 * 343 * @param s the string to encode 344 * @return the encoded bytes in utf8 345 * @throws IllegalArgumentException for malformed surrogates 346 * / 347 byte[] getBytesUTF8NoRepl(String s); 348 349 /** 350 * Inflated copy from byte[] to char[], as defined by StringLatin1.inflate 351 * / 352 void inflateBytesToChars(byte[] src, int srcOff, char[] dst, int dstOff, int len); 353 354 /** 355 * Decodes ASCII from the source byte array into the destination 356 * char array. 357 * 358 * @return the number of bytes successfully decoded, at most len 359 * / 360 int decodeASCII(byte[] src, int srcOff, char[] dst, int dstOff, int len); 361 362 /** 363 * Returns the initial `System.in` to determine if it is replaced 364 * with `System.setIn(newIn)` method 365 * / 366 InputStream initialSystemIn(); 367 368 /** 369 * Encodes ASCII codepoints as possible from the source array into 370 * the destination byte array, assuming that the encoding is ASCII 371 * compatible 372 * 373 * @return the number of bytes successfully encoded, or 0 if none 374 * / 375 int encodeASCII(char[] src, int srcOff, byte[] dst, int dstOff, int len); 376 377 /** 378 * Set the cause of Throwable 379 * @param cause set t's cause to new value 380 * / 381 void setCause(Throwable t, Throwable cause); 382 383 /** 384 * Get protection domain of the given Class 385 * / 386 ProtectionDomain protectionDomain(Class<?> c); 387 388 /** 389 * Get a method handle of string concat helper method 390 * / 391 MethodHandle stringConcatHelper(String name, MethodType methodType); 392 393 /** 394 * Get the string concat initial coder 395 * / 396 long stringConcatInitialCoder(); 397 398 /** 399 * Update lengthCoder for constant 400 * / 401 long stringConcatMix(long lengthCoder, String constant); 402 */ 403 // END Android-removed: Not used in Android. 404 405 // BEGIN Android-removed: Preview features not supported. 406 /* 407 /** 408 * Get the coder for the supplied character. 409 * / 410 @PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES) 411 long stringConcatCoder(char value); 412 413 /** 414 * Update lengthCoder for StringBuilder. 415 * / 416 @PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES) 417 long stringBuilderConcatMix(long lengthCoder, StringBuilder sb); 418 419 /** 420 * Prepend StringBuilder content. 421 * / 422 @PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES) 423 long stringBuilderConcatPrepend(long lengthCoder, byte[] buf, StringBuilder sb); 424 */ 425 // END Android-removed: Preview features not supported. 426 427 // BEGIN Android-removed: Not used in Android. 428 /* 429 /** 430 * Join strings 431 * / 432 String join(String prefix, String suffix, String delimiter, String[] elements, int size); 433 434 /* 435 * Get the class data associated with the given class. 436 * @param c the class 437 * @see java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...) 438 * / 439 Object classData(Class<?> c); 440 441 long findNative(ClassLoader loader, String entry); 442 443 /** 444 * Direct access to Shutdown.exit to avoid security manager checks 445 * @param statusCode the status code 446 * / 447 void exit(int statusCode); 448 449 /** 450 * Returns an array of all platform threads. 451 * / 452 Thread[] getAllThreads(); 453 454 /** 455 * Returns the ThreadContainer for a thread, may be null. 456 * / 457 ThreadContainer threadContainer(Thread thread); 458 459 /** 460 * Starts a thread in the given ThreadContainer. 461 * / 462 void start(Thread thread, ThreadContainer container); 463 464 /** 465 * Returns the top of the given thread's stackable scope stack. 466 * / 467 StackableScope headStackableScope(Thread thread); 468 469 /** 470 * Sets the top of the current thread's stackable scope stack. 471 * / 472 void setHeadStackableScope(StackableScope scope); 473 474 /** 475 * Returns the Thread object for the current platform thread. If the 476 * current thread is a virtual thread then this method returns the carrier. 477 * / 478 Thread currentCarrierThread(); 479 480 /** 481 * Executes the given value returning task on the current carrier thread. 482 * / 483 <V> V executeOnCarrierThread(Callable<V> task) throws Exception; 484 485 /** 486 * Returns the value of the current carrier thread's copy of a thread-local. 487 * / 488 <T> T getCarrierThreadLocal(CarrierThreadLocal<T> local); 489 490 /** 491 * Sets the value of the current carrier thread's copy of a thread-local. 492 * / 493 <T> void setCarrierThreadLocal(CarrierThreadLocal<T> local, T value); 494 495 /** 496 * Removes the value of the current carrier thread's copy of a thread-local. 497 * / 498 void removeCarrierThreadLocal(CarrierThreadLocal<?> local); 499 500 /** 501 * Returns {@code true} if there is a value in the current carrier thread's copy of 502 * thread-local, even if that values is {@code null}. 503 * / 504 boolean isCarrierThreadLocalPresent(CarrierThreadLocal<?> local); 505 506 /** 507 * Returns the current thread's scoped values cache 508 * / 509 Object[] scopedValueCache(); 510 511 /** 512 * Sets the current thread's scoped values cache 513 * / 514 void setScopedValueCache(Object[] cache); 515 516 /** 517 * Return the current thread's scoped value bindings. 518 * / 519 Object scopedValueBindings(); 520 521 /** 522 * Returns the innermost mounted continuation 523 * / 524 Continuation getContinuation(Thread thread); 525 526 /** 527 * Sets the innermost mounted continuation 528 * / 529 void setContinuation(Thread thread, Continuation continuation); 530 531 /** 532 * The ContinuationScope of virtual thread continuations 533 * / 534 ContinuationScope virtualThreadContinuationScope(); 535 536 /** 537 * Parks the current virtual thread. 538 * @throws WrongThreadException if the current thread is not a virtual thread 539 * / 540 void parkVirtualThread(); 541 542 /** 543 * Parks the current virtual thread for up to the given waiting time. 544 * @param nanos the maximum number of nanoseconds to wait 545 * @throws WrongThreadException if the current thread is not a virtual thread 546 * / 547 void parkVirtualThread(long nanos); 548 549 /** 550 * Re-enables a virtual thread for scheduling. If the thread was parked then 551 * it will be unblocked, otherwise its next attempt to park will not block 552 * @param thread the virtual thread to unpark 553 * @throws IllegalArgumentException if the thread is not a virtual thread 554 * @throws RejectedExecutionException if the scheduler cannot accept a task 555 * / 556 void unparkVirtualThread(Thread thread); 557 558 /** 559 * Creates a new StackWalker 560 * / 561 StackWalker newStackWalkerInstance(Set<StackWalker.Option> options, 562 ContinuationScope contScope, 563 Continuation continuation); 564 /** 565 * Returns '<loader-name>' @<id> if classloader has a name 566 * explicitly set otherwise <qualified-class-name> @<id> 567 * / 568 String getLoaderNameID(ClassLoader loader); 569 570 /** 571 * Is a security manager already set or allowed to be set 572 * (using -Djava.security.manager=allow)? 573 * / 574 boolean allowSecurityManager(); 575 */ 576 // END Android-removed: Not used in Android. 577 } 578