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