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