Path: blob/master/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java
41159 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.access;2627import java.lang.annotation.Annotation;28import java.lang.invoke.MethodHandle;29import java.lang.invoke.MethodHandles;30import java.lang.invoke.MethodType;31import java.lang.module.ModuleDescriptor;32import java.lang.reflect.Executable;33import java.lang.reflect.Method;34import java.net.URI;35import java.nio.charset.CharacterCodingException;36import java.nio.charset.Charset;37import java.security.AccessControlContext;38import java.security.ProtectionDomain;39import java.util.List;40import java.util.Map;41import java.util.Set;42import java.util.concurrent.ConcurrentHashMap;43import java.util.stream.Stream;4445import jdk.internal.module.ServicesCatalog;46import jdk.internal.reflect.ConstantPool;47import sun.reflect.annotation.AnnotationType;48import sun.nio.ch.Interruptible;4950public interface JavaLangAccess {5152/**53* Returns the list of {@code Method} objects for the declared public54* methods of this class or interface that have the specified method name55* and parameter types.56*/57List<Method> getDeclaredPublicMethods(Class<?> klass, String name, Class<?>... parameterTypes);5859/**60* Return the constant pool for a class.61*/62ConstantPool getConstantPool(Class<?> klass);6364/**65* Compare-And-Set the AnnotationType instance corresponding to this class.66* (This method only applies to annotation types.)67*/68boolean casAnnotationType(Class<?> klass, AnnotationType oldType, AnnotationType newType);6970/**71* Get the AnnotationType instance corresponding to this class.72* (This method only applies to annotation types.)73*/74AnnotationType getAnnotationType(Class<?> klass);7576/**77* Get the declared annotations for a given class, indexed by their types.78*/79Map<Class<? extends Annotation>, Annotation> getDeclaredAnnotationMap(Class<?> klass);8081/**82* Get the array of bytes that is the class-file representation83* of this Class' annotations.84*/85byte[] getRawClassAnnotations(Class<?> klass);8687/**88* Get the array of bytes that is the class-file representation89* of this Class' type annotations.90*/91byte[] getRawClassTypeAnnotations(Class<?> klass);9293/**94* Get the array of bytes that is the class-file representation95* of this Executable's type annotations.96*/97byte[] getRawExecutableTypeAnnotations(Executable executable);9899/**100* Returns the elements of an enum class or null if the101* Class object does not represent an enum type;102* the result is uncloned, cached, and shared by all callers.103*/104<E extends Enum<E>> E[] getEnumConstantsShared(Class<E> klass);105106/**107* Set current thread's blocker field.108*/109void blockedOn(Interruptible b);110111/**112* Registers a shutdown hook.113*114* It is expected that this method with registerShutdownInProgress=true115* is only used to register DeleteOnExitHook since the first file116* may be added to the delete on exit list by the application shutdown117* hooks.118*119* @param slot the slot in the shutdown hook array, whose element120* will be invoked in order during shutdown121* @param registerShutdownInProgress true to allow the hook122* to be registered even if the shutdown is in progress.123* @param hook the hook to be registered124*125* @throws IllegalStateException if shutdown is in progress and126* the slot is not valid to register.127*/128void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook);129130/**131* Returns a new Thread with the given Runnable and an132* inherited AccessControlContext.133*/134Thread newThreadWithAcc(Runnable target, @SuppressWarnings("removal") AccessControlContext acc);135136/**137* Invokes the finalize method of the given object.138*/139void invokeFinalize(Object o) throws Throwable;140141/**142* Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s)143* associated with the given class loader, creating it if it doesn't already exist.144*/145ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap(ClassLoader cl);146147/**148* Defines a class with the given name to a class loader.149*/150Class<?> defineClass(ClassLoader cl, String name, byte[] b, ProtectionDomain pd, String source);151152/**153* Defines a class with the given name to a class loader with154* the given flags and class data.155*156* @see java.lang.invoke.MethodHandles.Lookup#defineClass157*/158Class<?> defineClass(ClassLoader cl, Class<?> lookup, String name, byte[] b, ProtectionDomain pd, boolean initialize, int flags, Object classData);159160/**161* Returns a class loaded by the bootstrap class loader.162*/163Class<?> findBootstrapClassOrNull(String name);164165/**166* Define a Package of the given name and module by the given class loader.167*/168Package definePackage(ClassLoader cl, String name, Module module);169170/**171* Invokes Long.fastUUID172*/173String fastUUID(long lsb, long msb);174175/**176* Record the non-exported packages of the modules in the given layer177*/178void addNonExportedPackages(ModuleLayer layer);179180/**181* Invalidate package access cache182*/183void invalidatePackageAccessCache();184185/**186* Defines a new module to the Java virtual machine. The module187* is defined to the given class loader.188*189* The URI is for information purposes only, it can be {@code null}.190*/191Module defineModule(ClassLoader loader, ModuleDescriptor descriptor, URI uri);192193/**194* Defines the unnamed module for the given class loader.195*/196Module defineUnnamedModule(ClassLoader loader);197198/**199* Updates the readability so that module m1 reads m2. The new read edge200* does not result in a strong reference to m2 (m2 can be GC'ed).201*202* This method is the same as m1.addReads(m2) but without a permission check.203*/204void addReads(Module m1, Module m2);205206/**207* Updates module m to read all unnamed modules.208*/209void addReadsAllUnnamed(Module m);210211/**212* Updates module m1 to export a package unconditionally.213*/214void addExports(Module m1, String pkg);215216/**217* Updates module m1 to export a package to module m2. The export does218* not result in a strong reference to m2 (m2 can be GC'ed).219*/220void addExports(Module m1, String pkg, Module m2);221222/**223* Updates a module m to export a package to all unnamed modules.224*/225void addExportsToAllUnnamed(Module m, String pkg);226227/**228* Updates module m1 to open a package to module m2. Opening the229* package does not result in a strong reference to m2 (m2 can be GC'ed).230*/231void addOpens(Module m1, String pkg, Module m2);232233/**234* Updates module m to open a package to all unnamed modules.235*/236void addOpensToAllUnnamed(Module m, String pkg);237238/**239* Updates module m to open all packages in the given sets.240*/241void addOpensToAllUnnamed(Module m, Set<String> concealedPkgs, Set<String> exportedPkgs);242243/**244* Updates module m to use a service.245*/246void addUses(Module m, Class<?> service);247248/**249* Returns true if module m reflectively exports a package to other250*/251boolean isReflectivelyExported(Module module, String pn, Module other);252253/**254* Returns true if module m reflectively opens a package to other255*/256boolean isReflectivelyOpened(Module module, String pn, Module other);257258/**259* Updates module m to allow access to restricted methods.260*/261Module addEnableNativeAccess(Module m);262263/**264* Updates all unnamed modules to allow access to restricted methods.265*/266void addEnableNativeAccessAllUnnamed();267268/**269* Returns true if module m can access restricted methods.270*/271boolean isEnableNativeAccess(Module m);272273/**274* Returns the ServicesCatalog for the given Layer.275*/276ServicesCatalog getServicesCatalog(ModuleLayer layer);277278/**279* Record that this layer has at least one module defined to the given280* class loader.281*/282void bindToLoader(ModuleLayer layer, ClassLoader loader);283284/**285* Returns an ordered stream of layers. The first element is the286* given layer, the remaining elements are its parents, in DFS order.287*/288Stream<ModuleLayer> layers(ModuleLayer layer);289290/**291* Returns a stream of the layers that have modules defined to the292* given class loader.293*/294Stream<ModuleLayer> layers(ClassLoader loader);295296/**297* Constructs a new {@code String} by decoding the specified subarray of298* bytes using the specified {@linkplain java.nio.charset.Charset charset}.299*300* The caller of this method shall relinquish and transfer the ownership of301* the byte array to the callee since the later will not make a copy.302*303* @param bytes the byte array source304* @param cs the Charset305* @return the newly created string306* @throws CharacterCodingException for malformed or unmappable bytes307*/308String newStringNoRepl(byte[] bytes, Charset cs) throws CharacterCodingException;309310/**311* Encode the given string into a sequence of bytes using the specified Charset.312*313* This method avoids copying the String's internal representation if the input314* is ASCII.315*316* This method throws CharacterCodingException instead of replacing when317* malformed input or unmappable characters are encountered.318*319* @param s the string to encode320* @param cs the charset321* @return the encoded bytes322* @throws CharacterCodingException for malformed input or unmappable characters323*/324byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException;325326/**327* Returns a new string by decoding from the given utf8 bytes array.328*329* @param off the index of the first byte to decode330* @param len the number of bytes to decode331* @return the newly created string332* @throws IllegalArgumentException for malformed or unmappable bytes.333*/334String newStringUTF8NoRepl(byte[] bytes, int off, int len);335336/**337* Encode the given string into a sequence of bytes using utf8.338*339* @param s the string to encode340* @return the encoded bytes in utf8341* @throws IllegalArgumentException for malformed surrogates342*/343byte[] getBytesUTF8NoRepl(String s);344345/**346* Inflated copy from byte[] to char[], as defined by StringLatin1.inflate347*/348void inflateBytesToChars(byte[] src, int srcOff, char[] dst, int dstOff, int len);349350/**351* Decodes ASCII from the source byte array into the destination352* char array.353*354* @return the number of bytes successfully decoded, at most len355*/356int decodeASCII(byte[] src, int srcOff, char[] dst, int dstOff, int len);357358/**359* Set the cause of Throwable360* @param cause set t's cause to new value361*/362void setCause(Throwable t, Throwable cause);363364/**365* Get protection domain of the given Class366*/367ProtectionDomain protectionDomain(Class<?> c);368369/**370* Get a method handle of string concat helper method371*/372MethodHandle stringConcatHelper(String name, MethodType methodType);373374/**375* Get the string concat initial coder376*/377long stringConcatInitialCoder();378379/**380* Update lengthCoder for constant381*/382long stringConcatMix(long lengthCoder, String constant);383384/**385* Join strings386*/387String join(String prefix, String suffix, String delimiter, String[] elements, int size);388389/*390* Get the class data associated with the given class.391* @param c the class392* @see java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)393*/394Object classData(Class<?> c);395396long findNative(ClassLoader loader, String entry);397}398399400